Python Requests User Agent


Datacenter proxies

  • unlimited bandwidth
  • Price starting from $0.08/IP
  • Locations: EU, America, Asia



Sending “User-agent” using Requests library in Python – Stack …

I want to send a value for “User-agent” while requesting a webpage using Python Requests. I am not sure is if it is okay to send this as a part of the header, as in the code below:
debug = {‘verbose’:}
user_agent = {‘User-agent’: ‘Mozilla/5. 0’}
response = (url, headers = user_agent, config=debug)
The debug information isn’t showing the headers being sent during the request.
Is it acceptable to send this information in the header? If not, how can I send it?
asked May 15 ’12 at 17:48
The user-agent should be specified as a field in the header.
Here is a list of HTTP header fields, and you’d probably be interested in request-specific fields, which includes User-Agent.
If you’re using requests v2. 13 and newer
The simplest way to do what you want is to create a dictionary and specify your headers directly, like so:
import requests
url = ‘SOME URL’
headers = {
‘User-Agent’: ‘My User Agent 1. 0’,
‘From’: ” # This is another valid field}
response = (url, headers=headers)
If you’re using requests v2. 12. x and older
Older versions of requests clobbered default headers, so you’d want to do the following to preserve default headers and then add your own to them.
# Get a copy of the default headers that requests would use
headers = ()
# Update the headers with your custom ones
# You don’t have to worry about case-sensitivity with
# the dictionary keys, because default_headers uses a custom
# CaseInsensitiveDict implementation within requests’ source code.
‘User-Agent’: ‘My User Agent 1. 0’, })
answered May 15 ’12 at 17:58
逆さま逆さま69. 5k14 gold badges155 silver badges171 bronze badges
It’s more convenient to use a session, this way you don’t have to remember to set headers each time:
session = ssion()
({‘User-Agent’: ‘Custom user agent’})
By default, session also manages cookies for you. In case you want to disable that, see this question.
answered Jun 14 ’17 at 4:16
useruser17. 6k6 gold badges93 silver badges92 bronze badges
simply you can do it like below:
url = (“URL”, headers={“FUser”:”your username”, “FPass”:”your password”, “user-agent”: “your custom text for the user agent “})
Henry Ecker26. 3k10 gold badges19 silver badges37 bronze badges
answered Sep 12 at 7:00
A Python library that provides an easy way to identify devices like mobile ...


  • No logs
  • Kill Switch
  • 6 devices
  • Monthly price: $4.92


A Python library that provides an easy way to identify devices like mobile …

user_agents is a Python library that provides an easy way to identify/detect devices like mobile phones, tablets and their capabilities by parsing (browser/HTTP) user agent strings. The goal is to reliably detect whether:
User agent is a mobile, tablet or PC based device
User agent has touch capabilities (has touch screen)
user_agents relies on the excellent ua-parser to do the actual parsing of the raw user agent string.
user-agents is hosted on PyPI and can be installed as such:
pip install pyyaml ua-parser user-agents
Alternatively, you can also get the latest source code from Github and install it manually.
Various basic information that can help you identify visitors can be accessed browser, device and os attributes. For example:
from user_agents import parse
# iPhone’s user agent string
ua_string = ‘Mozilla/5. 0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534. 46 (KHTML, like Gecko) Version/5. 1 Mobile/9B179 Safari/7534. 48. 3′
user_agent = parse(ua_string)
# Accessing user agent’s browser attributes
owser # returns Browser(family=u’Mobile Safari’, version=(5, 1), version_string=’5. 1′)
# returns ‘Mobile Safari’
rsion # returns (5, 1)
rsion_string # returns ‘5. 1′
# Accessing user agent’s operating system properties
# returns OperatingSystem(family=u’iOS’, version=(5, 1), version_string=’5. 1′)
# returns ‘iOS’
# returns (5, 1)
# returns ‘5. 1′
# Accessing user agent’s device properties
# returns Device(family=u’iPhone’, brand=u’Apple’, model=u’iPhone’)
# returns ‘iPhone’
# returns ‘Apple’
# Viewing a pretty string version
str(user_agent) # returns “iPhone / iOS 5. 1 / Mobile Safari 5. 1”
user_agents also expose a few other more “sophisticated” attributes that are derived from one or more basic attributes defined above. As for now, these attributes should correctly identify popular platforms/devices, pull requests to support smaller ones are always welcome.
Currently these attributes are supported:
is_mobile: whether user agent is identified as a mobile phone (iPhone, Android phones, Blackberry, Windows Phone devices etc)
is_tablet: whether user agent is identified as a tablet device (iPad, Kindle Fire, Nexus 7 etc)
is_pc: whether user agent is identified to be running a traditional “desktop” OS (Windows, OS X, Linux)
is_touch_capable: whether user agent has touch capabilities
is_bot: whether user agent is a search engine crawler/spider
For example:
# Let’s start from an old, non touch Blackberry device
ua_string = ‘BlackBerry9700/5. 0. 862 Profile/MIDP-2. 1 Configuration/CLDC-1. 1 VendorID/331 UNTRUSTED/1. 0 3gpp-gba’
_mobile # returns True
_tablet # returns False
_touch_capable # returns False
_pc # returns False
_bot # returns False
str(user_agent) # returns “BlackBerry 9700 / BlackBerry OS 5 / BlackBerry 9700”
# Now a Samsung Galaxy S3
ua_string = ‘Mozilla/5. 0 (Linux; U; Android 4. 4; en-gb; GT-I9300 Build/IMM76D) AppleWebKit/534. 30 (KHTML, like Gecko) Version/4. 0 Mobile Safari/534. 30’
_touch_capable # returns True
str(user_agent) # returns “Samsung GT-I9300 / Android 4. 4 / Android 4. 4”
# iPad’s user agent string
ua_string = ‘Mozilla/5. 0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531. 21. 10 (KHTML, like Gecko) Version/4. 4 Mobile/7B314 Safari/531. 10’
_mobile # returns False
_tablet # returns True
str(user_agent) # returns “iPad / iOS 3. 2 / Mobile Safari 4. 4”
# Kindle Fire’s user agent string
ua_string = ‘Mozilla/5. 0 (Macintosh; U; Intel Mac OS X 10_6_3; en-us; Silk/1. 1. 0-80) AppleWebKit/533. 16 (KHTML, like Gecko) Version/5. 0 Safari/533. 16 Silk-Accelerated=true’
str(user_agent) # returns “Kindle / Android / Amazon Silk 1. 0-80”
# Touch capable Windows 8 device
ua_string = ‘Mozilla/5. 0 (compatible; MSIE 10. 0; Windows NT 6. 2; Trident/6. 0; Touch)’
_pc # returns True
str(user_agent) # returns “PC / Windows 8 / IE 10”
Running Tests
python -m unittest discover
Version 2. 2. 0 (2020-08-23)
ua-parser >= 0. 10. 0 is required. Thanks @jnozsc!
Added get_device(), get_os() and get_browser() instance methods to UserAgent. Thanks @rodrigondec!
Version 2. 1 (2020-02-08)
python-user-agents now require ua-parser>=0. 9. Thanks @jnozsc!
Properly detect Chrome Mobile browser families. Thanks @jnozsc!
Version 2. 0 (2019-04-07)
python-user-agents now require ua-parser>=0. 8. Thanks @IMDagger!
Version 1. 1
Fixes packaging issue
Version 1. 0
Adds compatibility with ua-parser 0. 4. 0
Access to more device information in and
Version 0. 3. 2
Better mobile detection
Better PC detection
Version 0. 1
_mobile returns True when mobile spider is detected
Version 0. 0
Added str/unicode methods for convenience of pretty string
Version 0. 0
Fixed errors when running against newer versions if ua-parser
Support for Python 3
Version 0. 1
Added is_bot property
Symbian OS devices are now detected as a mobile device
Version 0. 1
Initial release
Developed by the cool guys at Stamps.
HTTP headers | User-Agent - GeeksforGeeks

