Python urllib.request.urlopen不适用于特定网站

Python urllib.request.urlopen不适用于特定网站,python,python-3.x,Python,Python 3.x,我使用urllib.request.request作为memidex.com页面的url,但是urllib.request.urlopen(url)行无法打开url url = urllib.request.Request("http://www.memidex.com/" + term) my_request = urllib.request.urlopen(url) info = BeautifulSoup(my_request, "html.parser") 我曾尝试在不同的网站上使用相

我使用urllib.request.request作为memidex.com页面的url,但是urllib.request.urlopen(url)行无法打开url

url = urllib.request.Request("http://www.memidex.com/" + term)
my_request = urllib.request.urlopen(url)
info = BeautifulSoup(my_request, "html.parser")

我曾尝试在不同的网站上使用相同的代码,但它对该网站有效,因此我不知道为什么它对memidex.com无效。

您需要在url请求中添加标题以克服错误。顺便问一下“HTTP错误403:禁止”您的错误正确吗? 希望下面的代码对您有所帮助

import urllib.request

user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
url = "http://www.memidex.com/"
headers={'User-Agent':user_agent,} 
request=urllib.request.Request(url,None,headers)
response = urllib.request.urlopen(request)
data = response.read()
print(data)

你说不能给我请求是什么意思?我的请求是我为urllib.request.urlopen()行命名的变量。Urllib.request.urlopen()无法打开我在前一行中提供的url。该程序只是挂起/冻结?我已将该程序设置为在网站没有我要查找的内容时打印“未找到”,因此,由于它无法打开url,所以它只打印该url。请求没有您需要的内容是有区别的,一个完全失败的请求,不是吗?