Set User-Agent on Python Requests


Tags: python
Reading time: less than 1 minute

Question

I need to make GET requests with Python using the requests library, but my User Agent string shows up as python-requests/2.27.1. How can I change it to look more like a browser.

Answer

import requests

user_agent = "Firefox whatever"
headers = {"User-Agent": user_agent}

requests.get("http://example.com/", headers = headers)

Citation

If you find this work useful, please cite it as:
@article{yaltirakliqnapythonrequestssetuseragent,
  title   = "Set User-Agent on Python Requests",
  author  = "Yaltirakli, Gokberk",
  journal = "gkbrk.com",
  year    = "2024",
  url     = "https://www.gkbrk.com/qna/python-requests-set-user-agent/"
}
Not using BibTeX? Click here for more citation styles.
IEEE Citation
Gokberk Yaltirakli, "Set User-Agent on Python Requests", November, 2024. [Online]. Available: https://www.gkbrk.com/qna/python-requests-set-user-agent/. [Accessed Nov. 12, 2024].
APA Style
Yaltirakli, G. (2024, November 12). Set User-Agent on Python Requests. https://www.gkbrk.com/qna/python-requests-set-user-agent/
Bluebook Style
Gokberk Yaltirakli, Set User-Agent on Python Requests, GKBRK.COM (Nov. 12, 2024), https://www.gkbrk.com/qna/python-requests-set-user-agent/

Comments

© 2024 Gokberk Yaltirakli