安装 pip install pdfminer

  爬取数据是数据分析项目的第一个阶段,有的加密成pdf格式的文件,下载后需要解析,使用pdfminer工具。

  先介绍一下什么是pdfminer

  下面是官方一段英文介绍:

PDFMiner is a tool for extracting information from PDF documents. Unlike other PDF-related tools, it focuses entirely on getting and analyzing text data. PDFMiner allows one to obtain the exact location of text in a page, as well as other information such as fonts or lines. It includes a PDF converter that can transform PDF files into other text formats (such as HTML). It has an extensible PDF parser that can be used for other purposes than text analysis.

  主要用两个例子学习它的使用

  例子1:

$ pdf2txt.py -o output.html samples/naacl06-shinyama.pdf
(extract text as an HTML file whose filename is output.html) $ pdf2txt.py -V -c euc-jp -o output.html samples/jo.pdf
(extract a Japanese HTML file in vertical writing, CMap is required) $ pdf2txt.py -P mypassword -o output.txt secret.pdf
(extract a text from an encrypted PDF file)

  参数:

 -o filename
Specifies the output file name. By default, it prints the extracted contents to stdout in text format. -p pageno[,pageno,...]
Specifies the comma-separated list of the page numbers to be extracted. Page numbers start at one. By default, it extracts text from all the pages. -c codec
Specifies the output codec. -t type
Specifies the output format. The following formats are currently supported. text : TEXT format. (Default)
html : HTML format. Not recommended for extraction purposes because the markup is messy.
xml : XML format. Provides the most information.
tag : "Tagged PDF" format. A tagged PDF has its own contents annotated with HTML-like tags. pdf2txt tries to extract its content streams rather than inferring its text locations. Tags used here are defined in the PDF specification (See §10.7 "Tagged PDF"). -I image_directory
Specifies the output directory for image extraction. Currently only JPEG images are supported. -M char_margin

例子2:

$ dumppdf.py -a foo.pdf
(dump all the headers and contents, except stream objects) $ dumppdf.py -T foo.pdf
(dump the table of contents) $ dumppdf.py -r -i6 foo.pdf > pic.jpeg
(extract a JPEG image)

参数:

 -a
Instructs to dump all the objects. By default, it only prints the document trailer (like a header). -i objno,objno, ...
Specifies PDF object IDs to display. Comma-separated IDs, or multiple -i options are accepted. -p pageno,pageno, ...
Specifies the page number to be extracted. Comma-separated page numbers, or multiple -p options are accepted. Note that page numbers start at one, not zero. -r (raw)
-b (binary)
-t (text)
Specifies the output format of stream contents. Because the contents of stream objects can be very large, they are omitted when none of the options above is specified. With -r option, the "raw" stream contents are dumped without decompression. With -b option, the decompressed contents are dumped as a binary blob. With -t option, the decompressed contents are dumped in a text format, similar to repr() manner. When -r or -b option is given, no stream header is displayed for the ease of saving it to a file. -T
Shows the table of contents.

编写自己的pdf解析文档:

# -*- coding: utf- -*-
from pdfminer.pdfparser import PDFParser
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdfpage import PDFPage
from pdfminer.pdfpage import PDFTextExtractionNotAllowed
from pdfminer.pdfinterp import PDFResourceManager
from pdfminer.pdfinterp import PDFPageInterpreter
from pdfminer.pdfdevice import PDFDevice
from pdfminer.layout import *
from pdfminer.converter import PDFPageAggregator
import os
# os.chdir(r'F:\test')
fp = open('PDF/1202268749.pdf', 'rb')
#来创建一个pdf文档分析器
parser = PDFParser(fp)
#创建一个PDF文档对象存储文档结构
document = PDFDocument(parser)
# 检查文件是否允许文本提取
if not document.is_extractable:
raise PDFTextExtractionNotAllowed
else:
# 创建一个PDF资源管理器对象来存储共赏资源
rsrcmgr=PDFResourceManager()
# 设定参数进行分析
laparams=LAParams()
# 创建一个PDF设备对象
# device=PDFDevice(rsrcmgr)
device=PDFPageAggregator(rsrcmgr,laparams=laparams)
# 创建一个PDF解释器对象
interpreter=PDFPageInterpreter(rsrcmgr,device)
# 处理每一页
for page in PDFPage.create_pages(document):
interpreter.process_page(page)
# 接受该页面的LTPage对象
layout=device.get_result()
for x in layout:
if(isinstance(x,LTTextBoxHorizontal)):
with open('a.html','a') as f:
f.write(x.get_text().encode('utf-8')+'\n')

参考:

pdfminer官网:  http://www.unixuser.org/~euske/python/pdfminer/index.html

http://www.cnblogs.com/RoundGirl/p/4979267.html

