python html解析器未返回链接
我一直在尝试解析rss新闻提要,我设法获得了大部分字段,但没有找到文章的链接和发布日期。 这是我的代码:python html解析器未返回链接,python,html,parsing,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/austral
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)
导入bs4
从bs4进口美汤作为汤
从urllib.request导入urlopen
进口稀土
#将xml.etree.ElementTree作为ET导入
rss_url=”https://news.google.com/news/rss/search/section/q/australia/australia?hl=en-AU&gl=AU&ned=AU“
Client=urlopen(rss\u url)
xml_page=Client.read()
Client.close()
soup\u page=soup(xml\u页面,“html.parser”)
#soup\u page=ET.parse(xml\u页面)
新闻列表=汤页面。findAll(“项目”)
#打印新闻标题、url和发布日期
有关新闻列表中的新闻:
#text=news.text
title=news.title.text
link=news.link.text
pubdate=news.pubdate.text
description=news.description.text
publisher=re.findall(“(.*?”,说明)
文章链接=链接
文章信息=[标题、出版商、链接、发布日期]
打印(文章信息)
除了pubdate和link,我得到了大多数字段。你知道有什么可以帮忙吗?非常感谢 关于
pubDate
和链接
字段:
pubDate
字段可以使用所有小写字母进行检索:
pubdate=news.pubdate.text
先前版本的Beauty Soup 4.5.3正确捕获了链接
字段,但在当前版本4.6.0中未捕获该字段。在你看到的空白行中出现4.0的结果。按照以下要求安装4.5.3:
$ pip3 uninstall beautifulsoup4
$ pip3 install 'beautifulsoup4==4.5.3'
以下是美丽的汤发布历史。4.5.3于2017年1月2日发布,4.6.0于2017年5月7日发布
[《联盟党关于国家能源保障的分歧——政治现场》,[《卫报》],'https://www.theguardian.com/australia-news/live/2018/may/29/nationals-barnaby-joyce-superannuation-coalition-banking-royal-commission-tax-politics-live“,”2018年5月28日星期一22:37:07 GMT']
[“澳大利亚的住宅租赁协议落后于世界其他地区:租户联盟”,[“ABC在线”],'http://www.abc.net.au/news/2018-05-29/residential-rental-agreements-in-australia-need-updating/9809364“,”2018年5月28日星期一19:39:43 GMT']
您从pubdate和link获得了什么?它们是空白的吗?你有错误吗?我返回了空白,没有错误。谢谢你的pubdate提示!现在可以了。但link对我来说仍然不起作用。它仍然返回空白…Beautiful Soup 4.6.0存在问题。降级至4.5.3工程。我已经用pip3
命令更新了答案,以卸载4.6.0并安装4.5.3。我尝试用pip安装4.5.3版本,但它一直说这是一个无效的要求…即使是soup版本4.5.3也无法解决链接问题。您使用的是哪一版本的Python和哪一个OS/版本?我正在macOS上使用Python 3.6.0。