technical_difficulty
technical_difficulty

Reputation: 477

Add header with custom User-Agent for every HTTP request

Is it possible to make python use a custom header with a specific User-Agent string for every HTTP connection when using urllib? In order that the following persists throughout the program and is added to every Request(), urllib.request.urlretrieve() and every other function that establishes an HTTP connection:

.add_header('User-Agent', 'Mozilla/5.0')

Or is it possible to change python's default User-Agent string from Python-urllib/3.5 to Mozilla/5.0 somewhere in its config files?

Upvotes: 1

Views: 1442

Answers (1)

Craig Blaszczyk
Craig Blaszczyk

Reputation: 972

You can update the headers by using a requests.Session

import requests
session = requests.Session()
session.headers['User-Agent'] = 'my user agent'
response = session.get('http://google.com')

Upvotes: 1

Related Questions