我一直負責使用Scrapy從網站上拉取PDF文件。我對Python並不陌生,但Scrapy對我來說是一個新手。我一直在試驗控制檯和一些基本的蜘蛛。我發現和修改這個代碼:使用Scrapy從網站上查找和下載PDF文件
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
,我得不到任何回報。我沒有創建scrapy項目,因爲我想抓取並下載文件,沒有元數據。我將不勝感激任何幫助。
你可以分享這些日誌? – eLRuLL