0

Python + 编程菜鸟在这里,所以你可能不得不忍受我。我有许多 xml 文件(RSS 档案),我想从中提取新闻文章 url。我在 Windows 上使用 Python 2.7.3 ......这是我正在查看的代码示例:

<feed xmlns:media="http://search.yahoo.com/mrss/" xmlns:gr="http://www.google.com/schemas/reader/atom/" xmlns:idx="urn:atom-extension:indexing" xmlns="http://www.w3.org/2005/Atom" idx:index="no" gr:dir="ltr">
<!-- 
Content-type: Preventing XSRF in IE.

 -->
<generator uri="http://www.google.com/reader">Google Reader</generator>
<id>
tag:google.com,2005:reader/feed/http://feeds.smh.com.au/rssheadlines/national.xml
</id>
<title>The Sydney Morning Herald National Headlines</title>
<subtitle type="html">
The top National headlines from The Sydney Morning Herald. For all the news, visit http://www.smh.com.au.
</subtitle>
<gr:continuation>CJPL-LnHybcC</gr:continuation>
<link rel="self" href="http://www.google.com/reader/atom/feed/http://feeds.smh.com.au/rssheadlines/national.xml?n=1000&c=%5BC%5D"/>
<link rel="alternate" href="http://www.smh.com.au/national" type="text/html"/>
<updated>2013-06-16T07:55:56Z</updated>
<entry gr:is-read-state-locked="true" gr:crawl-timestamp-msec="1371369356359">
<id gr:original-id="http://news.smh.com.au/breaking-news-sport/daley-opts-for-dugan-for-origin-two-20130616-2oc5k.html">tag:google.com,2005:reader/item/dabe358abc6c18c5</id>
<category term="user/03956512242887934409/state/com.google/read" scheme="http://www.google.com/reader/" label="read"/>
<title type="html">Daley opts for Dugan for Origin two</title>
<published>2013-06-16T07:12:11Z</published>
<updated>2013-06-16T07:12:11Z</updated>
<link rel="alternate" href="http://rss.feedsportal.com/c/34697/f/644122/s/2d5973e2/l/0Lnews0Bsmh0N0Bau0Cbreaking0Enews0Esport0Cdaley0Eopts0Efor0Edugan0Efor0Eorigin0Etwo0E20A130A6160E2oc5k0Bhtml/story01.htm" type="text/html"/>

具体来说,我想提取“原始ID”链接:

<id gr:original-id="http://news.smh.com.au/breaking-news-sport/daley-opts-for-dugan-for-origin-two-20130616-2oc5k.html">tag:google.com,2005:reader/item/dabe358abc6c18c5</id>

我最初尝试为此使用 BeautifulSoup,但遇到了问题,从我所做的研究看来,元素树是要走的路。首先,我尝试了 ET:

import xml.etree.ElementTree as ET
tree = ET.parse('thefile.xml')
root = tree.getroot()

#first_original_id = root[8][0]

parents_of_interest = root[8::]

for elem in parents_of_interest:
    print elem.items()[0][1]

据我所知,parents_of_interest确实获取了我想要的数据(作为字典列表),但for循环只返回一堆true语句,并且在阅读了文档之后,似乎这是错误的方法。

我认为有我正在寻找的答案,但即使这是一个很好的解释,我似乎也无法将其应用于我自己的情况。从那个答案我试过:

print tree.find('//{http://www.w3.org/2005/Atom}entry}id').text

但是得到了错误:

__main__:1: FutureWarning: This search is broken in 1.3 and earlier, and will be fixed in a future version.  If you rely
 on the current behaviour, change it to './/{http://www.w3.org/2005/Atom}entry}id'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'text'

对此的任何帮助将不胜感激......如果这是一个冗长的问题,我很抱歉......但我想我会详细说明一切......以防万一。

4

1 回答 1

0

您的 xpath 表达式与第一个 id 匹配,而不是您要查找的那个,并且 original-id 是元素的属性,因此您应该编写如下内容:

idelem = tree.find('./{http://www.w3.org/2005/Atom}entry/{http://www.w3.org/2005/Atom}id')
if idelem is not None:
    print idelem.get('{http://www.google.com/schemas/reader/atom/}original-id')

这只会找到第一个匹配的 id,如果你想要它们,使用findall并迭代结果。

于 2013-06-16T18:52:20.740 回答