python爬虫学习第二十六天

来源:互联网 发布:非凡软件站手机版 编辑:程序博客网 时间:2024/06/01 20:50

今天第一个内容是如何处理.docx文件(微端microsoft office广泛使用的文件格式)

微软 Office 产品中 Word 用 .doc 文件格式。这种二进制格式很难读 取,而且能够读取 word 格式的软件很少。为了跟上时代,让自己的软件能够符合主流软 件的标准,微软决定使用 Open Office 的类 XML 格式标准,此后新版 Word 文件才与其他 文字处理软件兼容,这个格式就是 .docx
Python 对 这 种 Google Docs、Open Office 和 Microsoft Office 都 在 使 用 的 .docx 格 式 的 支 持 还 不 够 好,如果想读取 Microsoft Office 文件的正文内容,是没有现有的库能够很好胜任的,我们需要自己动手找方法

第一步是从文件读取 XML

from urllib.request import urlopenfrom zipfile import ZipFilefrom io import BytesIOwordFile = 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"))

上面这段代码输出下面的内容:

<!--?xml version="1.0" encoding="UTF-8" standalone="yes"?--> <w:document mc:ignorable="w14 w15 wp14" xmlns:m="http://schemas.openx mlformats.org/officeDocument/2006/math" xmlns:mc="http://schemas.open xmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-micros oft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/off iceDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vm l" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/m ain" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w14="htt p://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http:// schemas.microsoft.com/office/word/2012/wordml" xmlns:wne="http://sche mas.microsoft.com/office/word/2006/wordml" xmlns:wp="http://schemas.o penxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wp14="h ttp://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" x mlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessin gCanvas" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wor dprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word /2010/wordprocessingInk" xmlns:wps="http://schemas.microsoft.com/offi ce/word/2010/wordprocessingShape"><w:body><w:p w:rsidp="00764658" w:r sidr="00764658" w:rsidrdefault="00764658"><w:ppr><w:pstyle w:val="Tit le"></w:pstyle></w:ppr><w:r><w:t>A Word Document on a Website</w:t></ w:r><w:bookmarkstart w:id="0" w:name="_GoBack"></w:bookmarkstart><w:b ookmarkend w:id="0"></w:bookmarkend></w:p><w:p w:rsidp="00764658" w:r sidr="00764658" w:rsidrdefault="00764658"></w:p><w:p w:rsidp="0076465 8" w:rsidr="00764658" w:rsidrdefault="00764658" w:rsidrpr="00764658"> <w: r> <w:t>This is a Word document, full of content that you want ve ry much. Unfortunately, it’ s difficult to access because I’ m putting it on my website as a .</w:t></w:r><w:prooferr w:type="spellStart"></ w:prooferr><w:r><w:t>docx</w:t></w:r><w:prooferr w:type="spellEnd"></ w:prooferr> <w:r> <w:t xml:space="preserve"> file, rather than just p ublishing it as HTML</w:t> </w:r> </w:p> <w:sectpr w:rsidr="00764658" w:rsidrpr="00764658"> <w:pgszw:h="15840" w:w="12240"></w:pgsz><w:pgm ar w:bottom="1440" w:footer="720" w:gutter="0" w:header="720" w:left= "1440" w:right="1440" w:top="1440"></w:pgmar> <w:cols w:space="720">< /w:cols&g; <w:docgrid w:linepitch="360"></w:docgrid> </w:sectpr> </w: body> </w:document> 

这是xml格式的文档内容,但不是我们想要的,想要从中获取我们需要的信息,还需要一些别的操作。

文档的所有正文内容都包含在 < w:t > 标签里面,标题内容也是如此,所以我们的思路就是用Beautifulsoup提取处w:t标签,如下程序实现了这一点

from urllib.request import urlopenfrom zipfile import ZipFilefrom io import BytesIOfrom bs4 import BeautifulSoupwordFile = urlopen("http://pythonscraping.com/pages/AWordDocument.docx").read()wordFile = BytesIO(wordFile)document = ZipFile(wordFile)xml_content = document.read("word/document.xml")bsObj = BeautifulSoup(xml_content.decode("utf-8"),'xml')textStrings = bsObj.findAll("w:t")for textElem in textStrings:    print(textElem.text)

输出如下:

A Word Document on a Website
This is a Word document, full of content that you want very much. Unfortunately, it’s difficult to access because I’m putting it on my website as a .
docx
file, rather than just publishing it as HTML

到这里第一部分,初级爬虫采集就完成了,接下来就要进入高级爬虫数据采集了。
首先,数据清洗。

网页中并不全是格式标准的数据,但实际采集过程中我们不可避免的要去一些凌乱的格式中去寻找我们需要的数据。这时候就要用到数据清洗这个功能了,所谓数据清洗,简单来说是应用一些工具和技术,通过改变代码的编写方式,帮你 从源头控制数据零乱的问题,并且对已经进入数据库的数据进行清洗

练习1 返回维基百科词条“Python programming language”的 2-gram 列表
ngram可以自行百度,n元语法

from urllib.request import urlopenfrom bs4 import BeautifulSoupdef ngrams(input1,n):    input1 = input1.split(' ')    output = []    for i in range(len(input1)-n+1):        output.append(input1[i:i+n])    return output    passhtml = urlopen("http://en.wikipedia.org/wiki/Python_(programming_language)")bsObj = BeautifulSoup(html)content = bsObj.find("div",{"id":"mw-content-text"}).get_text()ngrams_2 = ngrams(content,2)# print(ngrams_2)print("number if 2-grams:",str(len(ngrams_2)))

今天脑子有点懵逼,先到这里,打卡~

原创粉丝点击