Python實現(xiàn)爬蟲設(shè)置代理IP和偽裝成瀏覽器的方法分享
更新時間:2018年05月07日 09:14:54 作者:Jepson2017
今天小編就為大家分享一篇Python實現(xiàn)爬蟲設(shè)置代理IP和偽裝成瀏覽器的方法分享,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
1.python爬蟲瀏覽器偽裝
#導入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-01Python使用Tesseract實現(xiàn)從圖像中讀取文本
Tesseract?是一個基于計算機的系統(tǒng),用于光學字符識別?(OCR)?和其他圖像到文本處理,本文將介紹如何使用?Python?中的?Tesseract?創(chuàng)建一個可以從圖像中讀取文本的程序,需要的可以參考下2023-11-11