3. 1子节点和子孙节点
soup.body.h1# 选中body 标签下的h1,这个h1 标签是body标签的子节点
同理,soup.div.find_all(‘img’)会找到所有div里面的img标签。
.children 和.descendants
对比代码如下:
html = urlopen('http://www.pythonscraping.com/pages/page3.html')
soup = BeautifulSoup(html, 'lxml')
children = soup.find('table',{
'id':'giftList'}).children
descendants = soup.find('table',{
'id':'giftList'}).descendants
sum =

本文探讨了使用BeautifulSoup在Python爬虫中如何获取子节点和子孙节点,通过.children和.descendants的区别展示它们在查找元素时的不同。同时,介绍了查询父节点的方法,包括.parents和.parent的使用示例。
最低0.47元/天 解锁文章
子孙节点(.children .descendants)和父节点.parents&spm=1001.2101.3001.5002&articleId=79340687&d=1&t=3&u=2f4cc6eab0734cbfb178c5732480e3f9)
2339

被折叠的 条评论
为什么被折叠?



