【问题标题】:urlopen not working for specific urlurlopen 不适用于特定的 url
【发布时间】:2018-09-15 05:39:31
【问题描述】:

大家好,我对编程很陌生,所以请原谅我任何菜鸟错误。 我正在尝试在 python 3.6 中使用 urllib 来查找网页上的字符串,而我发现 here 的解决方案对于我测试过的大多数网页都非常有效,例如:

from urllib.request import urlopen
>>>string = 'google'
>>>url = 'https://google.com/'
>>>webp=urlopen(url).read()
>>>isfound = webp.find(string.encode('utf-8'))
>>>print(isfound)
186

但是当我尝试使用 URL https://chan.sankakucomplex.com/ (NSFW) 时,我得到了大量的错误,我不知道为什么。 我已经尝试将我的 DNS 更改为 8.8.8.8,但这并没有改变任何东西。

错误: Traceback (most recent call last): File "C:\Users\-\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 1318, in do_open encode_chunked=req.has_header('Transfer-encoding')) File "C:\Users\-\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 1239, in request self._send_request(method, url, body, headers, encode_chunked) File "C:\Users\-\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 1285, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "C:\Users\-\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 1234, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "C:\Users\-\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 1026, in _send_output self.send(msg) File "C:\Users\-\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 964, in send self.connect() File "C:\Users\-\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 1392, in connect super().connect() File "C:\Users\-\AppData\Local\Programs\Python\Python36\lib\http\client.py", line 936, in connect (self.host,self.port), self.timeout, self.source_address) File "C:\Users\-\AppData\Local\Programs\Python\Python36\lib\socket.py", line 704, in create_connection for res in getaddrinfo(host, port, 0, SOCK_STREAM): File "C:\Users\-\AppData\Local\Programs\Python\Python36\lib\socket.py", line 743, in getaddrinfo for res in _socket.getaddrinfo(host, port, family, type, proto, flags): socket.gaierror: [Errno 11004] getaddrinfo failed During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:/Users/-/Desktop/python/searcher/stackex.py", line 5, in <module> webp=urlopen(url).read() File "C:\Users\-\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 223, in urlopen return opener.open(url, data, timeout) File "C:\Users\-\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 526, in open response = self._open(req, data) File "C:\Users\-\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 544, in _open '_open', req) File "C:\Users\-\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 504, in _call_chain result = func(*args) File "C:\Users\-\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 1361, in https_open context=self._context, check_hostname=self._check_hostname) File "C:\Users\-\AppData\Local\Programs\Python\Python36\lib\urllib\request.py", line 1320, in do_open raise URLError(err) urllib.error.URLError: <urlopen error [Errno 11004] getaddrinfo failed>

【问题讨论】:

  • 您所在的国家或地区可能会阻止此内容吗?
  • 您的代码在我的机器上运行(使用您的网站和字符串“sankaku”。Similar issue.
  • 谢谢!我忘记了我需要使用代理才能访问该站点。知道如何让 urllib 使用我的 SOCKS5 代理吗?这个dae.me/blog/1959/… 似乎不适合我。

标签: python python-3.x urllib urlopen


【解决方案1】:
urllib.error.URLError: <urlopen error [Errno 11004] getaddrinfo failed>

这告诉我您的 DNS 服务器拒绝将文本地址 (chan.sankakucomplex.com) 转换为 IP 地址。这不是 Python 的问题。

您可以从运行 Python 代码的同一台机器上运行的网络浏览器访问该站点吗?

【讨论】:

  • 谢谢!我忘记了我需要使用代理才能访问该站点。知道如何让 urllib 使用我的 SOCKS5 代理吗?这个dae.me/blog/1959/… 似乎不适合我。
  • 不确定 urllib。但是如果你开始使用请求,你的生活会轻松很多 (docs.python-requests.org/en/master)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-27
  • 2020-08-17
相关资源
最近更新 更多