我正在寻找一个代码片段(语言在这里并不重要),它将提取与此页面关联的所有提要(RSS、原子等)。
所以 input isURL和 output list of channels。
重要的是完整性,这意味着如果页面关联了一些信息通道,则应该找到它。
我最好询问在 HTML 代码中找到什么以及在哪里找到以涵盖完整性。
谢谢你
我正在寻找一个代码片段(语言在这里并不重要),它将提取与此页面关联的所有提要(RSS、原子等)。
所以 input isURL和 output list of channels。
重要的是完整性,这意味着如果页面关联了一些信息通道,则应该找到它。
我最好询问在 HTML 代码中找到什么以及在哪里找到以涵盖完整性。
谢谢你
head您可以在 html 文件的标签中找到提要。在那里,它们应该被指定为link具有关联内容类型和指定其位置的 href 属性的标签。
要使用 python 从页面中提取所有提要 URL,您可以使用以下内容:
import urllib
from HTMLParser import HTMLParser
class FeedParser(HTMLParser):
def __init__(self, *args, **kwargs):
self.feeds = set()
HTMLParser.__init__(self, *args, **kwargs)
def handle_starttag(self, tag, attrs):
if tag == 'link':
try:
href = [attr[1] for attr in attrs if attr[0] == 'href'][0]
except IndexError:
return None
else:
if ('type', 'application/atom+xml') in attrs or ('type', 'application/rss+xml') in attrs:
self.feeds.add(href)
def get_all_feeds_from_url(url):
f = urllib.urlopen(url)
contents = f.read()
f.close()
parser = FeedParser()
parser.feed(contents)
parser.close()
return list(parser.feeds)
This code would have to be extended quite a bit though if you want to cover all the quirky ways a feed can be added to a html page.