Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用bs4浏览html?_Python_Html_Python 3.x - Fatal编程技术网

Python 如何使用bs4浏览html?

Python 如何使用bs4浏览html?,python,html,python-3.x,Python,Html,Python 3.x,今天下午刚开始学习python。尝试从kubuntu.org(简单html)的rss提要中获取信息,但我不知道如何浏览html,只打印提要标题: #!/usr/bin/python3.5 import bs4 as bs import urllib.request site = urllib.request.urlopen('https://kubuntu.org/feed').read() soup = bs.BeautifulSoup(site, 'lxml') for title in

今天下午刚开始学习python。尝试从kubuntu.org(简单html)的rss提要中获取信息,但我不知道如何浏览html,只打印提要标题:

#!/usr/bin/python3.5
import bs4 as bs
import urllib.request

site = urllib.request.urlopen('https://kubuntu.org/feed').read()
soup = bs.BeautifulSoup(site, 'lxml')

for title in soup.find_all('item'):
    print(title.text)
编辑:

title
添加到
find_all
行可以满足我的需求,但仍有大量数据使用title标记

#!/usr/bin/python3.5
import bs4 as bs
import urllib.request

site = urllib.request.urlopen('https://kubuntu.org/feed').read()
soup = bs.BeautifulSoup(site, 'lxml')

for title in soup.find_all(['item', 'title']):
    print(title.text)

只需访问
标题
标记为
项目
的子节点:

...
for item in soup.find_all('item'):
    print(item.title.text)
输出:

Kubuntu Artful Aardvark (17.10) Alpha 1
Latest round of backports PPA updates include Plasma 5.10.2 for Zesty 17.04
Plasma 5.10.1 now in Zesty backports
17.10 Wallpaper Contest deadline for submissions soon
Plasma bugfix releases, Frameworks, & selected app updates now available in backports PPA for Zesty and Xenial
17.10 Wallpaper Contest! Call for artists
KDE PIM update now available for Zesty Zapus 17.04
KDE PIM update for Zesty available for testers
Kubuntu 17.04 Released!
Kubuntu 17.04 Release Candidate – call for testers