鱼C论坛

 找回密码
 立即注册
查看: 930|回复: 2

[已解决]爬虫练习错误求助

[复制链接]
发表于 2019-7-25 16:28:53 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 fendow 于 2019-7-25 16:58 编辑
  1. import urllib.request
  2. import os

  3. def url_open(url):
  4.     req = urllib.request.Request(url)
  5.     req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36')
  6.     response = urllib.request.urlopen(req)
  7.     html = response.read()
  8.     return html
  9.    
  10. def get_page(url):
  11.     html = url_open(url).decode('utf-8')
  12.     a = html.find('current-comment-page') + 23
  13.     b = html.find(']',a)  
  14.     return html[a:b]
  15.      
  16. def find_imgs(url):
  17.     html = url_open(url).decode('utf-8')
  18.     img_addrs = []
  19.     a = html.find('img src=')
  20.    
  21.     while a != -1:
  22.         b = html.find('.jpg',a,a+255 )
  23.         if b != -1:
  24.             img_addrs.append(html[a+9:b+4])
  25.         else:
  26.             b = a+9
  27.         a = html.find('img src=',b)
  28.    
  29. def save_imgs(folder,img_addrs):
  30.     for each in img_addre:
  31.         filename = each.split('/')[-1]
  32.         with open(filename,'wb') as f:
  33.             img = url_open(each)
  34.             f.write(img)

  35. def download_mm(folder='ooxx',pages=18):
  36.     os.mkdir(folder)
  37.     os.chdir(folder)
  38.    
  39.     url = 'http://jandan.net/ooxx/'
  40.     page_num = int(get_page(url))
  41.    
  42.     for i in range(pages):
  43.         page_num -= i
  44.         page_url = url + 'page-' + str(page_num) + '#comments'
  45.         img_addrs = find_imgs(page_url)
  46.         save_imgs(folder,img_addrs)
  47.         
  48. if __name__== '__main__':
  49.     download_mm()
复制代码