HTTP headers | User-Agent – GeeksforGeeks

The HTTP headers User-Agent is a request header that allows a characteristic string that allows network protocol peers to identify the Operating System and Browser of the web-server. Your browser sends the user agent to every website you connect to. There is no conventional way of writing a user agent string as different browsers use different formats and many web browsers load a lot of information onto their user your browser is connected to a website, a User-Agent field is included in the HTTP header. The data of the header field varies from browser to browser. This information is used to serve different websites to different web browsers and different operating / orUser-Agent: Mozilla/ () ()
Directives There are three directives in HTTP headers oduct: This holds the product oduct-version: This holds the product version of the used mment: This holds the sub-product information of the used productYou can also check your User-agent with the help of (Windows NT 10. 0; Win64; x64) AppleWebKit/537. 36 (KHTML, like Gecko) Chrome/77. 0. 3865. 90 Safari/537. 36The following conclusions can be drawn with the help of user-agent header:The user agent application is Mozilla version 5. operating system is NT version 10. 0 (and is running on a Windows(64-bit) Machine) engine responsible for displaying content on this device is AppleWebKit version 537. 36 (KHTML, an open-source layout engine, is present too) client is Chrome version client is based on Safari version 537. 36. Examples:Mozilla:Mozilla/5. 0 (Windows NT 6. 1; Win64; x64; rv:47. 0) Gecko/20100101 Firefox/47. 3
Mozilla/5. 0 (Macintosh; Intel Mac OS X x. y; rv:42. 0) Gecko/20100101 Firefox/43. 4Chrome:Mozilla/5. 0 (X11; Linux x86_64) AppleWebKit/537. 36Safari:Mozilla/5. 0 (iPhone; CPU iPhone OS 11_3_1 like Mac OS X) AppleWebKit/603. 1. 30 (KHTML, like Gecko)
Version/10. 0 Mobile/14E304 Safari/602. 1Supported Browsers: The browsers compatible with HTTP headers User-Agent are listed below:Google ChromeInternet ExplorerFirefoxSafariOpera

Frequently Asked Questions about python requests user agent

What is Python requests user agent?

A user agent is typically a client that sends requests on behalf of a user. Making a request with a user agent fakes a browser visit to a particular webpage.

How do I set user agent in request?

Use requests. get() to set the user agent Define a dictionary with a key “User-Agent” set to the desired user agent. Call requests. get(url, headers=headers) with url as the target URL and headers as the dictionary of the previous step to set the user agent in a request.

How do I find the user agent in Python?

user_agents is a Python library that provides an easy way to identify/detect devices like mobile phones, tablets and their capabilities by parsing (browser/HTTP) user agent strings. The goal is to reliably detect whether: User agent is a mobile, tablet or PC based device.Sep 7, 2021

About the author


If you 're a SEO / IM geek like us then you'll love our updates and our website. Follow us for the latest news in the world of web automation tools & proxy servers!

By proxyreview

Recent Posts

Useful Tools