简体   繁体   English

如何在我的 python 代码中使我的“用户代理标头”始终保持最新?

[英]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.

相关问题 如何在Python 3中检索带有User-Agent标头的文件? - How can I retrieve files with User-Agent headers in Python 3? Python 次请求 - 403 次禁止 - 尽管设置了“User-Agent”标头 - Python requests - 403 forbidden - despite setting `User-Agent` headers 如何在Python MozEmbed中设置User-Agent? - How to set User-Agent in Python MozEmbed? 如何在 Python 中访问 User-Agent 以获取我的浏览器详细信息,我尝试的每种方法都返回 Python-usrlib 2.7 - How to access User-Agent in Python to get my browser details , every method i try returns Python-usrlib 2.7 用于 python 的 pdfkit 中的用户代理 - User-Agent in pdfkit for python Urlretrieve 和用户代理? - Python - Urlretrieve and User-Agent? - Python 如何解决意外的 Tor 行为? 用户代理和 HTTPS 协议显示我的实际 ip - How to solve unexpected Tor behaviour? User-agent and HTTPS protocol show my actual ip 如何使用 Selenium 和 Python 为用户代理设置自定义名称 - How to set a custom name for the user-agent using Selenium and Python 如何在用 Python 编写的 Selenium 脚本中成功更改 User-Agent? - How to successfully change User-Agent in Selenium script written in Python? 如何在 python-webkit 中设置用户代理 - How to set user-agent in python-webkit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM