user agent python requests code example
Example 1: python requests set user agent
import requests
url = 'SOME URL'
headers = {
'User-Agent': 'My User Agent 1.0',
'From': '[email protected]'
}
response = requests.get(url, headers=headers)
Example 2: user agent for python
headers = {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"}
r = requests.get('http://httpbin.org/headers',headers=headers)
pprint(r.json())
Example 3: why mentioning user agent in request library
import requests
url = 'SOME URL'
headers = requests.utils.default_headers()
headers.update(
{
'User-Agent': 'My User Agent 1.0',
}
)
response = requests.get(url, headers=headers)