大佬教程收集整理的这篇文章主要介绍了如何从 <link/> 标签恢复 http 链接,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试从 RSS 页面恢复网络链接。我在 windows 10 系统上使用 python3、请求和 BeautifulSoup4。我的代码如下:
RSS = "http://www.example.com/xml/RSS/all.xml"
mYheaders = {'User-Agent': 'Mozilla/5.0 (X11; linux x86_64; rv:45.0) Gecko/20100101 firefox/45.0'}
sourcePage = requests.get(RSS,headers = mYheaders,timeout=(5,10))
sourceText = sourcePage.text
soup = BeautifulSoup(sourceText,'HTMl.parser')
Articles = soup.findAll('item')
for i in Articles:
title = i.title
link = i.link
Pub = i.pubdate
print('title: ',titlE)
print('link: ',link)
print('Pub: ',Pub)
打印如下:
title: <title>There is some text here</title>
link: <link/>
Pub: <pubdate>Sat,06 Feb 2021 10:22:41 +0000</pubdate>
文章中的单个项目具有以下形式:
<item>
<link/>https://www.example.com/news/2021/2/6/blahblah
<title>Some Title TEXT here</title>
<description><![cdaTA[Some text here' and here.]]></description>
<pubdate>Sat,06 Feb 2021 11:58:23 +0000</pubdate>
<category>News</category>
<guID ispermalink="false">https://www.example.com/?t=1234567</guID>
</item>
问题在于
<link/>
因为它没有以适当的形式被捕获,即
<link>...</link>
当我在浏览器 (FireFox) 中打开同一个链接(上面的 RSS)时,链接标签显示正确:
<item>
<link>
https://www.example.com/blah/blah
</link>
<title>
Some Title TEXT here.
</title>
<description>
Some description here.
</description>
<pubDate>Sun,07 Feb 2021 08:03:48 +0000</pubDate>
<category>News</category>
<guID isPermalink="false">https://www.example.com/?t=123456</guID>
</item>
我猜问题在于对 xml 页面使用 HTMl.parser。如果我需要使用一些 xml 解析器,你能指导我在 python3 上使用哪一个。代码将在 raspBerry pi 上运行,但我正在 windows10 上开发。
提前感谢您的解决方案!
由于<link></link>
标签被转换成<link/>
,所以你需要使用.next_sibling
来获取你需要的链接。代码看起来像这样:
...
for i in Articles:
title = i.title
Link = i.link.next_sibling
Pub = i.pubdate
print('title: ',titlE)
print('Link: ',Link)
print('Pub: ',Pub)
此外,如果您只想获得没有标签的 title 和 Pub,请使用 .text
。
以上是大佬教程为你收集整理的如何从 <link/> 标签恢复 http 链接全部内容,希望文章能够帮你解决如何从 <link/> 标签恢复 http 链接所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。