用python爬虫批量下载pdf的实现


Posted in Python onDecember 01, 2020

今天遇到一个任务,给一个excel文件,里面有500多个pdf文件的下载链接,需要把这些文件全部下载下来。我知道用python爬虫可以批量下载,不过之前没有接触过。今天下午找了下资料,终于成功搞定,免去了手动下载的烦恼。

由于我搭建的python版本是3.5,我学习了上面列举的参考文献2中的代码,这里的版本为2.7,有些语法已经不适用了。我修正了部分语法,如下:

# coding = UTF-8
# 爬取李东风PDF文档,网址:http://www.math.pku.edu.cn/teachers/lidf/docs/textrick/index.htm

import urllib.request
import re
import os

# open the url and read
def getHtml(url):
  page = urllib.request.urlopen(url)
  html = page.read()
  page.close()
  return html

# compile the regular expressions and find
# all stuff we need
def getUrl(html):
  reg = r'(?:href|HREF)="?((?:http://)?.+?\.pdf)'
  url_re = re.compile(reg)
  url_lst = url_re.findall(html.decode('gb2312'))
  return(url_lst)

def getFile(url):
  file_name = url.split('/')[-1]
  u = urllib.request.urlopen(url)
  f = open(file_name, 'wb')

  block_sz = 8192
  while True:
    buffer = u.read(block_sz)
    if not buffer:
      break

    f.write(buffer)
  f.close()
  print ("Sucessful to download" + " " + file_name)


root_url = 'http://www.math.pku.edu.cn/teachers/lidf/docs/textrick/'

raw_url = 'http://www.math.pku.edu.cn/teachers/lidf/docs/textrick/index.htm'

html = getHtml(raw_url)
url_lst = getUrl(html)

os.mkdir('ldf_download')
os.chdir(os.path.join(os.getcwd(), 'ldf_download'))

for url in url_lst[:]:
  url = root_url + url
  getFile(url)

上面这个例子是个很好的模板。当然,上面的还不适用于我的情况,我的做法是:先把地址写到了html文件中,然后对正则匹配部分做了些修改,我需要匹配的地址都是这样的,http://pm.zjsti.gov.cn/tempublicfiles/G176200001/G176200001.pdf。改进后的代码如下:

# coding = UTF-8
# 爬取自己编写的html链接中的PDF文档,网址:file:///E:/ZjuTH/Documents/pythonCode/pythontest.html

import urllib.request
import re
import os

# open the url and read
def getHtml(url):
  page = urllib.request.urlopen(url)
  html = page.read()
  page.close()
  return html

# compile the regular expressions and find
# all stuff we need
def getUrl(html):
  reg = r'([A-Z]\d+)' #匹配了G176200001
  url_re = re.compile(reg)
  url_lst = url_re.findall(html.decode('UTF-8')) #返回匹配的数组
  return(url_lst)

def getFile(url):
  file_name = url.split('/')[-1]
  u = urllib.request.urlopen(url)
  f = open(file_name, 'wb')

  block_sz = 8192
  while True:
    buffer = u.read(block_sz)
    if not buffer:
      break

    f.write(buffer)
  f.close()
  print ("Sucessful to download" + " " + file_name)


root_url = 'http://pm.zjsti.gov.cn/tempublicfiles/' #下载地址中相同的部分

raw_url = 'file:///E:/ZjuTH/Documents/pythonCode/pythontest.html'

html = getHtml(raw_url)
url_lst = getUrl(html)

os.mkdir('pdf_download')
os.chdir(os.path.join(os.getcwd(), 'pdf_download'))

for url in url_lst[:]:
  url = root_url + url+'/'+url+'.pdf' #形成完整的下载地址
  getFile(url)

这就轻松搞定啦。

我参考了以下资料,这对我很有帮助:
1、廖雪峰python教程
2、用Python 爬虫批量下载PDF文档
3、用Python 爬虫爬取贴吧图片
4、Python爬虫学习系列教程

