Python 如何使用bs4浏览html?
今天下午刚开始学习python。尝试从kubuntu.org(简单html)的rss提要中获取信息,但我不知道如何浏览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
#!/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