使用Scrapy从网站查找和下载pdf文件

使用Scrapy从网站查找和下载pdf文件,第1张

使用Scrapy从网站查找和下载pdf文件

我已经更新了你的代码,这实际上是可行的:

import urlparseimport scrapyfrom scrapy.http import Requestclass pwc_tax(scrapy.Spider):    name = "pwc_tax"    allowed_domains = ["www.pwc.com"]    start_urls = ["http://www.pwc.com/us/en/tax-services/publications/research-and-insights.html"]    def parse(self, response):        for href in response.css('div#all_results h3 a::attr(href)').extract(): yield Request(     url=response.urljoin(href),     callback=self.parse_article )    def parse_article(self, response):        for href in response.css('div.download_wrapper a[href$=".pdf"]::attr(href)').extract(): yield Request(     url=response.urljoin(href),     callback=self.save_pdf )    def save_pdf(self, response):        path = response.url.split('/')[-1]        self.logger.info('Saving PDF %s', path)        with open(path, 'wb') as f: f.write(response.body)


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/4926064.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-12
下一篇2022-11-12

发表评论

登录后才能评论

评论列表(0条)

    保存