代码如下:
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()
1
Fuyu0gap OP 分开下载页面里所有的 PDF 是可以的,以及这个 Markdown 显示不全怎么肥四……
|
2
pc10201 2018-04-21 19:07:19 +08:00
with open('linear.pdf','ab') as f
pdf 不能这么简单的拼接吧,最好每一个分开下载,再用第三方工具整合在一起 |