ShineLeBlog

Python:爬取一个可下载的PDF链接并保存为本地pdf文件

2021-04-09 17:52  ShineLe  阅读(766)  评论(0编辑  收藏  举报

问题:网页http://gk.chengdu.gov.cn/govInfo/detail.action?id=2653973&tn=2中有一个PDF需要下载,开发者模式下该PDF的链接为http://gk.chengdu.gov.cn/uploadfiles/07180246020404/2020061116272871.pdf,如何下载该PDF保存为本地文件文件?

1)进入scrapy shell

scrapy shell

2)爬取该PDF所在的网页URL

shell模式下用方法fetch

 fetch(\'http://gk.chengdu.gov.cn/govInfo/detail.action?id=2653973&tn=2\')

爬取到网页内容全部保存在了response

3)通过XPath提取PDF的链接

In [5]: response.xpath(\'.//a[starts-with(@class,"ke")]/@href\').extract()[0]
Out[5]: \'http://gk.chengdu.gov.cn/uploadfiles/07180246020404/2020061116272871.pdf\'

4)通过fetch请求该URL,得到response,PDF内容就都保存在了该response中,通过response.body提取

with open(\'abc.pdf\',\'wb\')as f:
    f.write(response.body)

5)这样内容就写入了PDF文件abc.pdf

分类:

技术点:

相关文章:

  • 2021-09-10
  • 2021-11-23
  • 2021-11-23
  • 2022-12-23
  • 2021-09-11
  • 2022-12-23
  • 2021-08-03
猜你喜欢
  • 2021-11-22
  • 2021-11-08
  • 2022-12-23
  • 2021-12-08
  • 2022-01-01
  • 2021-12-07
  • 2021-04-02
相关资源
相似解决方案