mitmproxy抓取商標(biāo)網(wǎng)查詢數(shù)據(jù)

先開(kāi)啟mitmproxy, 命令如下

mitmweb -s 抓取.py

我們需要對(duì)request的url做過(guò)濾,發(fā)現(xiàn)請(qǐng)求的url包含有ajax字符就提取response內(nèi)容,然后使用bs4解析xml,獲取需要的數(shù)據(jù),代碼如下:

from mitmproxy import ctx

from bs4 import BeautifulSoup

import pandas as pd

from selenium import webdriver

# def request(flow):

#? ? flow.request.headers['user-agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'

def run_selenium():

? ? driver = webdriver.PhantomJS()

? ? url = 'http://wsjs.saic.gov.cn/txnRead01.do?SVVVdE0o=KalIkqkedI6edI6edpSi_r6ZKYhRAJQahSFFMpYtTEaqqH0'

? ? driver.get(url)


def response(flow):

? ? ctx.log.error('獲取的url是: ' + flow.request.url)

? ? if 'txnRead02.ajax' in flow.request.url:

? ? ? ? soup = BeautifulSoup(flow.response.text, 'xml')

? ? ? ? for record in soup.find_all('record'):

? ? ? ? ? ? item = {}

? ? ? ? ? ? item['index'] = record.find('index').get_text()

? ? ? ? ? ? item['注冊(cè)號(hào)'] = record.find('sn').get_text()

? ? ? ? ? ? item['中文名稱(chēng)'] = record.find('hnc').get_text()

? ? ? ? ? ? item['注冊(cè)時(shí)間'] = record.find('mno').get_text()

? ? ? ? ? ? item['英文名稱(chēng)'] = record.find('hne').get_text()

? ? ? ? ? ? item['國(guó)際分類(lèi)'] = record.find('nc').get_text()

? ? ? ? ? ? ctx.log.warn(str(item))

? ? ? ? ? ? df = pd.DataFrame(item, index = ['0'])

? ? ? ? ? ? header = True if item['index'] == 1 else False

? ? ? ? ? ? df.to_csv('/爬蟲(chóng)例子/商標(biāo).csv', mode = 'a', encoding='utf_8_sig', index = False, header = header)

? ? ? ? # [ctx.log.warn(a.get('href')) for a in soup.find_all('a')]

if __name__ == "__main__":

? ? run_selenium()

在這里我并沒(méi)有使用selenium來(lái)自動(dòng)調(diào)用網(wǎng)頁(yè),后續(xù)會(huì)繼續(xù)對(duì)此進(jìn)行改進(jìn)的。

需要手動(dòng)進(jìn)行的操作就是翻頁(yè),翻頁(yè)后,mitmproxy會(huì)自動(dòng)獲取結(jié)果并且解析保存為csv的文件。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容