发送“用户代理"在 Python 中使用请求库 [英] Sending "User-agent" using Requests library in Python

查看:55
本文介绍了发送“用户代理"在 Python 中使用请求库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在使用 Python 请求请求网页时为 "User-agent" 发送一个值.我不确定是否可以将其作为标头的一部分发送,如下面的代码所示:

debug = {'verbose': sys.stderr}user_agent = {'用户代理':'Mozilla/5.0'}response = requests.get(url, headers = user_agent, config=debug)

调试信息未显示请求期间发送的标头.

在标题中发送此信息是否可以接受?如果没有,我该如何发送?

解决方案

user-agent 应指定为标题中的字段.

这是一个HTTP标头字段列表,您可能对请求特定字段,其中包括User-Agent.

如果您使用的是 requests v2.13 及更高版本

做你想做的最简单的方法是创建一个字典并直接指定你的标题,如下所示:

导入请求url = '一些网址'标题 = {'用户代理': '我的用户代理 1.0','From': 'youremail@domain.com' # 这是另一个有效字段}response = requests.get(url, headers=headers)

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

旧版本的 requests 破坏了默认标头,因此您需要执行以下操作以保留默认标头,然后将您自己的标头添加到其中.

导入请求url = '一些网址'# 获取请求将使用的默认标头的副本headers = requests.utils.default_headers()# 用你的自定义标题更新标题# 你不必担心区分大小写# 字典键,因为 default_headers 使用自定义# CaseInsensitiveDict 在请求的源代码中实现.标题.更新({'用户代理': '我的用户代理 1.0',})response = requests.get(url, headers=headers)

I want to send a value for "User-agent" while requesting a webpage using Python Requests. I am not sure is if it is okay to send this as a part of the header, as in the code below:

debug = {'verbose': sys.stderr}
user_agent = {'User-agent': 'Mozilla/5.0'}
response  = requests.get(url, headers = user_agent, config=debug)

The debug information isn't showing the headers being sent during the request.

Is it acceptable to send this information in the header? If not, how can I send it?

解决方案

The user-agent should be specified as a field in the header.

Here is a list of HTTP header fields, and you'd probably be interested in request-specific fields, which includes User-Agent.

If you're using requests v2.13 and newer

The simplest way to do what you want is to create a dictionary and specify your headers directly, like so:

import requests

url = '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)

If you're using requests v2.12.x and older

Older versions of requests clobbered default headers, so you'd want to do the following to preserve default headers and then add your own to them.

import requests

url = 'SOME URL'

# Get a copy of the default headers that requests would use
headers = 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 code.
headers.update(
    {
        'User-Agent': 'My User Agent 1.0',
    }
)

response = requests.get(url, headers=headers)

这篇关于发送“用户代理"在 Python 中使用请求库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