Python實(shí)現(xiàn)爬蟲設(shè)置代理IP和偽裝成瀏覽器的方法分享
1.python爬蟲瀏覽器偽裝
#導(dǎo)入urllib.request模塊 import urllib.request #設(shè)置請(qǐng)求頭 headers=("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0") #創(chuàng)建一個(gè)opener opener=urllib.request.build_opener() #將headers添加到opener中 opener.addheaders=[headers] #將opener安裝為全局 urllib.request.install_opener(opener) #用urlopen打開網(wǎng)頁 data=urllib.request.urlopen(url).read().decode('utf-8','ignore')
2.設(shè)置代理
#定義代理ip proxy_addr="122.241.72.191:808" #設(shè)置代理 proxy=urllib.request.ProxyHandle({'http':proxy_addr}) #創(chuàng)建一個(gè)opener opener=urllib.request.build_opener(proxy,urllib.request.HTTPHandle) #將opener安裝為全局 urllib.request.install_opener(opener) #用urlopen打開網(wǎng)頁 data=urllib.request.urlopen(url).read().decode('utf-8','ignore')
3.同時(shí)設(shè)置用代理和模擬瀏覽器訪問
#定義代理ip proxy_addr="122.241.72.191:808" #創(chuàng)建一個(gè)請(qǐng)求 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) #設(shè)置代理 proxy=urllib.request.ProxyHandle("http":proxy_addr) #創(chuàng)建一個(gè)opener opener=urllib.request.build_opener(proxy,urllib.request.HTTPHandle) #將opener安裝為全局 urllib.request.install_opener(opener) #用urlopen打開網(wǎng)頁 data=urllib.request.urlopen(req).read().decode('utf-8','ignore')
4.在請(qǐng)求頭中添加多個(gè)信息
import urllib.request page_headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0", "Host":"www.baidu.com", "Cookie":"xxxxxxxx" } req=urllib.request.Request(url,headers=page_headers) data=urllib.request.urlopen(req).read().decode('utf-8','ignore')
5.添加post請(qǐng)求參數(shù)
import urllib.request import urllib.parse #設(shè)置post參數(shù) page_data=urllib.parse.urlencode([ ('pn',page_num), ('kd',keywords) ]) #設(shè)置headers page_headers={ 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0', 'Connection':'keep-alive', 'Host':'www.lagou.com', 'Origin':'https://www.lagou.com', 'Cookie':'JSESSIONID=ABAAABAABEEAAJA8F28C00A88DC4D771796BB5C6FFA2DDA; user_trace_token=20170715131136-d58c1f22f6434e9992fc0b35819a572b', 'Accept':'application/json, text/javascript, */*; q=0.01', 'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8', 'Referer':'https://www.lagou.com/jobs/list_%E6%95%B0%E6%8D%AE%E6%8C%96%E6%8E%98?labelWords=&fromSearch=true&suginput=', 'X-Anit-Forge-Token':'None', 'X-Requested-With':'XMLHttpRequest' } #打開網(wǎng)頁 req=urllib.request.Request(url,headers=page_headers) data=urllib.request.urlopen(req,data=page_data.encode('utf-8')).read().decode('utf-8')
6.利用phantomjs模擬瀏覽器請(qǐng)求
#1.下載phantomjs安裝到本地,并設(shè)置環(huán)境變量 from selenium import webdriver bs=webdriver.PhantomJS() #打開url bs.get(url) #獲取網(wǎng)頁源碼 url_data=bs.page_source #將瀏覽到的網(wǎng)頁保存為圖片 bs.get_screenshot_as_file(filename)
7.phantomjs設(shè)置user-agent和cookie
from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities dcap = dict(DesiredCapabilities.PHANTOMJS) dcap["phantomjs.page.settings.userAgent"] = ("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0") bs = webdriver.PhantomJS(desired_capabilities=dcap) bs.get(url) #刪除cookie bs.delete_all_cookies() #設(shè)置cookie #cookie格式:在瀏覽器cookie中查看,一個(gè)cookie需要包含以下參數(shù),domain、name、value、path cookie={ 'domain':'.www.baidu.com', #注意前面有. 'name':'xxxx', 'value':'xxxx', 'path':'xxxx' } #向phantomjs中添加cookie bs.add_cookie(cookie)
8.利用web_driver工具
#1.下載web_driver工具(如chromdriver.exe)及對(duì)應(yīng)的瀏覽器 #2.將chromdriver.exe放到某個(gè)目錄,如c:\chromdriver.exe from selenium import webdriver driver=webdriver.Chrome(executable_path="C:\chromdriver.exe") #打開url driver.get(url)
以上這篇Python實(shí)現(xiàn)爬蟲設(shè)置代理IP和偽裝成瀏覽器的方法分享就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Python爬蟲簡(jiǎn)單運(yùn)用爬取代理IP的實(shí)現(xiàn)
- python爬蟲構(gòu)建代理ip池抓取數(shù)據(jù)庫的示例代碼
- Python爬蟲設(shè)置ip代理過程解析
- python3 Scrapy爬蟲框架ip代理配置的方法
- Python爬蟲使用代理IP的實(shí)現(xiàn)
- Python代理IP爬蟲的新手使用教程
- 檢測(cè)python爬蟲時(shí)是否代理ip偽裝成功的方法
- Python爬蟲動(dòng)態(tài)ip代理防止被封的方法
- selenium+python設(shè)置爬蟲代理IP的方法
- Python爬蟲常用小技巧之設(shè)置代理IP
- python爬取代理ip的示例
相關(guān)文章
python數(shù)據(jù)可視化Seaborn畫熱力圖
這篇文章主要介紹了數(shù)據(jù)可視化Seaborn畫熱力圖,熱力圖的想法其實(shí)很簡(jiǎn)單,用顏色替換數(shù)字,下面我們來看看文章對(duì)操作過程的具體介紹吧,需要的小伙伴可以參考一下具體內(nèi)容,希望對(duì)你有所幫助2022-01-01四步教你學(xué)會(huì)打包一個(gè)新的Python模塊
當(dāng)你安裝應(yīng)用程序時(shí),通常是安裝一個(gè)軟件包,其中包含應(yīng)用程序的可執(zhí)行代碼和重要文件。在?Linux上,軟件一般被打包成RPM或DEB等格式,然而幾乎每天都有新的Python模塊發(fā)布,因此你很容易遇到一個(gè)尚未打包的Python模塊。本文教你四步打包一個(gè)新的Python模塊2022-09-09ID3決策樹以及Python實(shí)現(xiàn)詳細(xì)過程
決策樹是我本人非常喜歡的機(jī)器學(xué)習(xí)模型,非常直觀容易理解,并且和數(shù)據(jù)結(jié)構(gòu)的結(jié)合很緊密,下面這篇文章主要給大家介紹了關(guān)于ID3決策樹以及Python實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2024-01-01python機(jī)器學(xué)習(xí)pytorch?張量基礎(chǔ)教程
這篇文章主要為大家介紹了python機(jī)器學(xué)習(xí)pytorch?張量基礎(chǔ)教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10pygame學(xué)習(xí)筆記之設(shè)置字體及顯示中文
游戲界面中文字也是非常常見的元素之一,pygame專門提供了Font模塊來支持文字的顯示,下面這篇文章主要給大家介紹了關(guān)于pygame學(xué)習(xí)筆記之設(shè)置字體及顯示中文的相關(guān)資料,需要的朋友可以參考下2022-07-07Python使用Tesseract實(shí)現(xiàn)從圖像中讀取文本
Tesseract?是一個(gè)基于計(jì)算機(jī)的系統(tǒng),用于光學(xué)字符識(shí)別?(OCR)?和其他圖像到文本處理,本文將介紹如何使用?Python?中的?Tesseract?創(chuàng)建一個(gè)可以從圖像中讀取文本的程序,需要的可以參考下2023-11-11