2016-04-30 6 views
0

ニュースソースへのRSSフィードがあります。ニューステキストやその他のメタデータの中には、RSS形式のコメントセクションへのURL参照も含まれています。私はニュース記事ごとにコメントセクションの内容をダウンロードして追加したいと思っています。私の目標は、RSSに含まれている記事ごとに記事とコメントを含むRSSフィードを作成し、この新しいRSSを口語でPDFに変換することです。ここで参照したURLをダウンロードしてXMLに含めます

は、例えば、XMLです:

<?xml version="1.0" encoding="UTF-8"?> 
<feed xmlns="http://www.w3.org/2005/Atom"> 
    <entry> 
     <author> 
      <name>Some Author</name> 
      <uri>http://thenews.com</uri> 
     </author> 
     <category term="sports" label="Sports" /> 
     <content type="html">This is the news text.</content> 
     <id>123abc</id> 
     <link href="http://thenews.com/article/123abc/comments" /> 
     <updated>2016-04-29T13:44:00+00:00</updated> 
     <title>The Title</title> 
    </entry> 
    <entry> 
     <author> 
      <name>Some other Author</name> 
      <uri>http://thenews.com</uri> 
     </author> 
     <category term="sports" label="Sports" /> 
     <content type="html">This is another news text.</content> 
     <id>123abd</id> 
     <link href="http://thenews.com/article/123abd/comments" /> 
     <updated>2016-04-29T14:46:00+00:00</updated> 
     <title>The other Title</title> 
    </entry> 
</feed> 

今私はURLの内容と<リンクのhref = "http://thenews.com/article/123abc/comments"/>を交換したいです。 RSSフィードは、URLの最後に/ rssを追加することで取得できます。最終的には、1つのエントリは次のようになります。

<entry> 
    <author> 
    <name>Some Author</name> 
    <uri>http://thenews.com</uri> 
    </author> 
    <category term="sports" label="Sports" /> 
    <content type="html">This is the news text.</content> 
    <id>123abc</id> 
    <comments> 
    <comment>  
     <author>A commenter</author> 
     <timestamp>2016-04-29T16:00:00+00:00</timestamp> 
     <text>Cool story, yo!</text> 
    </comment> 
    <comment> 
     <author>Another commenter</author> 
     <timestamp>2016-04-29T16:01:00+00:00</timestamp> 
     <text>This is interesting news.</text> 
    </comment> 
    </comments> 
    <updated>2016-04-29T13:44:00+00:00</updated> 
    <title>The Title</title> 
</entry> 

私はどのプログラミング言語にも対応しています。私はpythonとlxmlでこれを試しましたが、遠くには届きませんでした。コメントURLを抽出してコメントフィードをダウンロードできましたが、実際の<リンク>タグを置き換えることができませんでした。 実際のRSSをダウンロードすることなく、ここに私が来てどのくらいです:href属性からXMLをダウンロードし、新しいElementにXMLを解析し、各<link>要素について

import lxml.etree as et 
import urllib2 
import re 

# These will be downloaded from the RSS feed source when the code works 
xmltext = """[The above news feed, too long to paste]""" 
commentsRSS = """[The above comments feed]""" 

hdr = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'} 

article = et.fromstring(xmltext) 

for elem in article.xpath('//feed/entry'): 
    commentsURL = elem.xpath('link/@href') 

    #request = urllib2.Request(commentsURL[0] + '.rss', headers=hdr) 
    #comments = urllib2.urlopen(request).read() 
    comments = commentsRSS 

    # Now the <link>-tag should be replaced by the comments feed without the <?xml ...> tag 

答えて

1

を。対応する新しいElement<link>を置き換え、このような何かその後:

.... 
article = et.fromstring(xmltext) 
ns = {'d': 'http://www.w3.org/2005/Atom'} 
for elem in article.xpath('//d:feed/d:entry/d:link', namespaces=ns): 
    request = urllib2.Request(elem.attrib['href'] + '.rss', headers=hdr) 
    comments = urllib2.urlopen(request).read() 
    newElem = et.fromstring(comments) 
    elem.getparent().replace(elem, newElem) 

# print the result 
print et.tostring(article) 
+0

はどうもありがとうございまし – soner

+0

@sonerおっと(あなたがエラーになった「ルート」への「記事」に変更を除く)、これは完全にうまく働きました。 ..修正済み – har07

関連する問題