久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

Python爬蟲設(shè)置ip代理過程解析

瀏覽:2日期:2022-07-17 08:00:47

1、get方式:如何為爬蟲添加ip代理,設(shè)置Request header(請(qǐng)求頭)

import urllib import urllib.requestimport urllib.parseimport randomimport timefrom fake_useragent import UserAgentua = 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)’’’########################################################’’’無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)頁面提交數(shù)據(jù)是有載荷但是載荷內(nèi)容為空時(shí),必須以data = {}傳參,不然無法獲取網(wǎng)頁數(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í)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 汾阳市| 安仁县| 洛阳市| 扎赉特旗| 鹿泉市| 正蓝旗| 瑞昌市| 来安县| 明光市| 泽库县| 峨眉山市| 龙里县| 遂平县| 天门市| 上饶市| 景泰县| 嘉义市| 赤壁市| 门源| 天祝| 绥化市| 民乐县| 志丹县| 辽宁省| 安乡县| 娱乐| 萍乡市| 汕尾市| 宁夏| 辽宁省| 江山市| 上林县| 株洲市| 惠州市| 旬邑县| 三原县| 西充县| 吉安市| 河西区| 巴彦淖尔市| 鹤壁市|