beautifulsoup 不打印链接

beautifulsoup not printing links

我正在废弃 rss

from bs4 import BeautifulSoup
import urllib2
import requests


url = raw_input("");
re=requests.get(url);

def rss_get_items(url):    
    request = urllib2.Request(url)
    response = urllib2.urlopen(request)
    soup = BeautifulSoup(response)

    for item_node in soup.find_all('item'):
        item = {}
        for subitem_node in item_node.findChildren():
            key = subitem_node.name
            value = subitem_node.text
            item[key] = value
        yield item

if __name__ == '__main__':
    for item in rss_get_items(url):
        print item['title']
        print item['pubdate']
        print item['link']
        print item['guid']
        print item['description']

我从这个网站上 post 的回答中得到了这个脚本的一部分,我只是给这个人点赞。我忘记了原始 post 和 post 编辑它的用户名。无论如何我不能打印链接,它就是不起作用,我想知道为什么。

我可以按照文档进行操作

for link in soup.find_all('a'):
    print(link.get('href'))
# http://example.com/elsie
# http://example.com/lacie
# http://example.com/tillie

这可行,但出于好奇,我只想知道第一种方法是否适用于打印链接,只是出于好奇。

我正在使用 aljazeera.com rss

当您抓取 xml 内容时,请使用 xml 解析器来创建您的汤。

soup = BeautifulSoup(response, 'xml')