HTML文档是互联网上的主要文档类型,但还存在如TXT、WORD、Excel、PDF、csv等多种类型的文档。网络爬虫不仅需要能够抓取HTML中的敏感信息,也需要有抓取其他类型文档的能力。下面简要记录一些个人已知的基于python3的抓取方法,以备查阅。
- 抓取TXT文档
在python3下,常用方法是使用urllib.request.urlopen方法直接获取。之后利用正则表达式等方式进行敏感词检索。
from urllib.request import urlopen
from urllib.error import URLError,HTTPError
import re
try:
textPage = urlopen("http://www.pythonscraping.com/pages/warandpeace/chapter1.txt")
except (URLError,HTTPError) as e:
print("Errors:\n")
print(e)
text = str(textPage.read())
pattern = re.compile("\..*1805(\w|,|\s|-)*(\.)")
match = pattern.search(text)
if match is not None:
print(match.group())
ss = text.split('.')
key_words = "1805"
words_list = [x.lower() for x in key_words.split()]
for item in ss:
if all([word in item.lower() and True or False for word in words_list]):
print(item)
上面的方法是已知目标网页为txt文本时的抓取。事实上,在自动抓取网页时,必须考虑目标网页是否为纯文本,用何种编码等问题。
如果只是编码问题,可以简单使用print(textPage.read(),’utf-8’)等python字符处理方法来解决,如果抓取的是某个HTML,最好先分析,例如:
from urllib.request import urlopen
from urllib.error import URLError,HTTPError
from bs4 import BeautifulSoup
try:
html = urlopen("https://en.wikipedia.org/wiki/Python_(programming_language)")
except (URLError,HTTPError) as e:
print(e)
try:
bsObj = BeautifulSoup(html,"html.parser")
cOntent= bsObj.find("div",{"id":"mw-content-text"}).get_text()
except AttributeError as e:
print(e)
meta = bsObj.find("meta")
if meta.attrs['charset'] == 'UTF-8':
cOntent= bytes(content,"UTF-8")
print("-----------------UTF-8--------------")
print(content.decode("UTF-8"))
if meta.attrs['charset'] == 'iso-8859-1':
cOntent= bytes(content,"iso-8859-1")
print("--------------iso-8859-1------------")
print(content.decode("iso-8859-1"))
2.抓取CSV文档
CSV文件是一种常见的数据存档文件,与TXT文档基本类似,但在内容组织上有一定格式,文件的首行为标题列,之后的文件中的每一行表示一个数据记录。这就像一个二维数据表或excel表格一样。 python3中包含一个csv解析库,可用于读写csv文件,但其读取目标一般要求是在本地,要读取远程网络上的csv文件需要用urllib.request.urlopen先获取。例如:
from urllib.request import urlopen
import csv
from io import StringIO
try:
data = urlopen("http://pythonscraping.com/files/MontyPythonAlbums.csv").read().decode("ascii","ignore")
except (URLError,HTTPError) as e:
print("Errors:\n")
print(e)
dataFile = StringIO(data)
csvReader = csv.reader(dataFile)
count = 0
for row in csvReader:
if count <10:
print(row)
else:
print("...\n...")
break
count += 1
with open("./localtmp.csv","wt",newline='',encoding='utf-8') as localcsvfile:
writer = csv.writer(localcsvfile)
count = 0
try:
for row in csvReader:
if count <10:
writer.writerow(row)
else:
break
count += 1
finally:
localcsvfile.close()
csv文档的标题行(首行)需要特殊处理,csv.DictReader可以很好的解决这个问题。DictReader将读取的行转换为python字典对象,而不是列表。标题行的各列名即为字典的键名。
from urllib.request import urlopen
import csv
from io import StringIO
try:
data = urlopen("http://pythonscraping.com/files/MontyPythonAlbums.csv").read().decode("ascii","ignore")
except (URLError,HTTPError) as e:
print("Errors:\n")
print(e)
dataFile = StringIO(data)
csvReader = csv.reader(dataFile)
dictReader = csv.DictReader(dataFile)
print(dictReader.fieldnames)
count = 0
for row in dictReader:
if count <10:
print(row)
else:
print("...\n...")
break
count += 1
3.抓取PDF文档
pdf文档的远程抓取与操作,可借助比较流行的pdfminer3k库来完成。
from urllib.request import urlopen
from pdfminer.pdfinterp import PDFResourceManager,process_pdf
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from io import StringIO,open
def readPDF(filename):
resmgr = PDFResourceManager()
retstr = StringIO()
laparams = LAParams()
device = TextConverter(resmgr,retstr,laparams=laparams)
process_pdf(resmgr,device,filename)
device.close()
cOntent= retstr.getvalue()
retstr.close()
return content
try:
pdffile = urlopen("http://www.fit.vutbr.cz/research/groups/speech/servite/2010/rnnlm_mikolov.pdf")
except (URLError,HTTPError) as e:
print("Errors:\n")
print(e)
outputString = readPDF(pdffile)
print(outputString)
pdffile.close()
4.抓取WORD
老版word使用了二进制格式,后缀名为.doc,word2007后出现了与OPEN OFFICE类似的类XML格式文档,后缀名为.docx。python对word文档的支持不够,似乎没有完美解决方案。为读取docx内容,可以使用以下方法:
(1)利用urlopen抓取远程word docx文件;
(2)将其转换为内存字节流;
(3)解压缩(docx是压缩后文件);
(4)将解压后文件作为xml读取
(5)寻找xml中的标签(正文内容)并处理
from zipfile import ZipFile
from urllib.request import urlopen
from io import BytesIO
from bs4 import BeautifulSoup
wordFile = urlopen("http://pythonscraping.com/pages/AWordDocument.docx").read()
wordFile = BytesIO(wordFile)
document = ZipFile(wordFile)
xml_cOntent= document.read("word/document.xml")
wordObj = BeautifulSoup(xml_content.decode("utf-8"),"lxml")
textStrings = wordObj.findAll("w:t")
for textElem in textStrings:
print(textElem.text)
抓取EXCEL
抓取HTML源文档
抓取HTML表单数据
抓取Javascript数据