python网络爬虫使用BeautifulSoup时出现findAll执行失败问题分析

来源:互联网 发布:读写数据什么意思 编辑:程序博客网 时间:2024/06/17 04:20

    最近在学习和演技python网络爬虫,并且使用了BeautifulSoup进行格式化查找。在使用Python读取word文档的时候,由于在Linux环境下读取Word文档,需要先将word文档转换为xml文档,在使用findAll函数进行文档内容定位时。findAll执行无结果。经过分析和问题查找,发现是由于为将BeautifulSoup的解析器指定为xml,导致后续的查找失败。修改后的代码片段如下:

from zipfile import ZipFilefrom urllib.request import urlopenfrom 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')wordObj=BeautifulSoup(xml_content.decode('utf-8'),"xml")textString=wordObj.findAll("w:t")for textElem in textString:    print(textElem.text)
   整理出来,望大家在遇到findAll执行失败的时候,首先考虑是否BeautifulSoup的解析结构不正确,为指定解析器。