代码如下:
-- coding: utf-8 --
import scrapy
class LinearSpider(scrapy.Spider): name = "linear" allowed_domains = ["ocw.mit.edu"] start_urls = ['https://ocw.mit.edu/courses/mathematics/18-06sc-linear-algebra-fall-2011/resource-index/']
def parse(self, response):
page_hrefs = response.xpath("*//tr//td/a/@href").re(".*sum.pdf")
for href in page_hrefs:
new_url = 'https://ocw.mit.edu' + href
print(new_url)
yield scrapy.Request(new_url,callback=self.parse_href)
def parse_href(self,response):
with open('linear.pdf','ab') as f:
f.write(response.body)
f.close()