2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

pythonのrequestsでGETメソッドを送るときのheaderを設定する

Posted at

はじめに

requestsを使用してGETメソッドを送るときのheaderを設定の仕方です。
備忘録として記事にします。

結論

headesという引数がget関数にあるため、それを使用します。

サンプルコード

.py
import requests

def main():
    # 内容は一例
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100',
        'Accept-Language': 'ja,en;q=0.9,en-GB;q=0.8,en-US;q=0.7'
    }
    response = requests.get('https://www.google.com/', headers=headers, timeout=60)
    print(response.request.headers)

if __name__ == '__main__':
    main()

結果

{'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Accept-Language': 'ja,en;q=0.9,en-GB;q=0.8,en-US;q=0.7'}

以上です。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?