最后报错
  1. TimeoutError                              Traceback (most recent call last)
  2. C:\ProgramData\Anaconda3\lib\urllib\request.py in do_open(self, http_class, req, **http_conn_args)
  3.    1316                 h.request(req.get_method(), req.selector, req.data, headers,
  4. -> 1317                           encode_chunked=req.has_header('Transfer-encoding'))
  5.    1318             except OSError as err: # timeout error

  6. C:\ProgramData\Anaconda3\lib\http\client.py in request(self, method, url, body, headers, encode_chunked)
  7.    1228         """Send a complete request to the server."""
  8. -> 1229         self._send_request(method, url, body, headers, encode_chunked)
  9.    1230

  10. C:\ProgramData\Anaconda3\lib\http\client.py in _send_request(self, method, url, body, headers, encode_chunked)
  11.    1274             body = _encode(body, 'body')
  12. -> 1275         self.endheaders(body, encode_chunked=encode_chunked)
  13.    1276

  14. C:\ProgramData\Anaconda3\lib\http\client.py in endheaders(self, message_body, encode_chunked)
  15.    1223             raise CannotSendHeader()
  16. -> 1224         self._send_output(message_body, encode_chunked=encode_chunked)
  17.    1225

  18. C:\ProgramData\Anaconda3\lib\http\client.py in _send_output(self, message_body, encode_chunked)
  19.    1015         del self._buffer[:]
  20. -> 1016         self.send(msg)
  21.    1017

  22. C:\ProgramData\Anaconda3\lib\http\client.py in send(self, data)
  23.     955             if self.auto_open:
  24. --> 956                 self.connect()
  25.     957             else:

  26. C:\ProgramData\Anaconda3\lib\http\client.py in connect(self)
  27.     927         self.sock = self._create_connection(
  28. --> 928             (self.host,self.port), self.timeout, self.source_address)
  29.     929         self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

  30. C:\ProgramData\Anaconda3\lib\socket.py in create_connection(address, timeout, source_address)
  31.     726     if err is not None:
  32. --> 727         raise err
  33.     728     else:

  34. C:\ProgramData\Anaconda3\lib\socket.py in create_connection(address, timeout, source_address)
  35.     715                 sock.bind(source_address)
  36. --> 716             sock.connect(sa)
  37.     717             # Break explicitly a reference cycle

  38. TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。

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

  40. URLError                                  Traceback (most recent call last)
  41. <ipython-input-20-bcc7d98d73ed> in <module>
  42.      45
  43.      46 if __name__== '__main__':
  44. ---> 47     download_mm()

  45. <ipython-input-20-bcc7d98d73ed> in download_mm(folder, pages)
  46.      36
  47.      37     url = 'http://jandan.net/ooxx/'
  48. ---> 38     page_num = int(get_page(url))
  49.      39
  50.      40     for i in range(pages):

  51. <ipython-input-20-bcc7d98d73ed> in get_page(url)
  52.       9     print(url)
  53.      10 def get_page(url):
  54. ---> 11     html = url_open(url).decode('utf-8')
  55.      12     a = html.find('current-comment-page') + 23
  56.      13     b = html.find(']',a)

  57. <ipython-input-20-bcc7d98d73ed> in url_open(url)
  58.       5     req = urllib.request.Request(url)
  59.       6     req.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36')
  60. ----> 7     response = urllib.request.urlopen(url)
  61.       8     html = response.read()
  62.       9     print(url)

  63. C:\ProgramData\Anaconda3\lib\urllib\request.py in urlopen(url, data, timeout, cafile, capath, cadefault, context)
  64.     220     else:
  65.     221         opener = _opener
  66. --> 222     return opener.open(url, data, timeout)
  67.     223
  68.     224 def install_opener(opener):

  69. C:\ProgramData\Anaconda3\lib\urllib\request.py in open(self, fullurl, data, timeout)
  70.     523             req = meth(req)
  71.     524
  72. --> 525         response = self._open(req, data)
  73.     526
  74.     527         # post-process response

  75. C:\ProgramData\Anaconda3\lib\urllib\request.py in _open(self, req, data)
  76.     541         protocol = req.type
  77.     542         result = self._call_chain(self.handle_open, protocol, protocol +
  78. --> 543                                   '_open', req)
  79.     544         if result:
  80.     545             return result

  81. C:\ProgramData\Anaconda3\lib\urllib\request.py in _call_chain(self, chain, kind, meth_name, *args)
  82.     501         for handler in handlers:
  83.     502             func = getattr(handler, meth_name)
  84. --> 503             result = func(*args)
  85.     504             if result is not None:
  86.     505                 return result

  87. C:\ProgramData\Anaconda3\lib\urllib\request.py in http_open(self, req)
  88.    1343
  89.    1344     def http_open(self, req):
  90. -> 1345         return self.do_open(http.client.HTTPConnection, req)
  91.    1346
  92.    1347     http_request = AbstractHTTPHandler.do_request_

  93. C:\ProgramData\Anaconda3\lib\urllib\request.py in do_open(self, http_class, req, **http_conn_args)
  94.    1317                           encode_chunked=req.has_header('Transfer-encoding'))
  95.    1318             except OSError as err: # timeout error
  96. -> 1319                 raise URLError(err)
  97.    1320             r = h.getresponse()
  98.    1321         except:

  99. URLError: <urlopen error [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。>
复制代码

请问大佬们,这个是什么问题  是 notebook 不支持吗。。
最佳答案
2019-7-25 16:53:07
煎蛋网,在小甲鱼老师的带领下,加上了不弱的反爬功能,换一个网站吧,这个对身体也不好。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-7-25 16:53:07 | 显示全部楼层    本楼为最佳答案   
煎蛋网,在小甲鱼老师的带领下,加上了不弱的反爬功能,换一个网站吧,这个对身体也不好。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-7-25 17:09:48 | 显示全部楼层
新手·ing 发表于 2019-7-25 16:53
煎蛋网,在小甲鱼老师的带领下,加上了不弱的反爬功能,换一个网站吧,这个对身体也不好。

原来是被爬网站问题哈。。谢谢了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-10-21 03:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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