革命年 发表于 2020-7-26 18:49:04

爬虫出错了

这是错误
Traceback (most recent call last):
File "F:\lib\urllib\request.py", line 1350, in do_open
    h.request(req.get_method(), req.selector, req.data, headers,
File "F:\lib\http\client.py", line 1255, in request
    self._send_request(method, url, body, headers, encode_chunked)
File "F:\lib\http\client.py", line 1301, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
File "F:\lib\http\client.py", line 1250, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
File "F:\lib\http\client.py", line 1010, in _send_output
    self.send(msg)
File "F:\lib\http\client.py", line 950, in send
    self.connect()
File "F:\lib\http\client.py", line 921, in connect
    self.sock = self._create_connection(
File "F:\lib\socket.py", line 787, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "F:\lib\socket.py", line 918, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "F:/p55.py", line 3, in <module>
    p=u.urlopen(r)
File "F:\lib\urllib\request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
File "F:\lib\urllib\request.py", line 525, in open
    response = self._open(req, data)
File "F:\lib\urllib\request.py", line 542, in _open
    result = self._call_chain(self.handle_open, protocol, protocol +
File "F:\lib\urllib\request.py", line 502, in _call_chain
    result = func(*args)
File "F:\lib\urllib\request.py", line 1379, in http_open
    return self.do_open(http.client.HTTPConnection, req)
File "F:\lib\urllib\request.py", line 1353, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error getaddrinfo failed>
这是源代码
import urllib.request as u
r=u.Request('http://plaeckitten.com/g/500/600')
p=u.urlopen(r)
c=p.read()
with open("cat.jpg",'wb') as f:
    f.write(c)
哪里出错了?

zltzlt 发表于 2020-7-27 08:41:15

网址拼错了,http://plaeckitten.com/g/500/600,正确代码:

import urllib.request as u

r = u.Request('http://placekitten.com/g/500/600')
p = u.urlopen(r)
c = p.read()
with open("cat.jpg", 'wb') as f:
    f.write(c)
页: [1]
查看完整版本: 爬虫出错了