指定浏览器的用户代理如下所示:
Mozilla - Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1
Chrome - Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.9 Safari/537.36
Opera - Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36 OPR/48.0.2685.52
Safari - Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27
browser_dict = {'Safari': "safari url here",'OPR':"opera url here",'Chrome':"chrome url here", 'Firefox':"firefox url here"}
你可以做的是用空格分割用户代理:
这是我正在编写的伪代码:
useragent = user-agent.split(' ') //useragent will be list
再次使用 '/' 拆分上述列表的最后一项和最后一项:
useragent-last = useragent[-1].split('/')
useragent-lastsecond= useragent[-2].split('/')
现在检查浏览器字典中是否存在 useragent-last 或 useragent-lastsecond
for key, value in browser_dict.items():
if useragent-last == key:
go to url browser_dict[key] //url
elif useragent-lastsecond == key:
go to url browser_dict[key] //url
else:
print (No known browser detected.)
这可能会对你有所帮助。