2011-10-25 1 views

答えて

3

私は、HTMLを解析してからフィードを参照する<リンクrel = "alternate" >というタグを取得するために、Beautiful Soupをお勧めしていて、2番目のワッフルパラドックスです。コード私は通常使用:

from BeautifulSoup import BeautifulSoup as parser 

def detect_feeds_in_HTML(input_stream): 
    """ examines an open text stream with HTML for referenced feeds. 

    This is achieved by detecting all ``link`` tags that reference a feed in HTML. 

    :param input_stream: an arbitrary opened input stream that has a :func:`read` method. 
    :type input_stream: an input stream (e.g. open file or URL) 
    :return: a list of tuples ``(url, feed_type)`` 
    :rtype: ``list(tuple(str, str))`` 
    """ 
    # check if really an input stream 
    if not hasattr(input_stream, "read"): 
     raise TypeError("An opened input *stream* should be given, was %s instead!" % type(input_stream)) 
    result = [] 
    # get the textual data (the HTML) from the input stream 
    html = parser(input_stream.read()) 
    # find all links that have an "alternate" attribute 
    feed_urls = html.findAll("link", rel="alternate") 
    # extract URL and type 
    for feed_link in feed_urls: 
     url = feed_link.get("href", None) 
     # if a valid URL is there 
     if url: 
      result.append(url) 
    return result 
3

は、私は、既存のライブラリを知らないが、アトムまたはRSSフィードは、通常のような<head>セクションに<link>タグで示されます

<link rel="alternative" type="application/rss+xml" href="http://link.to/feed"> 
<link rel="alternative" type="application/atom+xml" href="http://link.to/feed"> 

簡単な方法これらのURLのとをダウンロードして解析することになりますlxml.htmlのようなHTMLパーサーで、hrefという属性を適切な<link>というタグで取得します。これらのフィード内の情報だけでなく、形成方法によっては

1

は(例えば、http://.../のフォーム内のすべてのリンクされている?彼らはすべてのフィード内のすべてのリンクはありますか?hrefまたはlinkタグになります場合は、知っていますか他のフィードに行くなど)、単純な正規表現からまっすぐな解析モジュールまで、フィードからのリンクを抽出することをお勧めしたいと思います。

パーシングモジュールのところでは、beautiful soupしかお勧めできません。上記の例の場合、最高のパーサーでさえもこれまでのところ、データのすべてのリンクが他のフィードへのリンクになることを保証できない場合、あなた自身でいくつかの追加のクロールとプロービングを行う必要があります。

7

feedfinderありません:

>>> import feedfinder 
>>> 
>>> feedfinder.feed('scripting.com') 
'http://scripting.com/rss.xml' 
>>> 
>>> feedfinder.feeds('scripting.com') 
['http://delong.typepad.com/sdj/atom.xml', 
'http://delong.typepad.com/sdj/index.rdf', 
'http://delong.typepad.com/sdj/rss.xml'] 
>>> 
+1

feedfinderはもはや維持されているが、今[ 'feedfinder2']あり(https://pypi.python.org/pypi/フィードファインダー2)。 – Scarabee

関連する問題