不懂python爬蟲(chóng)請(qǐng)求頭的設(shè)置方法?其實(shí)想解決這個(gè)問(wèn)題也不難,下面讓小編帶著大家一起學(xué)習(xí)怎么去解決,希望大家閱讀完這篇文章后大所收獲。
在吳川等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專(zhuān)注、極致的服務(wù)理念,為客戶(hù)提供成都網(wǎng)站制作、做網(wǎng)站、外貿(mào)營(yíng)銷(xiāo)網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作按需網(wǎng)站開(kāi)發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),高端網(wǎng)站設(shè)計(jì),營(yíng)銷(xiāo)型網(wǎng)站建設(shè),成都外貿(mào)網(wǎng)站制作,吳川網(wǎng)站建設(shè)費(fèi)用合理。
一、requests設(shè)置請(qǐng)求頭:
import requests url="http://www.targetweb.com" headers={ 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Cache-Control':'max-age=0', 'Connection':'keep-alive', 'Referer':'http://www.baidu.com/', 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.4882.400 QQBrowser/9.7.13059.400'} res=requests.get(url,headers=headers) #圖片下載時(shí)要用到字節(jié)流,請(qǐng)求方式如下 #res=requests.get(url,stream=True,headers)
二、Selenium+Chrome請(qǐng)求頭設(shè)置:
from selenium import webdriver options = webdriver.ChromeOptions() options.add_argument('lang=zh_CN.UTF-8')# 設(shè)置中文 options.add_argument('user-agent="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.4882.400 QQBrowser/9.7.13059.400"')# 設(shè)置頭部 browser = webdriver.Chrome(chrome_options=options) url="http://www.targetweb.com" browser.get(url) browser.quit()
三、selenium+phantomjs請(qǐng)求頭設(shè)置:
from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities des_cap = dict(DesiredCapabilities.PHANTOMJS) des_cap["phantomjs.page.settings.userAgent"] = ("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.4882.400 QQBrowser/9.7.13059.400") browser = webdriver.PhantomJS(desired_capabilities=des_cap) url="http://www.targetweb.com" browser.get(url) browser.quit()
四、爬蟲(chóng)框架scrapy設(shè)置請(qǐng)求頭:
在settings.py文件中添加如下:
DEFAULT_REQUEST_HEADERS = { 'accept': 'image/webp,*/*;q=0.8', 'accept-language': 'zh-CN,zh;q=0.8', 'referer': 'https://www.baidu.com/', 'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.4882.400 QQBrowser/9.7.13059.400',}
五、Python異步Aiohttp請(qǐng)求頭設(shè)置:
import aiohttp url="http://www.targetweb.com" headers={ 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Cache-Control':'max-age=0', 'Connection':'keep-alive', 'Referer':'http://www.baidu.com/', 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.4882.400 QQBrowser/9.7.13059.400'} asyncwithaiohttp.ClientSession(headers=headers)assession: asyncwithsession.get(url)asresp: print(resp.status) print(awaitresp.text())
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享python爬蟲(chóng)請(qǐng)求頭的設(shè)置方法內(nèi)容對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,遇到問(wèn)題就找創(chuàng)新互聯(lián),詳細(xì)的解決方法等著你來(lái)學(xué)習(xí)!