python html解析器没有返回链接

时间:2018-05-29 03:20:38

标签: python html parsing

我一直在尝试解析rss新闻源,但我设法获得了大部分字段,但链接到了文章和发布日期。 这是我的代码:

import bs4
from bs4 import BeautifulSoup as soup
from urllib.request import urlopen
import re
#import xml.etree.ElementTree as ET

rss_url="https://news.google.com/news/rss/search/section/q/australia/australia?hl=en-AU&gl=AU&ned=au"
Client=urlopen(rss_url)
xml_page=Client.read()
Client.close()
soup_page=soup(xml_page,"html.parser")
#soup_page=ET.parse(xml_page)
news_list=soup_page.findAll("item")
# Print news title, url and publish date
for news in news_list:
  #text=news.text
  title=news.title.text
  link=news.link.text
  pubdate=news.pubDate.text
  description=news.description.text
  publisher = re.findall('<font color="#6f6f6f">(.*?)</font>', description)
  article_link=link
  article_info=[title,publisher,link,pubdate]
  print(article_info)

我得到的大部分字段都是发布和链接。知道什么可以帮助吗?非常感谢!

1 个答案:

答案 0 :(得分:0)

关于pubDatelink字段:

可以使用全部小写来检索pubDate字段:

pubdate=news.pubdate.text

使用以前版本的Beautiful Soup 4.5.3正确捕获link字段,但当前版本4.6.0中没有。 4.6.0结果显示您看到的空白行。使用以下内容安装4.5.3:

$ pip3 uninstall beautifulsoup4
$ pip3 install 'beautifulsoup4==4.5.3'

这是Beautiful Soup发布历史。 4.5.3于2017年1月2日发布,4.6.0于2017年5月7日发布。

我在macOS上使用Python 3.6.0。

以下是显示所有字段的更新的前两行。

['Coalition party room split over national energy guarantee – politics live', ['The Guardian'], 'https://www.theguardian.com/australia-news/live/2018/may/29/nationals-barnaby-joyce-superannuation-coalition-banking-royal-commission-tax-politics-live', 'Mon, 28 May 2018 22:37:07 GMT']

['Residential rental agreements in Australia falling behind rest of the world: tenants union', ['ABC Online'], 'http://www.abc.net.au/news/2018-05-29/residential-rental-agreements-in-australia-need-updating/9809364', 'Mon, 28 May 2018 19:39:43 GMT']

相关问题