我有一個巨大的URL列表,我的任務是將它們提供給一個python腳本,如果有的話應該吐出feed URL。有沒有可以幫助的API庫或代碼?自動從網頁中提取供稿鏈接(原子,rss等)
4
A
回答
3
我在推薦Beautiful Soup來解析HTML,然後得到<鏈接rel =「alternate」標籤,其中的feed被引用的第二個華夫餅乾悖論。該代碼我通常使用:
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
我不知道任何現有的庫,但Atom或RSS提要通常與<link>
標籤顯示在<head>
節這樣:
<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的用HTML解析器,如lxml.html,並獲取相關<link>
標記的href
屬性。
1
取決於良好的形成在這些飼料中的信息是如何(比如,是否在http://.../
形式的所有環節嗎?你知道,如果他們都將在href
或link
標籤?在飼料的所有鏈接去其他的飼料?等),我會推薦從簡單的正則表達式到直接的解析模塊從提取飼料中提取鏈接。我只能推薦beautiful soup。儘管即使是最好的解析器也只會走得這麼遠 - 尤其是在上面提到的情況下,如果不能保證數據中的所有鏈接都將鏈接到其他提要;那麼你必須自己做一些額外的抓取和探測。
7
>>> 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. 如何從jQuery中的博客獲取rss供稿鏈接
- 2. 自動搜索供Rss供稿
- 3. 2頭部RSS供稿鏈接?
- 4. 如何提取原子/ RSS
- 5. 從網頁/博客提取原子
- 6. 從網頁提取所有鏈接
- 7. 從cefsharp上的網頁提取鏈接
- 8. 製作:manpage:提供鏈接到網頁
- 9. 讓RSS在我的網站供稿
- 10. jQuery - 從動態RSS提要鏈接將外部網頁加載到div
- 11. 使用Yahoo Pipes從RSS提要中提取超鏈接
- 12. 從Google快訊鏈接中提取原始網址
- 13. 如何檢查Rss供稿鏈接是否有效
- 14. 從Google電子表格中繪製xml/rss供稿(s)?
- 15. 重寫RSS供稿中的URL以用於自動博文
- 16. 刪除鏈接從Instagram圖片供稿
- 17. RSS原子:鏈接 - 自我引用不匹配文檔位置
- 18. 從網站中提取鏈接
- 19. 從網站鏈接中提取
- 20. 這是rss供稿嗎?
- 21. 新聞供稿網頁
- 22. 解析JSON供稿網頁
- 23. 無法在少數網站中找到rss供稿網址
- 24. 在c#中使用htmlagilitypack從網頁中提取鏈接和帖子的方法?
- 25. 提取與Nokogiri的RSS鏈接
- 26. 如何從網頁中提取句子
- 27. 使用syndicationitem獲取自定義rss供稿項目元素?
- 28. 從網站上的RSS提供的RSS源
- 29. 使用Go lang從網頁中提取鏈接
- 30. 如何查找並從網頁中提取鏈接?
feedfinder不再維持,但現在有['feedfinder2'](https://pypi.python.org/pypi/ feedfinder2)。 – Scarabee