Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 请求更新浏览器_Python_Python 2.7 - Fatal编程技术网

Python 请求更新浏览器

Python 请求更新浏览器,python,python-2.7,Python,Python 2.7,有时,当我试图从一个网站上获取html代码时,使用此代码 import requests url = "https://sit2play.com" response = requests.get(url) print response.content 我得到了这样的回应 <h3 class="ielte9"> The browser you're using is not supported. Please use a different browser like <a hr

有时,当我试图从一个网站上获取html代码时,使用此代码

import requests
url = "https://sit2play.com"
response = requests.get(url)
print response.content
我得到了这样的回应

<h3 class="ielte9">
The browser you're using is not supported. Please use a different browser like <a href="http://www.chrome.com">Chrome</a> or <a href="http://www.firefox.com">Firefox</a>.

您正在使用的浏览器不受支持。请使用其他浏览器,如或。

如何避免这种情况,并获取真正的页面内容?

使用以下命令将您的用户代理添加到请求的标题中:

headers = {
    'User-Agent': 'YOUR USER AGENT',
}

response = requests.get(url, headers=headers)
您可以从许多网站获得您的用户代理,如

编辑 如果上述解决方案不适用于您,这可能是因为您使用的是旧版本的
请求
,请尝试以下方法:

headers = requests.utils.default_headers()

headers.update({
         'User-Agent': 'YOUR USER AGENT',
    })

response = requests.get(url, headers=headers)

使用将用户代理添加到请求的标头

headers = {
    'User-Agent': 'YOUR USER AGENT',
}

response = requests.get(url, headers=headers)
您可以从许多网站获得您的用户代理,如

编辑 如果上述解决方案不适用于您,这可能是因为您使用的是旧版本的
请求
,请尝试以下方法:

headers = requests.utils.default_headers()

headers.update({
         'User-Agent': 'YOUR USER AGENT',
    })

response = requests.get(url, headers=headers)

你需要一根更好的绳子。该网站认为您使用的是Internet Explorer版本,您需要一个更好的字符串。该网站认为您使用的是Internet Explorer版本,请尝试以下用户代理:
Mozilla/5.0(Macintosh;Intel Mac OS X 10_13_0)AppleWebKit/537.36(KHTML,如Gecko)Chrome/62.0.3202.62 Safari/537.36
。工作正常吗?仍然会出现相同的错误,这是我当前使用的代码。请尝试以下用户代理:
Mozilla/5.0(Macintosh;英特尔Mac OS X 10_13_0)AppleWebKit/537.36(KHTML,类似Gecko)Chrome/62.0.3202.62 Safari/537.36
。它工作吗?仍然得到相同的错误,这是我目前使用的代码让我们。