请求更新浏览器

时间:2017-11-11 14:07:04

标签: 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 href="http://www.chrome.com">Chrome</a> or <a href="http://www.firefox.com">Firefox</a>.

我如何避免这种情况,并获得真实的网页内容?

1 个答案:

答案 0 :(得分:2)

使用

将您的用户代理添加到请求的标头中
headers = {
    'User-Agent': 'YOUR USER AGENT',
}

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

您可以从许多网站获取用户代理,例如this

修改

如果上述解决方案对您不起作用,可能是因为您使用的是旧版requests,请尝试以下方法:

headers = requests.utils.default_headers()

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

response = requests.get(url, headers=headers)
相关问题