栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Python中的请求库发送“用户代理”

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Python中的请求库发送“用户代理”

user-agent
应指定为在报头中的字段。

这是HTTP标头字段的列表,你可能会对特定于请求的字段感兴趣,其中包括User-Agent。

如果你使用的是v2.13及更高版本的请求

执行所需操作的最简单方法是创建字典并直接指定标题,如下所示:

import requestsurl = 'SOME URL'headers = {    'User-Agent': 'My User Agent 1.0',    'From': 'youremail@domain.com'  # This is another valid field}response = requests.get(url, headers=headers)

如果你使用的是v2.12.x及更高版本的请求

较旧版本的

requests
默认标头,因此你需要执行以下操作来保留默认标头,然后向其添加自己的标头。

import requestsurl = 'SOME URL'# Get a copy of the default headers that requests would useheaders = requests.utils.default_headers()# Update the headers with your custom ones# You don't have to worry about case-sensitivity with# the dictionary keys, because default_headers uses a custom# CaseInsensitiveDict implementation within requests' source pre.headers.update(    {        'User-Agent': 'My User Agent 1.0',    })response = requests.get(url, headers=headers)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/633362.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号