我的任务是使用Scrapy从网站上提取pdf文件。我对Python并不陌生,但Scrapy对我来说是一个非常新的东西。我一直在试验控制台和一些基本的蜘蛛。我发现并修改了以下代码:
import urlparse
import scrapy
from scrapy.http import Request
class 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):
base_url = "http://www.pwc.com/us/en/tax-services/publications/research-and-insights.html"
for a in response.xpath('//a[@href]/@href'):
link = a.extract()
if link.endswith('.pdf'):
link = urlparse.urljoin(base_url, link)
yield Request(link, callback=self.save_pdf)
def save_pdf(self, response):
path = response.url.split('/')[-1]
with open(path, 'wb') as f:
f.write(response.body)
我在命令行运行此代码
scrapy crawl mySpider
我什么也得不到。我没有创建一个零碎的项目,因为我想抓取和下载文件,没有元数据。我希望能在这方面得到帮助。
蜘蛛逻辑似乎不正确。
我快速浏览了一下你的网站,似乎有几种类型的页面:
因此,正确的逻辑是:首先获得1页,然后获得2页,然后我们可以下载3页。
然而,你的蜘蛛试图从1页中直接提取到3页的链接。
编辑:
我已经更新了你的代码,以下是一些实际可行的方法:
相关问题 更多 >
编程相关推荐