无法在python中打开一些网站

时间:2017-09-09 09:53:10

标签: python python-requests urllib

我可以获得除requests.get(web).content之外的任何网站www.bundukhansweets.pk

import requests
web = "http://umararfeen.com"
req = requests.get(web).status_code
print req

结果:200

但是,当我尝试在开始时打开一个www.的网站时,它会显示403(禁止)。我甚至试过"http://www."+website"www."+website

import requests
web = "http://bundukhansweets.pk"
print requests.get(web).status_code

结果:403

我甚至尝试urllib2但失败了。

1 个答案:

答案 0 :(得分:1)

您缺少标题

变化

import requests
web = "http://bundukhansweets.pk"
hdr = {
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Encoding': 'gzip,deflate,sdch',
    'Accept-Language': 'en-US,en;q=0.8',
    'Connection': 'keep-alive'
}
print requests.get(web, headers=hdr).status_code

这将起作用