beautifulsoup不打印链接

林诺布

我正在报废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']

我从此站点上发布的答案中获得了脚本的脚本部分,我只是在给家伙加分。我忘记了原始帖子以及发布它的用户名。无论如何,我无法打印链接,但它却无法正常工作,我想知道为什么。

我可以按照文档进行操作

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

Salmanwahed

在抓取xml内容时,请使用xml解析器创建汤。

soup = BeautifulSoup(response, 'xml')

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章