2017-04-16 103 views
0

我试图解析一个新闻网站的RSS源,并提取出版日期,标题,描述和链接到实际文章。因此,与这些代码行远:循环播放bs4.element.tag

with open('text.txt', 'r', encoding='utf-8') as f: 
    soup = bs4.BeautifulSoup(f, 'lxml') 
    all_item_tags = soup.find_all('item') 
    first = all_item_tags[0] 
    second = all_item_tags[1] 
    print(first.contents[9].contents[0], first.contents[1].contents[0], first.contents[4], first.contents[5].contents[0]) 
    print(second.contents[9].contents[0], second.contents[1].contents[0], second.contents[4], second.contents[5].contents[0]) 

我得到的信息,但我无法搞清楚如何循环它抓住的all_item_tags各项指标,然后将这些指数的.contents[].contents[]而无需编写firstsecondthird

编辑:的text.txt内容 - http://www.dailymail.co.uk/home/index.rss

+0

你能提供text.txt吗? – pregmatch

+0

如何使用all_item_tags中的item_tag? –

+0

它工作ty! item_tag in all_item_tags –

回答

0

从评论部分:

for item_tag in all_item_tags怎么样? – t.m.adam