ヘッダーのuser-agent
フィールドとして指定する必要があります。
これがHTTPヘッダーフィールドのリストです。おそらく、を含むリクエスト固有のフィールドに興味があるでしょうUser-Agent
。
リクエストv2.13以降を使用している場合
必要なことを行う最も簡単な方法は、辞書を作成し、次のようにヘッダーを直接指定することです。
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)
v2.12.x以前のリクエストを使用している場合
古いバージョンのrequests
古いデフォルトヘッダーは、デフォルトヘッダーを保持するために次の手順を実行してから、独自のヘッダーを追加します。
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)