Skip to content

Instantly share code, notes, and snippets.

@ShaikeA
Created January 16, 2019 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShaikeA/03e5eb66dc65a49aa196dc9fb602ffeb to your computer and use it in GitHub Desktop.
Save ShaikeA/03e5eb66dc65a49aa196dc9fb602ffeb to your computer and use it in GitHub Desktop.
# Create a dict of accept headers for each user-agent.
accepts = {"Firefox": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Safari, Chrome": "application/xml,application/xhtml+xml,text/html;q=0.9, text/plain;q=0.8,image/png,*/*;q=0.5"}
# Get a random user-agent. We used Chrome and Firefox user agents.
# Getting a user agent using the fake_useragent package
ua = UserAgent()
if random.random() > 0.5:
random_user_agent = ua.chrome
else:
random_user_agent = ua.firefox
valid_accept = accepts['Firefox'] if random_user_agent.find('Firefox') > 0 else accepts['Safari, Chrome']
headers = {"User-Agent": random_user_agent,
"Accept": valid_accept}
# See below for the complete function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment