python - bs4 .children打印出来的结果为什么是这样?

查看:187
本文介绍了python - bs4 .children打印出来的结果为什么是这样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

最近在练习写爬虫,在WebScrapingwithPython一书第20页中的一个例子:

from urllib.request import urlopen

from bs4 import BeautifulSoup

html = urlopen("http://www.pythonscraping.com/pages/page3.html")

bsObj = BeautifulSoup(html)

for child in bsObj.find("table",{"id":"giftList"}).children:
    print(child)

我把.children换成.descendants:

for child in bsObj.find('table',{'id':'giftList'}).descendants:
    print(child)

打印出来的结果为什么会有这些重复信息,比如这个:

<tr><th>
Item Title
</th><th>
Description
</th><th>
Cost
</th><th>
Image
</th></tr>
<th>
Item Title
</th>

Item Title

<th>
Description
</th>

Description

<th>
Cost
</th>

Cost

<th>
Image
</th>

Image

问题:
1.为什么Item Title,Description,Cost,Image这些字出现了两次呢?
2.通过这个例子还是不太明白children和descendants的区别T.T哪位高人点拨下,谢谢

解决方案

文档不是说得挺清楚了吗,多看几次文档就理解
链接描述

这篇关于python - bs4 .children打印出来的结果为什么是这样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