.

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    = "2025",
  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", January, 2025. [Online]. Available: https://www.gkbrk.com/qna/python-requests-set-user-agent/. [Accessed Jan. 20, 2025].
APA Style
Yaltirakli, G. (2025, January 20). 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 (Jan. 20, 2025), https://www.gkbrk.com/qna/python-requests-set-user-agent/

Comments

© 2025 Gokberk Yaltirakli