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

Do7li

我使用 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 请求添加标头以克服错误。BTW 'HTTP Error 403: Forbidden' 是你的错误吗?希望下面的代码对你有帮助。

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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

带有验证的urllib.request.urlopen(url)

如何处理来自urllib.request.urlopen()的响应编码

我需要使用urllib2.Request / urlopen处理哪些错误/异常?

urllib.request.urlopen返回字节,但是我无法对其进行解码

HTTP错误401:未经授权使用urllib.request.urlopen

模拟urllib.request.urlopen的read函数返回MagicMock签名

kaggle内核:urllib.request.urlopen不适合任何URL

HTTPError问题:urllib.request.urlopen python请求错误

Python urllib.request.urlopen:AttributeError:“ bytes”对象没有属性“ data”

为什么urllib.request.urlopen在《华尔街日报》的网站上给我404?

为什么urlopen不适用于某些网站?

Python请求lib工作,urllib.request.urlopen POST不工作

python:urllib.request.urlopen不起作用

Python 3:urllib.request.urlopen和Progressbar

为什么在这种情况下urllib.request.urlopen会阻塞?

urllib,urllib2,urllib.request.open,AttributeError:“模块”对象没有属性“ urlopen”

替换urllib.request.urlopen(url,ca *)的开启器

urllib.request.urlopen(url)如何通过IP地址使用此功能?

urllib.request.urlopen无法获取堆栈溢出选举的主要页面

urllib.request.urlopen 的行为很奇怪。第二天不返回数据。为什么?

Python 3.6.2 url.request.urlopen() urllib.error.HTTPError:HTTP 错误 403:禁止

urllib.request.urlopen(url) 在 python 3 中不起作用

urllib.request 的 urlopen 无法在 python 3.7 中打开页面

如何使用 urllib.request.urlopen 从多个 url 获取所有图像 url

将 urllib.request.urlopen 移动到请求

python3中来自urllib.request的urlopen的奇怪行为

urllib.error.HTTPError: HTTP Error 404: Not Found when using request.urlopen()

python: urllib.request.urlopen() HTTP 错误 308 永久重定向

python3 安全问题,os.system('wget...') 与 urllib.request.urlopen(...)