pythonブラウザの偽装

1802 ワード

https://www.jb51.net/article/139587.htm
python爬虫類ブラウザ偽装
1.
#urllibをインポートrequestモジュールimport urllib.request#設定要求ヘッダheaders=(「User-Agent」,「Mozilla/5.0(Windows NT 6.1;WOW 64)AppleWebKit/53.36(KHTML,like Gecko)Chrome/49.2623.221 Safari/53.36 SE 2.X MetaSr 1.0」)#Openropener=urllibを作成する.request.build_Opener()#headersをopenerに追加するopener.addheaders=[headers]#openerをグローバルurllibにインストールする.request.install_Opener(opener)#urlopenでWebページdata=urllibを開くrequest.urlopen(url).read().decode('utf-8','ignore')
 
2. # ip proxy_addr="122.241.72.191:808" # proxy=urllib.request.ProxyHandle({'http':proxy_addr}) # opener opener=urllib.request.build_opener(proxy,urllib.request.HTTPHandle) # opener urllib.request.install_opener(opener) # urlopen data=urllib.request.urlopen(url).read().decode('utf-8','ignore')
 
 
3. # ip proxy_addr="122.241.72.191:808" # req=urllib.request.Request(url) # headers req.add_header("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) # proxy=urllib.request.ProxyHandle("http":proxy_addr) # opener opener=urllib.request.build_opener(proxy,urllib.request.HTTPHandle) # opener urllib.request.install_opener(opener) # urlopen data=urllib.request.urlopen(req).read().decode('utf-8','ignore')
転載先:https://www.cnblogs.com/tao-yuan/p/10409459.html