python爬蟲添加請求頭代碼實(shí)例
這篇文章主要介紹了python爬蟲添加請求頭代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
request
import requests headers = { # 'Accept': 'application/json, text/javascript, */*; q=0.01', # 'Accept': '*/*', # 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-US;q=0.7', # 'Cache-Control': 'no-cache', # 'accept-encoding': 'gzip, deflate, br', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36', 'Referer': 'https://www.google.com/' } resp = requests.get('http://httpbin.org/get', headers=headers) print(resp.content)
urllib
import urllib, urllib2 def get_page_source(url): headers = {'Accept': '*/*', 'Accept-Language': 'en-US,en;q=0.8', 'Cache-Control': 'max-age=0', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36', 'Connection': 'keep-alive', 'Referer': 'http://www.baidu.com/' } req = urllib2.Request(url, None, headers) response = urllib2.urlopen(req) page_source = response.read() return page_source
phantomjs請求頁面
from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities def get_headers_driver(): desire = DesiredCapabilities.PHANTOMJS.copy() headers = {'Accept': '*/*', 'Accept-Language': 'en-US,en;q=0.8', 'Cache-Control': 'max-age=0', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36', 'Connection': 'keep-alive', 'Referer': 'http://www.baidu.com/' } for key, value in headers.iteritems(): desire['phantomjs.page.customHeaders.{}'.format(key)] = value driver = webdriver.PhantomJS(desired_capabilities=desire, service_args=['--load-images=yes'])#將yes改成no可以讓瀏覽器不加載圖片 return driver
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python實(shí)現(xiàn)列表中最大最小值輸出的示例
今天小編就為大家分享一篇python實(shí)現(xiàn)列表中最大最小值輸出的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-07-07python通過Matplotlib繪制常見的幾種圖形(推薦)
這篇文章主要介紹了使用matplotlib對(duì)幾種常見的圖形進(jìn)行繪制方法的相關(guān)資料,需要的朋友可以參考下2021-08-08Python中Django與Echarts的結(jié)合用法圖文詳解
ECharts是一個(gè)第三方控件,下面這篇文章主要給大家介紹了關(guān)于Python中Django與Echarts的結(jié)合用法,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-10-10python調(diào)用pymssql包操作SqlServer數(shù)據(jù)庫的實(shí)現(xiàn)
本文主要介紹了python調(diào)用pymssql包操作SqlServer數(shù)據(jù)庫的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06Python2.7版os.path.isdir中文路徑返回false的解決方法
這篇文章主要為大家詳細(xì)介紹了Python2.7版os.path.isdir中文路徑返回false的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06使用python3批量下載rbsp數(shù)據(jù)的示例代碼
這篇文章主要介紹了使用python3批量下載rbsp數(shù)據(jù)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12YOLOv5構(gòu)建安全帽檢測和識(shí)別系統(tǒng)使用詳解
這篇文章主要為大家介紹了YOLOv5構(gòu)建安全帽檢測和識(shí)別系統(tǒng)使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04