Skip to main content
 首页 » 编程设计

python之urlopen 不适用于特定的 url

2024年09月07日8insus

大家好,我是编程的新手,所以请原谅我的任何新手错误。 我试图在 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>

请您参考如下方法:

urllib.error.URLError: <urlopen error [Errno 11004] getaddrinfo failed> 

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

您能否从运行 Python 代码的同一台机器上运行的 Web 浏览器访问该站点?