Skip to content

Instantly share code, notes, and snippets.

@project237
Created October 21, 2022 11:31
Show Gist options
  • Save project237/f378be5a9a9f7c418a3896427f5e0c9b to your computer and use it in GitHub Desktop.
Save project237/f378be5a9a9f7c418a3896427f5e0c9b to your computer and use it in GitHub Desktop.
Change the requests module user agent to a specified user agent string.
import requests as req
NEW_AGENT = "USER_AGENT_HERE"
def change_user_agent(new_agent):
req.utils.default_user_agent = lambda: new_agent
def get_user_agent():
url = 'https://httpbin.org/user-agent'
response = req.get(url)
return response.request.headers
if __name__ == '__main__':
# change_user_agent(NEW_AGENT)
print(get_user_agent())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment