本次主要介紹爬蟲讀取純文本,PDF,圖像,視頻,郵件等文件,以及如何把文件下載到指定的文件夾,讀取並提取數據,介紹文檔的不一樣編碼類型,讓程序能夠讀取非英文的html頁面。html
文檔編碼是告訴程序讀取文檔的規則。文檔編碼的規則一般能夠根據文件的擴展名進行判斷。一般的文檔格式有純文本,PDF,PNG,CIF等。python
把文件存儲爲純文本格式的形式如今並很少,http://www.pythonscraping.com/pages/warandpeace/chapter1.txt 就是一個純文本存儲的例子,讀取時只須要使用urlopen打開便可,不須要使用beautifulsoup。由於這個頁面不是html。api
from urllib.request import urlopen textPage = urlopen("http://www.pythonscraping.com/pages/warandpeace/chapter1.txt") print(textPage.read())
全球統一的編碼格式UTF-8,並非存儲爲8位,8位只能存儲256個字符,顯然是不夠的.UTF-8每一個字符的開頭都是標識這個字符用了多少字節,一個字符最多使用4字節。大部分網站是使用UTF-8的,有一小部分歐洲網站採用ISO編碼,能夠在網頁的首部查看:網絡
<meta charset="utf-8" />
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
正確的讀取UTF-8文檔(如俄文):函數
from urllib.request import urlopen textPage = urlopen("http://www.pythonscraping.com/pages/warandpeace/chapter1-ru.txt") print(str(textPage.read(), 'utf-8'))
在進行網絡數據採集,有時候須要採集csv格式的數據或者須要把數據存爲csv格式。python中有一個庫對csv的支持很是好。 http://docs.python.org/3.4/library/csv.html 讀取csv有三張解決方法: 1.手動下載csv到本地,用python定位信息位置 2.用python程序下載csv文件,讀取後刪除文件 3.把文件讀取爲字符串,而後轉換爲一個StringIO對象,使他具備文件的屬性。 建議使用方法3.下面程序是從網上得到一個CSV文件,而後打印到命令行。網站
from urllib.request import urlopen from io import StringIO import csv data = urlopen("http://pythonscraping.com/files/MontyPythonAlbums.csv").read().decode('ascii', 'ignore') dataFile = StringIO(data) csvReader = csv.reader(dataFile) for row in csvReader: print(row)
輸出結果是:編碼
['Name', 'Year'] ["Monty Python's Flying Circus", '1970'] ['Another Monty Python Record', '1971'] ["Monty Python's Previous Record", '1972'] ...
返回的是一個列表,能夠使用下面的方法進行處理:url
for row in csvReader: print("The album \""+row[0]+"\" was released in "+str(row[1]))
返回的結果是:命令行
The album "Name" was released in Year The album "Monty Python's Flying Circus" was released in 1970 The album "Another Monty Python Record" was released in 1971 The album "Monty Python's Previous Record" was released in 1972 ...
PDFMiner3K是一個很是好用的庫(PDFMiner的python3移植版),很是靈活,能夠經過命令行調用也能夠使用代碼調用,可以處理不一樣語言的編碼,能夠便利的處理網絡文件。 下載安裝:https://pypi.python.org/pypi/pdfminer3k 解壓並用下面命令安裝:code
python setup.py install
參考文檔在解壓後的文件夾中:/pdfminer3k-1.3.0/docs/index.html
下面的程序能夠把任意PDF讀取成字符串,而後用StringIO轉換成文件對象。
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 from io import open def readPDF(pdfFile): rsrcmgr = PDFResourceManager() retstr = StringIO() laparams = LAParams() device = TextConverter(rsrcmgr, retstr, laparams=laparams) process_pdf(rsrcmgr, device, pdfFile) device.close() content = retstr.getvalue() retstr.close() return content pdfFile = urlopen("http://pythonscraping.com/pages/warandpeace/chapter1.pdf") outputString = readPDF(pdfFile) print(outputString) pdfFile.close()
定義了一個readPDF函數,當想要讀取本地pdf時,能夠直接使用open函數便可。
pdfFile = open("../pages/warandpeace/chapter1.pdf", 'rb')
從docx文件中讀取xml
from zipfile import ZipFile from urllib.request import urlopen from io import BytesIO wordFile = urlopen("http://pythonscraping.com/pages/AWordDocument.docx").read() wordFile = BytesIO(wordFile) document = ZipFile(wordFile) xml_content = document.read('word/document.xml') print(xml_content.decode('utf-8'))
轉換後的文檔內都包含在<w:t>中,使用下面代碼提取:
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')) textStrings = wordObj.findAll("w:t") for textElem in textStrings: print(textElem.text)