Python實現(xiàn)爬蟲設(shè)置代理IP和偽裝成瀏覽器的方法分享
1.python爬蟲瀏覽器偽裝
#導(dǎo)入urllib.request模塊
import urllib.request
#設(shè)置請求頭
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)建一個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)建一個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è)置用代理和模擬瀏覽器訪問
#定義代理ip
proxy_addr="122.241.72.191:808"
#創(chuà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)建一個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.在請求頭中添加多個信息
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請求參數(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模擬瀏覽器請求
#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中查看,一個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)及對應(yīng)的瀏覽器 #2.將chromdriver.exe放到某個目錄,如c:\chromdriver.exe from selenium import webdriver driver=webdriver.Chrome(executable_path="C:\chromdriver.exe") #打開url driver.get(url)
以上這篇Python實現(xiàn)爬蟲設(shè)置代理IP和偽裝成瀏覽器的方法分享就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python數(shù)據(jù)可視化Seaborn畫熱力圖
這篇文章主要介紹了數(shù)據(jù)可視化Seaborn畫熱力圖,熱力圖的想法其實很簡單,用顏色替換數(shù)字,下面我們來看看文章對操作過程的具體介紹吧,需要的小伙伴可以參考一下具體內(nèi)容,希望對你有所幫助2022-01-01
ID3決策樹以及Python實現(xiàn)詳細(xì)過程
決策樹是我本人非常喜歡的機(jī)器學(xué)習(xí)模型,非常直觀容易理解,并且和數(shù)據(jù)結(jié)構(gòu)的結(jié)合很緊密,下面這篇文章主要給大家介紹了關(guān)于ID3決策樹以及Python實現(xiàn)的相關(guān)資料,需要的朋友可以參考下2024-01-01
python機(jī)器學(xué)習(xí)pytorch?張量基礎(chǔ)教程
這篇文章主要為大家介紹了python機(jī)器學(xué)習(xí)pytorch?張量基礎(chǔ)教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
pygame學(xué)習(xí)筆記之設(shè)置字體及顯示中文
游戲界面中文字也是非常常見的元素之一,pygame專門提供了Font模塊來支持文字的顯示,下面這篇文章主要給大家介紹了關(guān)于pygame學(xué)習(xí)筆記之設(shè)置字體及顯示中文的相關(guān)資料,需要的朋友可以參考下2022-07-07
Python使用Tesseract實現(xiàn)從圖像中讀取文本
Tesseract?是一個基于計算機(jī)的系統(tǒng),用于光學(xué)字符識別?(OCR)?和其他圖像到文本處理,本文將介紹如何使用?Python?中的?Tesseract?創(chuàng)建一個可以從圖像中讀取文本的程序,需要的可以參考下2023-11-11

