鱼C论坛

 找回密码
 立即注册
查看: 1791|回复: 1

[已解决]爬虫出错了

[复制链接]
发表于 2020-7-26 18:49:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这是错误
  1. Traceback (most recent call last):
  2.   File "F:\lib\urllib\request.py", line 1350, in do_open
  3.     h.request(req.get_method(), req.selector, req.data, headers,
  4.   File "F:\lib\http\client.py", line 1255, in request
  5.     self._send_request(method, url, body, headers, encode_chunked)
  6.   File "F:\lib\http\client.py", line 1301, in _send_request
  7.     self.endheaders(body, encode_chunked=encode_chunked)
  8.   File "F:\lib\http\client.py", line 1250, in endheaders
  9.     self._send_output(message_body, encode_chunked=encode_chunked)
  10.   File "F:\lib\http\client.py", line 1010, in _send_output
  11.     self.send(msg)
  12.   File "F:\lib\http\client.py", line 950, in send
  13.     self.connect()
  14.   File "F:\lib\http\client.py", line 921, in connect
  15.     self.sock = self._create_connection(
  16.   File "F:\lib\socket.py", line 787, in create_connection
  17.     for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  18.   File "F:\lib\socket.py", line 918, in getaddrinfo
  19.     for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
  20. socket.gaierror: [Errno 11004] getaddrinfo failed

  21. During handling of the above exception, another exception occurred:

  22. Traceback (most recent call last):
  23.   File "F:/p55.py", line 3, in <module>
  24.     p=u.urlopen(r)
  25.   File "F:\lib\urllib\request.py", line 222, in urlopen
  26.     return opener.open(url, data, timeout)
  27.   File "F:\lib\urllib\request.py", line 525, in open
  28.     response = self._open(req, data)
  29.   File "F:\lib\urllib\request.py", line 542, in _open
  30.     result = self._call_chain(self.handle_open, protocol, protocol +
  31.   File "F:\lib\urllib\request.py", line 502, in _call_chain
  32.     result = func(*args)
  33.   File "F:\lib\urllib\request.py", line 1379, in http_open
  34.     return self.do_open(http.client.HTTPConnection, req)
  35.   File "F:\lib\urllib\request.py", line 1353, in do_open
  36.     raise URLError(err)
  37. urllib.error.URLError: <urlopen error [Errno 11004] getaddrinfo failed>
复制代码

这是源代码
  1. import urllib.request as u
  2. r=u.Request('http://plaeckitten.com/g/500/600')
  3. p=u.urlopen(r)
  4. c=p.read()
  5. with open("cat.jpg",'wb') as f:
  6.     f.write(c)
复制代码

哪里出错了?
最佳答案
2020-7-27 08:41:15
网址拼错了,http://plaeckitten.com/g/500/600,正确代码:

  1. import urllib.request as u

  2. r = u.Request('http://placekitten.com/g/500/600')
  3. p = u.urlopen(r)
  4. c = p.read()
  5. with open("cat.jpg", 'wb') as f:
  6.     f.write(c)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-7-27 08:41:15 | 显示全部楼层    本楼为最佳答案   
网址拼错了,http://plaeckitten.com/g/500/600,正确代码:

  1. import urllib.request as u

  2. r = u.Request('http://placekitten.com/g/500/600')
  3. p = u.urlopen(r)
  4. c = p.read()
  5. with open("cat.jpg", 'wb') as f:
  6.     f.write(c)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-29 06:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表