到此这篇关于用python爬虫批量下载pdf的实现的文章就介绍到这了,更多相关python爬虫批量下载pdf内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python 正则式使用心得
May 07 Python
python服务器与android客户端socket通信实例
Nov 12 Python
Python cookbook(数据结构与算法)将多个映射合并为单个映射的方法
Apr 19 Python
Django 登陆验证码和中间件的实现
Aug 17 Python
详解小白之KMP算法及python实现
Apr 04 Python
利用anaconda作为python的依赖库管理方法
Aug 13 Python
python 单线程和异步协程工作方式解析
Sep 28 Python
Python网络爬虫信息提取mooc代码实例
Mar 06 Python
python开发实例之python使用Websocket库开发简单聊天工具实例详解(python+Websocket+JS)
Mar 18 Python
python爬虫爬取网页数据并解析数据
Sep 18 Python
python温度转换华氏温度实现代码
Dec 06 Python
python中使用asyncio实现异步IO实例分析
Feb 26 Python
python3字符串输出常见面试题总结
Dec 01 #Python
python3中数组逆序输出方法
Dec 01 #Python
Python爬虫简单运用爬取代理IP的实现
Dec 01 #Python
python爬虫请求头的使用
Dec 01 #Python
在pycharm创建scrapy项目的实现步骤
Dec 01 #Python
Python实现迪杰斯特拉算法并生成最短路径的示例代码
Dec 01 #Python
python 检测图片是否有马赛克
Dec 01 #Python
You might like
PHP中文处理 中文字符串截取(mb_substr)和获取中文字符串字数
2011/11/10 PHP
PHP里的$_GET数组介绍
2019/03/22 PHP
Laravel框架自定义公共函数的引入操作示例
2019/04/16 PHP
php ZipArchive实现多文件打包下载实例
2019/10/31 PHP
jquery 新浪网易的评论块制作
2010/07/01 Javascript
根据一段代码浅谈Javascript闭包
2010/12/14 Javascript
基于JQuery的浮动DIV显示提示信息并自动隐藏
2011/02/11 Javascript
Jquery下:nth-child(an+b)的使用注意
2011/05/28 Javascript
JS中三目运算符和if else的区别分析与示例
2014/11/21 Javascript
jquery获取多个checkbox的值异步提交给php
2015/07/07 Javascript
深入理解jquery跨域请求方法
2016/05/18 Javascript
vue中的scope使用详解
2017/10/29 Javascript
vue中使用cropperjs的方法
2018/03/01 Javascript
vue 移动端注入骨架屏的配置方法
2019/06/25 Javascript
在vscode 中设置 vue模板内容的方法
2020/09/02 Javascript
[02:32]DOTA2英雄基础教程 祸乱之源
2013/12/23 DOTA
[03:58]2014DOTA2国际邀请赛 龙宝赛后解密DK获胜之道
2014/07/14 DOTA
[58:15]2018DOTA2亚洲邀请赛 4.1 小组赛 A组 NB vs Liquid
2018/04/02 DOTA
使用python装饰器验证配置文件示例
2014/02/24 Python
go和python变量赋值遇到的一个问题
2017/08/31 Python
Python探索之自定义实现线程池
2017/10/27 Python
使用python实现链表操作
2018/01/26 Python
详解Python字典的操作
2019/03/04 Python
简单了解Python matplotlib线的属性
2019/06/29 Python
Django组件content-type使用方法详解
2019/07/19 Python
python图形开发GUI库wxpython使用方法详解
2020/02/14 Python
python3实现往mysql中插入datetime类型的数据
2020/03/02 Python
Python MySQL 日期时间格式化作为参数的操作
2020/03/02 Python
基于Python绘制个人足迹地图
2020/06/01 Python
Python 防止死锁的方法
2020/07/29 Python
python获得命令行输入的参数的两种方式
2020/11/02 Python
Scrapy-Redis之RedisSpider与RedisCrawlSpider详解
2020/11/18 Python
介绍一下OSI七层模型
2012/07/03 面试题
写给老婆的检讨书
2014/02/21 职场文书
《1942》观后感
2015/06/08 职场文书
Java生成读取条形码和二维码的简单示例
2021/07/09 Java/Android