鱼C论坛

 找回密码
 立即注册
查看: 1652|回复: 4

[已解决]Scrapy框架中parse()函数中两个包含URL的文件没有被创建

[复制链接]
发表于 2020-6-9 21:06:53 | 显示全部楼层 |阅读模式

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

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

x
spider
  1. import scrapy
  2. class QuotesSpider(scrapy.Spider):
  3.     name = 'quotes'                     #蜘蛛的名字
  4.     def start_requests(self):           #包含了spider在启动时进行爬取的URL列表,该函数必须返回Request对象或者生成器

  5.         allowed_domains = ['quotes.toscrape.com']    #蜘蛛的范围
  6.         urls = [
  7.             'http://www.quotes.toscrape.com/page/1/'
  8.             'http://www.quotes.toscrape.com/page/2/'
  9.         ]                                   #从哪里开始爬
  10.         for url in urls:
  11.             yield scrapy.Request(url=url,callback=self.parse)

  12.     def parse(self,response):           #该函数负责解析返回的数据,提取数据以及生成需要进一步处理的URL的Request对象
  13.         page = response.url.split('/')[-2]
  14.         filename = 'quotes-%s.html'%page
  15.         with open(filename,"wb") as f:
  16.             f.write(response.body)
  17.         self.log('Save file %s'%filename)
复制代码

