python爬虫学习第二十六天

博客讲述了如何处理.docx文件,指出Python对.docx格式的支持不够完善,需要利用XML和BeautifulSoup来提取内容。文章展示了从Word文档中提取正文的步骤,并提及了后续的高级爬虫数据采集和数据清洗的重要性。
摘要由CSDN通过智能技术生成

今天第一个内容是如何处理.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 urlopen
from zipfile import ZipFile
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"))

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

<!--?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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值