第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

Python實現(xiàn)爬蟲設(shè)置代理IP和偽裝成瀏覽器的方法分享

標(biāo)簽:
Python


Python实现爬虫设置代理IP和伪装成浏览器的方法分享

 1.python爬虫浏览器伪装

123456789101112#导入urllib.request模块import urllib.request#设置请求头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")#创建一个openeropener=urllib.request.build_opener()#将headers添加到opener中opener.addheaders=[headers]#将opener安装为全局urllib.request.install_opener(opener)#用urlopen打开网页data=urllib.request.urlopen(url).read().decode('utf-8','ignore')

2.设置代理

12345678910#定义代理ipproxy_addr="122.241.72.191:808"#设置代理proxy=urllib.request.ProxyHandle({'http':proxy_addr})#创建一个openeropener=urllib.request.build_opener(proxy,urllib.request.HTTPHandle)#将opener安装为全局urllib.request.install_opener(opener)#用urlopen打开网页data=urllib.request.urlopen(url).read().decode('utf-8','ignore')

3.同时设置用代理和模拟浏览器访问

1234567891011121314#定义代理ipproxy_addr="122.241.72.191:808"#创建一个请求req=urllib.request.Request(url)#添加headersreq.add_header("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)#设置代理proxy=urllib.request.ProxyHandle("http":proxy_addr)#创建一个openeropener=urllib.request.build_opener(proxy,urllib.request.HTTPHandle)#将opener安装为全局urllib.request.install_opener(opener)#用urlopen打开网页data=urllib.request.urlopen(req).read().decode('utf-8','ignore')

4.在请求头中添加多个信息

1234567import urllib.requestpage_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请求参数

1234567891011121314151617181920212223import urllib.requestimport urllib.parse#设置post参数page_data=urllib.parse.urlencode([                  ('pn',page_num),                  ('kd',keywords)                  ])#设置headerspage_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':'     'Accept':'application/json, text/javascript, */*; q=0.01',     'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',     'Referer':'

6.利用phantomjs模拟浏览器请求

123456789#1.下载phantomjs安装到本地,并设置环境变量from selenium import webdriverbs=webdriver.PhantomJS()#打开urlbs.get(url)#获取网页源码url_data=bs.page_source#将浏览到的网页保存为图片bs.get_screenshot_as_file(filename)

7.phantomjs设置user-agent和cookie

123456789101112131415161718from selenium import webdriverfrom selenium.webdriver.common.desired_capabilities import DesiredCapabilitiesdcap = 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)#删除cookiebs.delete_all_cookies()#设置cookie#cookie格式:在浏览器cookie中查看,一个cookie需要包含以下参数,domain、name、value、pathcookie={  'domain':'.www.baidu.com', #注意前面有.  'name':'xxxx',  'value':'xxxx',  'path':'xxxx'  }#向phantomjs中添加cookiebs.add_cookie(cookie)

8.利用web_driver工具

123456#1.下载web_driver工具(如chromdriver.exe)及对应的浏览器#2.将chromdriver.exe放到某个目录,如c:\chromdriver.exefrom selenium import webdriverdriver=webdriver.Chrome(executable_path="C:\chromdriver.exe")#打开urldriver.get(url)


點擊查看更多內(nèi)容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評論
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學(xué)

大額優(yōu)惠券免費領(lǐng)

立即參與 放棄機會
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報

0/150
提交
取消