2

How do I find out the default browser's user agent using Python? Is there something returned with urllib, urllib2 or webbrowser that contains this information?

I'm using the code shown below, but the versions keep changing every time I update or switch browsers. Is there a way to make the Python code get the latest information from the browser? I'm not exactly sure why I need the user agent since I'm not using the browser. I think the url might decide to only allow certain browser versions. Right now it feels wrong to leave this transient information in a constant.

PALEMOON = 'Mozilla/5.0 (Windows NT 6.1; WOW64) KHTML/4.11 Gecko/20130308 Firefox/33.0 (PaleMoon/25.2)'
WATERFOX = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:40.0) Gecko/20100101 Firefox/51.1.0 Waterfox/51.1.0'

def getwebpage(url):
    """ Open a webpage 
    url --  the url to the webpage
    returns 
    page -- the source for the webpage
    """
    user_agent = WATERFOX
    headers = { 'User-Agent' : user_agent }
    req = urllib2.Request(url, None, headers)
    response = urllib2.urlopen(req)
    page = response.read()
    return page
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
wnderer
  • 21
  • 5
  • try **[this SO post](http://stackoverflow.com/questions/434597/open-document-with-default-application-in-python)**, It is possible to pass url argument in os.system() call :) – Piotr Kamoda Feb 23 '17 at 14:30
  • Are you posting or getting a request? you can use requests library and then use "requests.headers". – YOBA Feb 23 '17 at 14:58

0 Answers0