"urllib.error.HTTPError:HTTP错误404:Not Found“Python

2024-10-03 21:26:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用urllib.request.open功能: “https://prenotaonline.esteri.it/login.aspx?cidsede=100001&returnUrl=//

我可以用我的普通浏览器访问这个网页urrlib.request.open文件函数返回HTTP错误404:

import urllib.request


page = urllib.request.urlopen("https://prenotaonline.esteri.it/login.aspx?cidsede=100001&returnUrl=//").read()
print(page)

我得到以下错误:

^{pr2}$

我使用的是python3.5.3


Tags: https功能request错误pageloginitopen
1条回答
网友
1楼 · 发布于 2024-10-03 21:26:27

这是你在制作蜘蛛/爬行机器人时偶然发现的第一件事。在

检测机器人程序的基本方法是请求头是否包含User-Agent报头。在

请尝试以下代码段:

import requests

headers = {'USER-AGENT': 'Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405'}

r = requests.get(URL, headers=headers)

print r.status_code  # should be 200 
print r.content  # should hold page content

相关问题 更多 >