python爬取网页报错提示状态码404,可是在浏览器里可以打开网页
import urllib.request
url = "http://t.cn/Rq0PezN"
try:
response = urllib.request.urlopen(url)
source = response.geturl()
print(source)
except Exception as e:
request = urllib.request.urlopen(urllib.request.Request(url, headers = {'User-Agent':'Mozilla/8.0 (compatible; MSIE 8.0; Windows 7)'}))
print (request.url)
代码如上 运行后报错:urllib.error.HTTPError: HTTP Error 404: Not Found。
代码是通过短连接获取原链接,可以用浏览器打开这个链接但是却提示状态码404的错误。不知道这个原理是啥,怎样可以解决这个问题呢?