Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

2
0

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
Linked from these articles

Comments

No comments

Let's comment your feelings that are more than good

Qiita Conference 2024 Autumn will be held!: 11/14(Thu) - 11/15(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

Takahiro Anno, Masaki Fujimoto, Yukihiro Matsumoto(Matz)

View event details

Being held Article posting campaign

2
0

Login to continue?

Login or Sign up with social account

Login or Sign up with your email address