如何编写Python爬虫中设置请求头的代码?

2026-05-21 22:142阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计642个文字,预计阅读时间需要3分钟。

如何编写Python爬虫中设置请求头的代码?

pythonimport requests

url=http://www.targetweb.comheaders={ 'Accept': 'text/,application/x+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Cache-Control': 'max-age=0', 'Connection': 'keep-alive', 'Referer': 'http:'}

一、requests设置请求头:

importrequests url="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':'www.baidu.com/', 'User-Agent':'Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/53.0.2785.104Safari/537.36Core/1.53.4882.400QQBrowser/9.7.13059.400'} res=requests.get(url,headers=headers) #图片下载时要用到字节流,请求方式如下 #res=requests.get(url,stream=True,headers)

二、Selenium+Chrome请求头设置:

fromseleniumimportwebdriver options=webdriver.ChromeOptions() options.add_argument('lang=zh_CN.UTF-8')#设置中文 options.add_argument('user-agent="Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/53.0.2785.104Safari/537.36Core/1.53.4882.400QQBrowser/9.7.13059.400"')#设置头部 browser=webdriver.Chrome(chrome_options=options) url="www.targetweb.com" browser.get(url) browser.quit()

三、selenium+phantomjs请求头设置:

fromseleniumimportwebdriver fromselenium.webdriver.common.desired_capabilitiesimportDesiredCapabilities des_cap=dict(DesiredCapabilities.PHANTOMJS) des_cap["phantomjs.page.settings.userAgent"]=("Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/53.0.2785.104Safari/537.36Core/1.53.4882.400QQBrowser/9.7.13059.400") browser=webdriver.PhantomJS(desired_capabilities=des_cap) url="www.targetweb.com" browser.get(url) browser.quit()

四、爬虫框架scrapy设置请求头:

在settings.py文件中添加如下:

如何编写Python爬虫中设置请求头的代码?

DEFAULT_REQUEST_HEADERS={ 'accept':'image/webp,*/*;q=0.8', 'accept-language':'zh-CN,zh;q=0.8', 'referer':'www.baidu.com/', 'user-agent':'Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/53.0.2785.104Safari/537.36Core/1.53.4882.400QQBrowser/9.7.13059.400',}

五、Python异步Aiowww.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':'www.baidu.com/', 'User-Agent':'Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/53.0.2785.104Safari/537.36Core/1.53.4882.400QQBrowser/9.7.13059.400'} asyncwithaiohttp.ClientSession(headers=headers)assession: asyncwithsession.get(url)asresp: print(resp.status) print(awaitresp.text())

内容扩展:

1、为什么要设置headers?

在请求网页爬取的时候,输出的text信息中会出现抱歉,无法访问等字眼,这就是禁止爬取,需要通过反爬机制去解决这个问题。

headers是解决requests请求反爬的方法之一,相当于我们进去这个网页的服务器本身,假装自己本身在爬取数据。

对反爬虫网页,可以设置一些headers信息,模拟成浏览器取访问网站。

2、 headers在哪里找?

谷歌或者火狐浏览器,在网页面上点击:右键–>检查–>剩余按照图中显示操作,需要按Fn+F5刷新出网页来

有的浏览器是点击:右键->查看元素,刷新

以上就是python爬虫请求头设置代码的详细内容,更多关于python爬虫请求头如何设置的资料请关注易盾网络其它相关文章!

本文共计642个文字,预计阅读时间需要3分钟。

如何编写Python爬虫中设置请求头的代码?

pythonimport requests

url=http://www.targetweb.comheaders={ 'Accept': 'text/,application/x+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Cache-Control': 'max-age=0', 'Connection': 'keep-alive', 'Referer': 'http:'}

一、requests设置请求头:

importrequests url="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':'www.baidu.com/', 'User-Agent':'Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/53.0.2785.104Safari/537.36Core/1.53.4882.400QQBrowser/9.7.13059.400'} res=requests.get(url,headers=headers) #图片下载时要用到字节流,请求方式如下 #res=requests.get(url,stream=True,headers)

二、Selenium+Chrome请求头设置:

fromseleniumimportwebdriver options=webdriver.ChromeOptions() options.add_argument('lang=zh_CN.UTF-8')#设置中文 options.add_argument('user-agent="Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/53.0.2785.104Safari/537.36Core/1.53.4882.400QQBrowser/9.7.13059.400"')#设置头部 browser=webdriver.Chrome(chrome_options=options) url="www.targetweb.com" browser.get(url) browser.quit()

三、selenium+phantomjs请求头设置:

fromseleniumimportwebdriver fromselenium.webdriver.common.desired_capabilitiesimportDesiredCapabilities des_cap=dict(DesiredCapabilities.PHANTOMJS) des_cap["phantomjs.page.settings.userAgent"]=("Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/53.0.2785.104Safari/537.36Core/1.53.4882.400QQBrowser/9.7.13059.400") browser=webdriver.PhantomJS(desired_capabilities=des_cap) url="www.targetweb.com" browser.get(url) browser.quit()

四、爬虫框架scrapy设置请求头:

在settings.py文件中添加如下:

如何编写Python爬虫中设置请求头的代码?

DEFAULT_REQUEST_HEADERS={ 'accept':'image/webp,*/*;q=0.8', 'accept-language':'zh-CN,zh;q=0.8', 'referer':'www.baidu.com/', 'user-agent':'Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/53.0.2785.104Safari/537.36Core/1.53.4882.400QQBrowser/9.7.13059.400',}

五、Python异步Aiowww.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':'www.baidu.com/', 'User-Agent':'Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/53.0.2785.104Safari/537.36Core/1.53.4882.400QQBrowser/9.7.13059.400'} asyncwithaiohttp.ClientSession(headers=headers)assession: asyncwithsession.get(url)asresp: print(resp.status) print(awaitresp.text())

内容扩展:

1、为什么要设置headers?

在请求网页爬取的时候,输出的text信息中会出现抱歉,无法访问等字眼,这就是禁止爬取,需要通过反爬机制去解决这个问题。

headers是解决requests请求反爬的方法之一,相当于我们进去这个网页的服务器本身,假装自己本身在爬取数据。

对反爬虫网页,可以设置一些headers信息,模拟成浏览器取访问网站。

2、 headers在哪里找?

谷歌或者火狐浏览器,在网页面上点击:右键–>检查–>剩余按照图中显示操作,需要按Fn+F5刷新出网页来

有的浏览器是点击:右键->查看元素,刷新

以上就是python爬虫请求头设置代码的详细内容,更多关于python爬虫请求头如何设置的资料请关注易盾网络其它相关文章!