之前看爬虫的时候,看到这里就断了,一直不太理解这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"})是等价的,想想也知道,这个更符合一般人的直觉。