-
-
Save iambibhas/3229686 to your computer and use it in GitHub Desktop.
User Agent strings in more usable format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def uastring(platforms, format): | |
''' | |
This function is used to retrive User Agent String for various browsers/platforms from www.useragentstring.com. | |
Aim of this module is to simplify using the available data. | |
To find the value of the platform, visit http://useragentstring.com and get the part after /pages/ | |
The platform parameter should be a set of one or more platforms. Supported formats are csv, json and xml. | |
''' | |
import requests | |
import BeautifulSoup as bsoup | |
url = 'http://www.useragentstring.com/pages/' | |
# platforms = {'Blackberry', 'Browser%20for%20S60'} | |
browsers = [] | |
for platform in platforms: | |
try: | |
req = requests.get(url+platform+'/') | |
except requests.ConnectionError, e: | |
print e | |
if req.status_code == 200: | |
if len(req.text) > 0: | |
content = req.text | |
else: | |
print '0 size request received' | |
soup = bsoup.BeautifulSoup(content) | |
div = soup.find('div',{'id':'liste'}) | |
list = div.findAll('li') | |
for item in list: | |
browsers.append(''.join(item.findAll(text=True))) | |
else: | |
print 'HTTP Response code is {0}'.format(req.status_code) | |
data = _create_format(browsers,format) | |
return data | |
def _create_format(browsers, format): | |
if format == 'csv': | |
return ','.join(browsers) | |
elif format == 'json': | |
return '' | |
# To be completed | |
elif format == 'xml': | |
return '' | |
#To be completed | |
else: | |
return 'Unsupported format' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment