Scrapy'yi kullanan web sitelerinden pdf dosyalarını çekmekle görevlendirildim. Python için yeni değilim, ama Scrapy benim için çok yeni. Konsol ve birkaç ilkel örümcek deniyorum. Ben buldum ve bu kodu değiştirdik:Bir web sitesinden pdf dosyalarını bulmak ve indirmek için Scrapy'yi kullanma
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)
Ben
scrapy crawl mySpider
ile komut satırına bu kodu çalıştırmak ve geri hiçbir şey olsun. Bir terapi öğesi oluşturmadım çünkü dosyayı taramak ve indirmek istiyorum, meta veri yok. Bunun için herhangi bir yardım isterim.
günlükleri paylaşabilirim? – eLRuLL