利用Python编写一个简单的浏览器检查工具
网站:https://www.whatismybrowser.com/detect/what-is-my-user-agent/, 可以检测浏览器的版本,本代码利用requests模块,发起请求到该网站检查浏览器的信息
1 import requests 2 from lxml import etree 3 import termcolor 4 5 class UserAgentTest: 6 def __init__(self, user_agent) -> None: 7 self.url = 'https://www.whatismybrowser.com/detect/what-is-my-user-agent/' 8 self.user_agent = user_agent 9 10 def run(self): 11 headers = { 12 'User-Agent': self.user_agent 13 } 14 try: 15 response = requests.get(url=self.url, headers=headers).text 16 # print(response) 17 html = etree.HTML(response) 18 result = html.xpath('//div[@class="detected_result"]//text()')[1] 19 20 print("Your Browser is: \n", termcolor.colored(result, 'blue')) 21 except Exception as e: 22 print(e) 23 24 25 if __name__ == "__main__": 26 user_agent = input("Enter user agent: ") 27 useragenttest = UserAgentTest(user_agent=user_agent) 28 useragenttest.run()
STRIVE FOR PROGRESS,NOT FOR PERFECTION