Python抓包并解析json爬蟲(chóng)的完整實(shí)例代碼
Python抓包并解析json爬蟲(chóng)
在使用Python爬蟲(chóng)的時(shí)候,通過(guò)抓包url,打開(kāi)url可能會(huì)遇見(jiàn)以下類似網(wǎng)址,打開(kāi)后會(huì)出現(xiàn)類似這樣的界面,無(wú)法繼續(xù)進(jìn)行爬蟲(chóng):
例如:
需要爬取網(wǎng)頁(yè)中第二頁(yè)的數(shù)據(jù)時(shí),點(diǎn)擊F12➡網(wǎng)絡(luò)(Network)➡XHR,最好點(diǎn)擊清除鍵,如下圖:
通過(guò)點(diǎn)擊“第二頁(yè)”,會(huì)出現(xiàn)一個(gè)POST請(qǐng)求(有時(shí)會(huì)是GET請(qǐng)求),點(diǎn)擊POST請(qǐng)求的url,(這里網(wǎng)址以POST請(qǐng)求為例),
如圖:
然后復(fù)制參數(shù)代碼
代碼展示:
import requestsimport jsonurl = ’https://m.ctrip.com/restapi/soa2/13444/json/getCommentCollapseList?_fxpcqlniredt=09031130211378497389’header={’authority’: ’m.ctrip.com’,’method’: ’POST’,’path’: ’/restapi/soa2/13444/json/getCommentCollapseList?_fxpcqlniredt=09031130211378497389’,’scheme’: ’https’,’accept’: ’*/*’,’accept-encoding’: ’gzip, deflate, br’,’accept-language’: ’zh-CN,zh;q=0.9’,’cache-control’: ’no-cache’,’content-length’: ’278’,’content-type’: ’application/json’,’cookie’: ’__utma=1.1986366783.1601607319.1601607319.1601607319.1; __utmz=1.1601607319.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); _RSG=blqD1d4mGX0BA_amPD3t29; _RDG=286710759c35f221c000cbec6169743cac; _RGUID=0850c049-c137-4be5-90b7-0cd67093f28b; MKT_CKID=1601607321903.rzptk.lbzh; _ga=GA1.2.1986366783.1601607319; nfes_isSupportWebP=1; appFloatCnt=8; _gcl_dc=GCL.1601638857.CKzg58XqlewCFQITvAodioIJWw; Session=SmartLinkCode=U155952&SmartLinkKeyWord=&SmartLinkQuary=&SmartLinkHost=&SmartLinkLanguage=zh; Union=OUID=index&AllianceID=4897&SID=155952&SourceID=&createtime=1602506741&Expires=1603111540922; MKT_OrderClick=ASID=4897155952&AID=4897&CSID=155952&OUID=index&CT=1602506740926&CURL=https%3A%2F%2Fwww.ctrip.com%2F%3Fsid%3D155952%26allianceid%3D4897%26ouid%3Dindex&VAL={'pc_vid':'1601607319353.3cid9z'}; MKT_Pagesource=PC; _RF1=218.58.59.72; _bfa=1.1601607319353.3cid9z.1.1602506738089.1602680023977.4.25; _bfi=p1%3D290510%26p2%3D290510%26v1%3D25%26v2%3D24; MKT_CKID_LMT=1602680029515; __zpspc=9.5.1602680029.1602680029.1%232%7Cwww.baidu.com%7C%7C%7C%25E6%2590%25BA%25E7%25A8%258B%7C%23; _gid=GA1.2.1363667416.1602680030; _jzqco=%7C%7C%7C%7C1602680029668%7C1.672451398.1601607321899.1602506755440.1602680029526.1602506755440.1602680029526.undefined.0.0.16.16’,’cookieorigin’: ’https://you.ctrip.com’,’origin’: ’https://you.ctrip.com’,’pragma’: ’no-cache’,’referer’: ’https://you.ctrip.com/’,’sec-fetch-dest’: ’empty’,’sec-fetch-mode’: ’cors’,’sec-fetch-site’: ’same-site’,’user-agent’: ’Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36’}dat = { 'arg': { ’channelType’: 2, ’collapseType’: 0, ’commentTagId’: 0, ’pageIndex’: 1, ’pageSize’: 10, ’poiId’: 75648, ’sortType’: 3, ’sourceType’: 1, ’starType’: 0 }, 'head': { ’auth’: '', ’cid’: '09031117213661657011', ’ctok’: '', ’cver’: '1.0', ’extension’: [], ’lang’: '01', ’sid’: '8888', ’syscode’: '09', ’xsid’: '' }}r = requests.post(url, data=json.dumps(dat), headers=header)s = r.json()print(s)
運(yùn)行結(jié)果:
然后右擊結(jié)果,再點(diǎn)擊Show as JSON:
最后就會(huì)出現(xiàn)目標(biāo)url的響應(yīng)信息,就可以進(jìn)行爬取了?。。?/p>
總結(jié)
到此這篇關(guān)于Python抓包并解析json爬蟲(chóng)的文章就介紹到這了,更多相關(guān)Python抓包并解析json爬蟲(chóng)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 淺談XML Schema中的elementFormDefault屬性2. jsp學(xué)習(xí)之scriptlet的使用方法詳解3. ASP.NET MVC獲取多級(jí)類別組合下的產(chǎn)品4. ASP.NET MVC實(shí)現(xiàn)橫向展示購(gòu)物車5. ThinkPHP5 通過(guò)ajax插入圖片并實(shí)時(shí)顯示(完整代碼)6. Docker 容器健康檢查機(jī)制7. 解決python 輸出到csv 出現(xiàn)多空行的情況8. Python利用ROI進(jìn)行圖像合成的問(wèn)題小結(jié)9. python 編寫(xiě)輸出到csv的操作10. Java xml數(shù)據(jù)格式返回實(shí)現(xiàn)操作