爬:
  1. (base) C:\Users\admin\Desktop\tutorial>scrapy crawl quotes
  2. 2020-06-09 19:57:23 [scrapy.utils.log] INFO: Scrapy 2.1.0 started (bot: tutorial)
  3. 2020-06-09 19:57:23 [scrapy.utils.log] INFO: Versions: lxml 4.5.1.0, libxml2 2.9.10, cssselect 1.1.0, parsel 1.6.0, w3lib 1.22.0, Twisted 20.3.0, Python 3.7.6 (default, Jan  8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)], pyOpenSSL 19.1.0 (OpenSSL 1.1.1g  21 Apr 2020), cryptography 2.8, Platform Windows-10-10.0.18362-SP0
  4. 2020-06-09 19:57:23 [scrapy.utils.log] DEBUG: Using reactor: twisted.internet.selectreactor.SelectReactor
  5. 2020-06-09 19:57:23 [scrapy.crawler] INFO: Overridden settings:
  6. {'BOT_NAME': 'tutorial',
  7. 'NEWSPIDER_MODULE': 'tutorial.spiders',
  8. 'ROBOTSTXT_OBEY': True,
  9. 'SPIDER_MODULES': ['tutorial.spiders']}
  10. 2020-06-09 19:57:23 [scrapy.extensions.telnet] INFO: Telnet Password: b8b0c838b5723910
  11. 2020-06-09 19:57:23 [scrapy.middleware] INFO: Enabled extensions:
  12. ['scrapy.extensions.corestats.CoreStats',
  13. 'scrapy.extensions.telnet.TelnetConsole',
  14. 'scrapy.extensions.logstats.LogStats']
  15. 2020-06-09 19:57:24 [scrapy.middleware] INFO: Enabled downloader middlewares:
  16. ['scrapy.downloadermiddlewares.robotstxt.RobotsTxtMiddleware',
  17. 'scrapy.downloadermiddlewares.httpauth.HttpAuthMiddleware',
  18. 'scrapy.downloadermiddlewares.downloadtimeout.DownloadTimeoutMiddleware',
  19. 'scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware',
  20. 'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware',
  21. 'scrapy.downloadermiddlewares.retry.RetryMiddleware',
  22. 'scrapy.downloadermiddlewares.redirect.MetaRefreshMiddleware',
  23. 'scrapy.downloadermiddlewares.httpcompression.HttpCompressionMiddleware',
  24. 'scrapy.downloadermiddlewares.redirect.RedirectMiddleware',
  25. 'scrapy.downloadermiddlewares.cookies.CookiesMiddleware',
  26. 'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware',
  27. 'scrapy.downloadermiddlewares.stats.DownloaderStats']
  28. 2020-06-09 19:57:24 [scrapy.middleware] INFO: Enabled spider middlewares:
  29. ['scrapy.spidermiddlewares.httperror.HttpErrorMiddleware',
  30. 'scrapy.spidermiddlewares.offsite.OffsiteMiddleware',
  31. 'scrapy.spidermiddlewares.referer.RefererMiddleware',
  32. 'scrapy.spidermiddlewares.urllength.UrlLengthMiddleware',
  33. 'scrapy.spidermiddlewares.depth.DepthMiddleware']
  34. 2020-06-09 19:57:24 [scrapy.middleware] INFO: Enabled item pipelines:
  35. []
  36. 2020-06-09 19:57:24 [scrapy.core.engine] INFO: Spider opened
  37. 2020-06-09 19:57:24 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
  38. 2020-06-09 19:57:24 [scrapy.extensions.telnet] INFO: Telnet console listening on 127.0.0.1:6023
  39. 2020-06-09 19:57:28 [scrapy.downloadermiddlewares.retry] DEBUG: Retrying <GET http://www.quotes.toscrape.com/robots.txt> (failed 1 times): [<twisted.python.failure.Failure twisted.internet.error.ConnectionDone: Connection was closed cleanly.>]
  40. 2020-06-09 19:57:30 [scrapy.downloadermiddlewares.retry] DEBUG: Retrying <GET http://www.quotes.toscrape.com/robots.txt> (failed 2 times): [<twisted.python.failure.Failure twisted.internet.error.ConnectionDone: Connection was closed cleanly.>]
  41. 2020-06-09 19:57:32 [scrapy.downloadermiddlewares.retry] ERROR: Gave up retrying <GET http://www.quotes.toscrape.com/robots.txt> (failed 3 times): [<twisted.python.failure.Failure twisted.internet.error.ConnectionDone: Connection was closed cleanly.>]
  42. 2020-06-09 19:57:32 [scrapy.downloadermiddlewares.robotstxt] ERROR: Error downloading <GET http://www.quotes.toscrape.com/robots.txt>: [<twisted.python.failure.Failure twisted.internet.error.ConnectionDone: Connection was closed cleanly.>]
  43. Traceback (most recent call last):
  44.   File "C:\Users\admin\miniconda3\lib\site-packages\scrapy\core\downloader\middleware.py", line 44, in process_request
  45.     return (yield download_func(request=request, spider=spider))
  46. twisted.web._newclient.ResponseNeverReceived: [<twisted.python.failure.Failure twisted.internet.error.ConnectionDone: Connection was closed cleanly.>]
  47. 2020-06-09 19:57:34 [scrapy.downloadermiddlewares.retry] DEBUG: Retrying <GET http://www.quotes.toscrape.com/page/1/http://www.quotes.toscrape.com/page/2/> (failed 1 times): [<twisted.python.failure.Failure twisted.internet.error.ConnectionDone: Connection was closed cleanly.>]
  48. 2020-06-09 19:57:37 [scrapy.downloadermiddlewares.retry] DEBUG: Retrying <GET http://www.quotes.toscrape.com/page/1/http://www.quotes.toscrape.com/page/2/> (failed 2 times): [<twisted.python.failure.Failure twisted.internet.error.ConnectionDone: Connection was closed cleanly.>]
  49. 2020-06-09 19:57:39 [scrapy.downloadermiddlewares.retry] ERROR: Gave up retrying <GET http://www.quotes.toscrape.com/page/1/http://www.quotes.toscrape.com/page/2/> (failed 3 times): [<twisted.python.failure.Failure twisted.internet.error.ConnectionDone: Connection was closed cleanly.>]
  50. 2020-06-09 19:57:44 [scrapy.core.scraper] ERROR: Error downloading <GET http://www.quotes.toscrape.com/page/1/http://www.quotes.toscrape.com/page/2/>
  51. Traceback (most recent call last):
  52.   File "C:\Users\admin\miniconda3\lib\site-packages\scrapy\core\downloader\middleware.py", line 44, in process_request
  53.     return (yield download_func(request=request, spider=spider))
  54. twisted.web._newclient.ResponseNeverReceived: [<twisted.python.failure.Failure twisted.internet.error.ConnectionDone: Connection was closed cleanly.>]
  55. 2020-06-09 19:57:44 [scrapy.core.engine] INFO: Closing spider (finished)
  56. 2020-06-09 19:57:44 [scrapy.statscollectors] INFO: Dumping Scrapy stats:
  57. {'downloader/exception_count': 6,
  58. 'downloader/exception_type_count/twisted.web._newclient.ResponseNeverReceived': 6,
  59. 'downloader/request_bytes': 1503,
  60. 'downloader/request_count': 6,
  61. 'downloader/request_method_count/GET': 6,
  62. 'elapsed_time_seconds': 20.184052,
  63. 'finish_reason': 'finished',
  64. 'finish_time': datetime.datetime(2020, 6, 9, 11, 57, 44, 311541),
  65. 'log_count/DEBUG': 4,
  66. 'log_count/ERROR': 4,
  67. 'log_count/INFO': 10,
  68. 'retry/count': 4,
  69. 'retry/max_reached': 2,
  70. 'retry/reason_count/twisted.web._newclient.ResponseNeverReceived': 4,
  71. "robotstxt/exception_count/<class 'twisted.web._newclient.ResponseNeverReceived'>": 1,
  72. 'robotstxt/request_count': 1,
  73. 'scheduler/dequeued': 3,
  74. 'scheduler/dequeued/memory': 3,
  75. 'scheduler/enqueued': 3,
  76. 'scheduler/enqueued/memory': 3,
  77. 'start_time': datetime.datetime(2020, 6, 9, 11, 57, 24, 127489)}
  78. 2020-06-09 19:57:44 [scrapy.core.engine] INFO: Spider closed (finished)
复制代码

最后,没找到quotes-1.html 和quotes-2.html
最佳答案
2020-6-10 00:36:54
,有问题的网址,你还去请求,看教程,不看网址吧
O0I1]@BVFM{[14EWO~FCA.png
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-6-9 21:29:24 | 显示全部楼层
换了个网址可以了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-10 00:36:54 | 显示全部楼层    本楼为最佳答案   
,有问题的网址,你还去请求,看教程,不看网址吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-6-10 09:21:16 | 显示全部楼层
Stubborn 发表于 2020-6-10 00:36
,有问题的网址,你还去请求,看教程,不看网址吧

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-27 16:21:00 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-22 16:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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