pdfminer API介绍:pdf网页爬虫的更多相关文章

  1. Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱(转)

    原文:http://www.52nlp.cn/python-网页爬虫-文本处理-科学计算-机器学习-数据挖掘 曾经因为NLTK的缘故开始学习Python,之后渐渐成为我工作中的第一辅助脚本语言,虽然开 ...

  2. [resource-]Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱

    reference: http://www.52nlp.cn/python-%e7%bd%91%e9%a1%b5%e7%88%ac%e8%99%ab-%e6%96%87%e6%9c%ac%e5%a4% ...

  3. Python网页爬虫(一)

    很多时候我们想要获得网站的数据,但是网站并没有提供相应的API调用,这时候应该怎么办呢?还有的时候我们需要模拟人的一些行为,例如点击网页上的按钮等,又有什么好的解决方法吗?这些正是python和网页爬 ...

  4. 【Python】Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱

    本文转载自:https://www.cnblogs.com/colipso/p/4284510.html 好文 mark http://www.52nlp.cn/python-%E7%BD%91%E9 ...

  5. 网页爬虫--scrapy入门

    本篇从实际出发,展示如何用网页爬虫.并介绍一个流行的爬虫框架~ 1. 网页爬虫的过程 所谓网页爬虫,就是模拟浏览器的行为访问网站,从而获得网页信息的程序.正因为是程序,所以获得网页的速度可以轻易超过单 ...

  6. 网页爬虫的设计与实现(Java版)

    网页爬虫的设计与实现(Java版)     最近为了练手而且对网页爬虫也挺感兴趣,决定自己写一个网页爬虫程序. 首先看看爬虫都应该有哪些功能. 内容来自(http://www.ibm.com/deve ...

  7. 网页抓取:PHP实现网页爬虫方式小结

    来源:http://www.ido321.com/1158.html 抓取某一个网页中的内容,需要对DOM树进行解析,找到指定节点后,再抓取我们需要的内容,过程有点繁琐.LZ总结了几种常用的.易于实现 ...

  8. nodeJS实现简单网页爬虫功能

    前面的话 本文将使用nodeJS实现一个简单的网页爬虫功能 网页源码 使用http.get()方法获取网页源码,以hao123网站的头条页面为例 http://tuijian.hao123.com/h ...

  9. PHP实现网页爬虫

    抓取某一个网页中的内容,需要对DOM树进行解析,找到指定节点后,再抓取我们需要的内容,过程有点繁琐.LZ总结了几种常用的.易于实现的网页抓取方式,如果熟悉JQuery选择器,这几种框架会相当简单. 一 ...

随机推荐

  1. ASN1编码中的OID

    0.9.2342.19200300.100.1.25, domainComponent1.2.36.68980861.1.1.10, Signet pilot1.2.36.68980861.1.1.1 ...

  2. 任务分线程实现(java)

    1.创建一个类,用户存储信息 public class Users { private String userid; private String username; public Users() { ...

  3. c语言运算优先级与结合方向的问题

    c语言运算的结合方向一直困扰我,不过我终于get到了哈哈 先附上c语言运算优先级和结合方向图片 以上有c语言一共的15个优先级,从上往下递增 c语言的运算符号有很多,当我们打代码或者看代码的时候符号多 ...

  4. 降低 80% 的读写响应延迟!我们测评了 etcd 3.4 新特性(内含读写发展史)

    作者 | 陈洁(墨封)  阿里云开发工程师 导读:etcd 作为 K8s 集群中的存储组件,读写性能方面会受到很多压力,而 etcd 3.4 中的新特性将有效缓解压力,本文将从 etcd 数据读写机制 ...

  5. Android 点九图机制讲解及在聊天气泡中的应用

    点九图简介 Android为了使用同一张图作为不同数量文字的背景,设计了一种可以指定区域拉伸的图片格式".9.png",这种图片格式就是点九图. 注意:这种图片格式只能被使用于An ...

  6. 提供就医帮助的安卓APP

    首先 这是我们团队第一次开发安卓APP,也是我 个人第一个开发项目APP,俗话说:“万事开头难”.所以对于新手的我们来说,做好开发前的准备至关重要.凡事预则立不预则废! 首先我们团队这次开发的提供就医 ...

  7. ActiveMQ+ZooKeeper搭建高可用集群

    一.说明 实际的应用中,一般为了应用的高可用性,都会搭建集群环境去处理.部署多台应用,这样,即使一台有问题,其他热备应用可以立马顶上,继续提供服务. ActiveMQ的集群部署,基于zookeeper ...

  8. Kubernetes的Secret对象的使用

    Secret可以想要访问的加密数据,存放到Etcd中,Pod可以通过的Volume的方式,访问到Secret保存的信息 ,当数据修改的时候,Pod挂载的Secret文件也会被修改 一.创建Secret ...

  9. 品Spring:SpringBoot发起bean定义注册的“二次攻坚战”

    上一篇文章整体非常轻松,因为在容器启动前,只注册了一个bean定义,就是SpringBoot的主类. OK,今天接着从容器的启动入手,找出剩余所有的bean定义的注册过程. 具体细节肯定会颇为复杂,同 ...

  10. Mysql的MyISAM和InnoDB存储引擎的区别

    从以下几个方面: 1.存储结构 每个MyISAM在磁盘上存储成三个文件.第一个文件的名字以表的名字开始,扩展名指出文件类型. .frm文件存储表定义. 数据文件的扩展名为.MYD (MYData).  ...