【发布时间】:2021-07-06 20:09:15
【问题描述】:
所以只是一点上下文,我目前正在使用 Element Tree 来抓取几个加密新闻提要以获取最新文章的标题。下面的代码适用于大多数网站,但是在某些提要中我收到以下错误,例如:
xml.etree.ElementTree.ParseError:标签不匹配:第 134 行,第 2 列
我猜这是由于该网站的 XML 代码中的错误。我正在寻找一种方法来绕过此错误并无论如何都拉出最后一个标题,希望对此有所帮助:) 代码如下:
import xml.etree.ElementTree as ET
import requests
r = requests.get('https://cointelegraph.com/feed')
root = ET.fromstring(r.text)
headline = root.find('channel/item/title').text
print(headline)
【问题讨论】:
-
IIRC,如果您指定 'html.parser',BeautifulSoup 对格式错误的 xml 更宽容。
标签: python python-3.x parsing web-scraping elementtree