gpt4 book ai didi

python xml 解析cdata

转载 作者:行者123 更新时间:2023-12-01 03:05:15 24 4
gpt4 key购买 nike

我尝试从外汇日历中抓取新闻数据,但 xml 文件有一个小问题

def get_news_calendar():
r = requests.get('http://www.forexfactory.com/ffcal_week_this.xml')
soup = BeautifulSoup(r.text , 'lxml')
events = soup.find_all('event')
for event in events:
print event.find('title').text, event.find('country').text, event.find('date'), event.find('time').text, event.find('impact').text, event.find('forecast').text, event.find('previous').text

输出:

Current Account EUR <date></date>    
Retail Sales m/m GBP <date></date>
MPC Member Saunders Speaks GBP <date></date>
Core CPI m/m CAD <date></date>
CPI m/m CAD <date></date>
Trimmed CPI y/y CAD <date></date>
Median CPI y/y CAD <date></date>
Common CPI y/y CAD <date></date>
FOMC Member Kashkari Speaks USD <date></date>
Flash Manufacturing PMI USD <date></date>
Flash Services PMI USD <date></date>
Existing Home Sales USD <date></date>
IMF Meetings ALL <date></date>
IMF Meetings ALL <date></date>
Treasury Sec Mnuchin Speaks USD <date></date>
French Presidential Election EUR <date></date>

示例 xml 文件:

<event>
<title>German Flash Manufacturing PMI</title>
<country>EUR</country>
<date><![CDATA[04-21-2017]]></date>
<time><![CDATA[7:30am]]></time>
<impact><![CDATA[Medium]]></impact>
<forecast><![CDATA[58.1]]></forecast>
<previous><![CDATA[58.3]]></previous>
</event>

如何打印 cdata 的值?

最佳答案

您似乎弄错了解析器的名称。您正在解析 XML 文档,因此需要使用 lxml-xml 而不是 lxml

尝试更换

soup = BeautifulSoup(r.text , 'lxml')

soup = BeautifulSoup(r.text , 'lxml-xml')

get_news_calendar 函数进行此更改后,我在示例 XML 文件上运行它时得到以下输出:

German Flash Manufacturing PMI EUR <date>04-21-2017</date> 7:30am Medium 58.1 58.3

关于python xml 解析cdata,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43500872/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com