Python爬蟲(chóng)設(shè)置ip代理過(guò)程解析
1、get方式:如何為爬蟲(chóng)添加ip代理,設(shè)置Request header(請(qǐng)求頭)
import urllib
import urllib.request
import urllib.parse
import random
import time
from fake_useragent import UserAgent
ua = UserAgent()
url = "http://www.baidu.com"
########################################################
'''
設(shè)置ip代理
iplist = [ '127.0.0.1:80'] #可自行上網(wǎng)找一些代理
proxy_support = urllib.request.ProxyHandler({'http':random.choice(iplist)}) #也可以設(shè)置為https,要看你的代理支不支持
opener = urllib.request.build_opener(proxy_support)
'''
########################################################
'''無(wú)ip代理'''
opener = urllib.request.build_opener()
'''f12查看請(qǐng)求頭添加即可,不一定都需要全添加↓↓↓'''
opener.addheaders = [('Host', 'newtab.firefoxchina.cn'),
('User-Agent',ua.random),
('Accept-Encoding','deflate, br'),
('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'),
('Accept-Language', 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2'),
('Connection', 'keep-alive'),
('Upgrade-Insecure-Requests',1),
('Cookie', '__gads=ID=138080209be66bf8:T=1592037395:S=ALNI_Ma-g9wHmfxFL4GCy9veAjJrJRsNmg; Hm_lvt_dd4738b5fb302cb062ef19107df5d2e4=1592449208,1592471447,1592471736,1594001802; uid=rBADnV7m04mi8wRJK3xYAg=='),
]
urllib.request.install_opener(opener)
while True:
try:
response = urllib.request.urlopen(url)
break
except Exception as e:
print("錯(cuò)誤信息:" + str(e))
time.sleep(3)
html = response.read().decode("utf-8")
print(html)
2、post方式添加載荷(此處是打比方),修改urllib.request.install_opener(opener)以下的代碼即可
urllib.request.install_opener(opener)
# data = {} #當(dāng)頁(yè)面提交數(shù)據(jù)是有載荷但是載荷內(nèi)容為空時(shí),必須以data = {}傳參,不然無(wú)法獲取網(wǎng)頁(yè)數(shù)據(jù)
data = {'_csrf':'請(qǐng)把',
'collection-name':'載荷的參數(shù)',
'description':'以這種形式',
'_csrf':'裝載'
}
data = urllib.parse.urlencode(data).encode('utf-8')
req = urllib.request.Request(url,data)
while True:
try:
response = urllib.request.urlopen(req)
break
except Exception as e:
print("錯(cuò)誤信息:" + str(e))
time.sleep(3)
html = response.read().decode("utf-8")
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python 中的paramiko模塊簡(jiǎn)介及安裝過(guò)程
這篇文章主要介紹了python 中的paramiko模塊簡(jiǎn)介及安裝過(guò)程,通過(guò)實(shí)例詳解給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-02-02
python如何實(shí)現(xiàn)數(shù)組反轉(zhuǎn)
這篇文章主要介紹了python如何實(shí)現(xiàn)數(shù)組反轉(zhuǎn)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
python 自動(dòng)刷新網(wǎng)頁(yè)的兩種方法
這篇文章主要介紹了python 自動(dòng)刷新網(wǎng)頁(yè)的兩種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Python asyncio異步編程常見(jiàn)問(wèn)題小結(jié)
本文主要介紹了Python asyncio異步編程常見(jiàn)問(wèn)題小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
Python爬蟲(chóng)原理與基本請(qǐng)求庫(kù)urllib詳解
這篇文章主要介紹了Python爬蟲(chóng)原理與基本請(qǐng)求庫(kù)urllib詳解,爬蟲(chóng)就是通過(guò)模擬瀏覽器,按照一定的規(guī)則,自動(dòng)、大批量的獲取網(wǎng)絡(luò)資源,包括文本、圖片、鏈接、音頻、視頻等等,需要的朋友可以參考下2023-07-07

