博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
beautifulsoup 的children和descandants
阅读量:6902 次
发布时间:2019-06-27

本文共 4645 字,大约阅读时间需要 15 分钟。

之前看爬虫的时候,看到这里就断了,一直不太理解这2个的区别。

今天重新看,也借助了这位哥们的方法,把结果打印出来,我大概知道了这2者的区别。

http://www.cnblogs.com/chensimin1990/p/6725803.html

 

--------------------------------

from urllib.request import urlopenfrom bs4 import BeautifulSouphtml = urlopen("http://www.pythonscraping.com/pages/page3.html")bs_obj = BeautifulSoup(html,'html.parser')# name_list = bs_obj.find_all("span", {"class":"green"})#for name in name_list:#    print(name.get_text())  # file = open('test.txt','w')# content = ''for child in bs_obj.find("table",{
"id":"giftList"}).descendants: print(child)

代码是这样的

------------------------------------------------

Item TitleDescriptionCostImageItem TitleItem TitleDescriptionDescriptionCostCostImageImageVegetable BasketThis vegetable basket is the perfect gift for your health conscious (or overweight) friends!Now with super-colorful bell peppers!$15.00Vegetable BasketVegetable BasketThis vegetable basket is the perfect gift for your health conscious (or overweight) friends!Now with super-colorful bell peppers!This vegetable basket is the perfect gift for your health conscious (or overweight) friends!Now with super-colorful bell peppers!Now with super-colorful bell peppers!$15.00$15.00Russian Nesting DollsHand-painted by trained monkeys, these exquisite dolls are priceless! And by "priceless," we mean "extremely expensive"! 8 entire dolls per set! Octuple the presents!$10,000.52Russian Nesting DollsRussian Nesting DollsHand-painted by trained monkeys, these exquisite dolls are priceless! And by "priceless," we mean "extremely expensive"! 8 entire dolls per set! Octuple the presents!Hand-painted by trained monkeys, these exquisite dolls are priceless! And by "priceless," we mean "extremely expensive"!8 entire dolls per set! Octuple the presents!8 entire dolls per set! Octuple the presents!$10,000.52$10,000.52Fish PaintingIf something seems fishy about this painting, it's because it's a fish! Also hand-painted by trained monkeys!$10,005.00Fish PaintingFish PaintingIf something seems fishy about this painting, it's because it's a fish! Also hand-painted by trained monkeys!If something seems fishy about this painting, it's because it's a fish!Also hand-painted by trained monkeys!Also hand-painted by trained monkeys!$10,005.00$10,005.00Dead ParrotThis is an ex-parrot! Or maybe he's only resting?$0.50Dead ParrotDead ParrotThis is an ex-parrot! Or maybe he's only resting?This is an ex-parrot!Or maybe he's only resting?Or maybe he's only resting?$0.50$0.50Mystery BoxIf you love suprises, this mystery box is for you! Do not place on light-colored surfaces. May cause oil staining. Keep your friends guessing!$1.50Mystery BoxMystery BoxIf you love suprises, this mystery box is for you! Do not place on light-colored surfaces. May cause oil staining. Keep your friends guessing!If you love suprises, this mystery box is for you! Do not place on light-colored surfaces. May cause oil staining.Keep your friends guessing!Keep your friends guessing!$1.50$1.50

这是结果

 

用children的函数(?不知道为什么叫函数,感觉没有括号,明明是字段啊...)

from urllib.request import urlopenfrom bs4 import BeautifulSouphtml = urlopen("http://www.pythonscraping.com/pages/page3.html")bs_obj = BeautifulSoup(html,'html.parser')# name_list = bs_obj.find_all("span", {"class":"green"})#for name in name_list:#    print(name.get_text())  # file = open('test.txt','w')# content = ''for child in bs_obj.find("table",{
"id":"giftList"}).children: print(child)

结果是这样的:

Item TitleDescriptionCostImageVegetable BasketThis vegetable basket is the perfect gift for your health conscious (or overweight) friends!Now with super-colorful bell peppers!$15.00Russian Nesting DollsHand-painted by trained monkeys, these exquisite dolls are priceless! And by "priceless," we mean "extremely expensive"! 8 entire dolls per set! Octuple the presents!$10,000.52Fish PaintingIf something seems fishy about this painting, it's because it's a fish! Also hand-painted by trained monkeys!$10,005.00Dead ParrotThis is an ex-parrot! Or maybe he's only resting?$0.50Mystery BoxIf you love suprises, this mystery box is for you! Do not place on light-colored surfaces. May cause oil staining. Keep your friends guessing!$1.50

------------------

到说明的时候:

1、chidlren并不是只返回子代的第一层,而是到没有子代的那一层,也就是说会穿透所有的,这个我以前以为是descendants干的事。

2、那descendants还留着干嘛呢?

是这么一个作用,他对每一个子代都会遍历一边他所有的后代。

如果我们打个比方:

a

-a1

--a11

--a12

--a13

---a131

----a1311

如果用children,其实就是原样返回,如果用descendants的话,他会在a13的时候返回一次a1311,a131的时候又返回一次a1311。

另外

for child in bs_obj.find("table",{"id":"giftList"}).children 和 
for child in bs_obj.find("table",{"id":"giftList"})是等价的,想想也知道,这个更符合一般人的直觉。

转载于:https://www.cnblogs.com/onhacker/p/7220817.html

你可能感兴趣的文章
ORACLE常见的六种RMAN恢复测试
查看>>
(Portal 开发读书笔记) Personalization
查看>>
SRCNN 实验细节
查看>>
Java多线程第二节-线程的基本使用
查看>>
界面控件Essential Studio for ASP.NET Web Forms 2017 v3发布丨附下载
查看>>
教你制作属于自己的CentOS 6.4一键自动化安装ISO镜像光盘
查看>>
线上 mysql 主库配置文档
查看>>
Java web部署目录结构和web.xml作用
查看>>
负载均衡DR(direct routing)模式
查看>>
Python中list的遍历
查看>>
Linux下查看内存等硬件信息
查看>>
mysql 登录权限
查看>>
Java之内部类
查看>>
怎样使用python帮助手册
查看>>
我的友情链接
查看>>
UITabelView的分割线处理及介绍
查看>>
Spring整合MyBatis
查看>>
打印机拒绝访问无法连接
查看>>
Linux学习路线图
查看>>
Linux系统下常见性能分析工具的使用
查看>>