Python爬虫请求头设置全攻略:Requests、Selenium、Scrapy与Aiohttp

洛溪
洛溪
发布时间:2026-10-14 | 6人浏览过
原创

本文详细讲解Python爬虫中请求头(Headers)的设置方法,涵盖Requests、Selenium(Chrome/PhantomJS)、Scrapy框架及Aiohttp异步库。通过模拟浏览器头部信息,有效绕过网站反爬限制。内容包含具体代码示例及获取Headers的实操步骤,帮助开发者解决访问被拒问题。

Python爬虫请求头设置全攻略:Requests、Selenium、Scrapy与Aiohttp

一、使用Requests库设置请求头


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)

#图片下载时要用到字节流,请求方式如下

#res=requests.get(url,stream=True,headers)

二、Selenium配合Chrome浏览器设置请求头


from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('lang=zh_CN.UTF-8')# 设置中文
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"')# 设置头部
browser = webdriver.Chrome(chrome_options=options)
url="http://www.targetweb.com"
browser.get(url)
browser.quit()

三、Selenium配合PhantomJS设置请求头


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()

四、Scrapy爬虫框架设置请求头

在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设置请求头


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())

内容扩展:请求头设置原理与获取方法

内容扩展:

1、为什么要设置Headers?

在爬取网页时,若返回的文本中包含“抱歉,无法访问”等提示,通常意味着触发了网站的反爬机制。此时需要通过设置正确的请求头来绕过限制。

Headers是解决Requests请求被反爬拦截的关键手段。通过配置Headers,可以模拟真实用户的浏览器行为,使服务器误认为请求来自正常的浏览器访问。

对于设有反爬虫策略的网页,必须设置相应的Headers信息,以模拟浏览器访问网站,从而成功获取数据。

2、如何获取Headers?

在Chrome或Firefox浏览器中,右键点击网页空白处,选择“检查”(或按F12),在开发者工具中找到Network选项卡。刷新页面(Fn+F5)以加载资源,即可在请求详情中看到Headers信息。

部分浏览器操作路径为:右键点击网页 -> 查看元素 -> 刷新页面,随后在开发者工具中查找相关请求头。

以上即为Python爬虫请求头设置的完整指南,更多相关技术细节可参考本站其他文章。

本站声明

本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。

Python 相关产品和应用

全部下载

专题列表