[英]How to keep my 'User-Agent headers' always up to date in my python codes?
In my research I find several ways to create a fake User-Agent
and also hide my User-Agent
when making requests.在我的研究中,我发现了几种创建假
User-Agent
的方法,并且在发出请求时也隐藏了我的User-Agent
。
But what I'm actually looking for is how do I keep my User-Agent
always up to date without needing to get the value manually.但我真正在寻找的是如何让我的
User-Agent
始终保持最新状态,而无需手动获取值。
Manually way is search on Google for example:手动方式是在 Google 上搜索,例如:
Is there a correct method for this?有正确的方法吗?
For example, currently the User-Agent
I use to make requests is this:例如,目前我用来发出请求的
User-Agent
是这样的:
import requests
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
}
url = f'https://XXXXXXXXXXXXXXXXX.com'
response = requests.get(url, headers=headers).json()
But the version of Chrome that is appearing is already a much older version of the one I currently use.但是出现的 Chrome 版本已经是我目前使用的版本的旧版本。 I'd like to know if there's a way to keep this up to date without having to manually modify it every time!
我想知道是否有办法保持最新状态,而不必每次都手动修改!
you can use fake user agent:你可以使用假用户代理:
https://github.com/hellysmile/fake-useragent
it has a lot of options and keep user agent updated.它有很多选项并保持用户代理更新。
from fake_useragent import UserAgent
ua = UserAgent()
ua.ie
# Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US);
ua.msie
# Mozilla/5.0 (compatible; MSIE 10.0; Macintosh; Intel Mac OS X 10_7_3; Trident/6.0)'
ua['Internet Explorer']
# Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB7.4; InfoPath.2; SV1; .NET CLR 3.3.69573; WOW64; en-US)
ua.opera
# Opera/9.80 (X11; Linux i686; U; ru) Presto/2.8.131 Version/11.11
ua.chrome
# Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.2 (KHTML, like Gecko) Chrome/22.0.1216.0 Safari/537.2'
ua.google
# Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/537.13 (KHTML, like Gecko) Chrome/24.0.1290.1 Safari/537.13
ua['google chrome']
# Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11
ua.firefox
# Mozilla/5.0 (Windows NT 6.2; Win64; x64; rv:16.0.1) Gecko/20121011 Firefox/16.0.1
ua.ff
# Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:15.0) Gecko/20100101 Firefox/15.0.1
ua.safari
# Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25
# and the best one, random via real world browser usage statistic
ua.random
You can get your browsers user agent in javascript on the console with navigator.userAgent
.您可以使用
navigator.userAgent
在控制台上的 javascript 中获取您的浏览器用户代理。 How can you automatically have this value stored into your python scripts??如何自动将此值存储到 python 脚本中? I'm not really aware of a way todo that.
我真的不知道有什么方法可以做到这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.