以前看爬蟲的時候,看到這裏就斷了,一直不太理解這2個的區別。html
今天從新看,也藉助了這位哥們的方法,把結果打印出來,我大概知道了這2者的區別。python
http://www.cnblogs.com/chensimin1990/p/6725803.htmlapi
--------------------------------less
from urllib.request import urlopen from bs4 import BeautifulSoup html = 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)
代碼是這樣的函數
------------------------------------------------ui
<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 <tr class="gift" id="gift1"><td> Vegetable Basket </td><td> This vegetable basket is the perfect gift for your health conscious (or overweight) friends! <span class="excitingNote">Now with super-colorful bell peppers!</span> </td><td> $15.00 </td><td> <img src="../img/gifts/img1.jpg"/> </td></tr> <td> Vegetable Basket </td> Vegetable Basket <td> This vegetable basket is the perfect gift for your health conscious (or overweight) friends! <span class="excitingNote">Now with super-colorful bell peppers!</span> </td> This vegetable basket is the perfect gift for your health conscious (or overweight) friends! <span class="excitingNote">Now with super-colorful bell peppers!</span> Now with super-colorful bell peppers! <td> $15.00 </td> $15.00 <td> <img src="../img/gifts/img1.jpg"/> </td> <img src="../img/gifts/img1.jpg"/> <tr class="gift" id="gift2"><td> Russian Nesting Dolls </td><td> Hand-painted by trained monkeys, these exquisite dolls are priceless! And by "priceless," we mean "extremely expensive"! <span class="excitingNote">8 entire dolls per set! Octuple the presents!</span> </td><td> $10,000.52 </td><td> <img src="../img/gifts/img2.jpg"/> </td></tr> <td> Russian Nesting Dolls </td> Russian Nesting Dolls <td> Hand-painted by trained monkeys, these exquisite dolls are priceless! And by "priceless," we mean "extremely expensive"! <span class="excitingNote">8 entire dolls per set! Octuple the presents!</span> </td> Hand-painted by trained monkeys, these exquisite dolls are priceless! And by "priceless," we mean "extremely expensive"! <span class="excitingNote">8 entire dolls per set! Octuple the presents!</span> 8 entire dolls per set! Octuple the presents! <td> $10,000.52 </td> $10,000.52 <td> <img src="../img/gifts/img2.jpg"/> </td> <img src="../img/gifts/img2.jpg"/> <tr class="gift" id="gift3"><td> Fish Painting </td><td> If something seems fishy about this painting, it's because it's a fish! <span class="excitingNote">Also hand-painted by trained monkeys!</span> </td><td> $10,005.00 </td><td> <img src="../img/gifts/img3.jpg"/> </td></tr> <td> Fish Painting </td> Fish Painting <td> If something seems fishy about this painting, it's because it's a fish! <span class="excitingNote">Also hand-painted by trained monkeys!</span> </td> If something seems fishy about this painting, it's because it's a fish! <span class="excitingNote">Also hand-painted by trained monkeys!</span> Also hand-painted by trained monkeys! <td> $10,005.00 </td> $10,005.00 <td> <img src="../img/gifts/img3.jpg"/> </td> <img src="../img/gifts/img3.jpg"/> <tr class="gift" id="gift4"><td> Dead Parrot </td><td> This is an ex-parrot! <span class="excitingNote">Or maybe he's only resting?</span> </td><td> $0.50 </td><td> <img src="../img/gifts/img4.jpg"/> </td></tr> <td> Dead Parrot </td> Dead Parrot <td> This is an ex-parrot! <span class="excitingNote">Or maybe he's only resting?</span> </td> This is an ex-parrot! <span class="excitingNote">Or maybe he's only resting?</span> Or maybe he's only resting? <td> $0.50 </td> $0.50 <td> <img src="../img/gifts/img4.jpg"/> </td> <img src="../img/gifts/img4.jpg"/> <tr class="gift" id="gift5"><td> Mystery Box </td><td> If you love suprises, this mystery box is for you! Do not place on light-colored surfaces. May cause oil staining. <span class="excitingNote">Keep your friends guessing!</span> </td><td> $1.50 </td><td> <img src="../img/gifts/img6.jpg"/> </td></tr> <td> Mystery Box </td> Mystery Box <td> If you love suprises, this mystery box is for you! Do not place on light-colored surfaces. May cause oil staining. <span class="excitingNote">Keep your friends guessing!</span> </td> If you love suprises, this mystery box is for you! Do not place on light-colored surfaces. May cause oil staining. <span class="excitingNote">Keep your friends guessing!</span> Keep your friends guessing! <td> $1.50 </td> $1.50 <td> <img src="../img/gifts/img6.jpg"/> </td> <img src="../img/gifts/img6.jpg"/>
這是結果this
用children的函數(?不知道爲何叫函數,感受沒有括號,明明是字段啊...)url
from urllib.request import urlopen from bs4 import BeautifulSoup html = 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)
結果是這樣的:spa
<tr><th> Item Title </th><th> Description </th><th> Cost </th><th> Image </th></tr> <tr class="gift" id="gift1"><td> Vegetable Basket </td><td> This vegetable basket is the perfect gift for your health conscious (or overweight) friends! <span class="excitingNote">Now with super-colorful bell peppers!</span> </td><td> $15.00 </td><td> <img src="../img/gifts/img1.jpg"/> </td></tr> <tr class="gift" id="gift2"><td> Russian Nesting Dolls </td><td> Hand-painted by trained monkeys, these exquisite dolls are priceless! And by "priceless," we mean "extremely expensive"! <span class="excitingNote">8 entire dolls per set! Octuple the presents!</span> </td><td> $10,000.52 </td><td> <img src="../img/gifts/img2.jpg"/> </td></tr> <tr class="gift" id="gift3"><td> Fish Painting </td><td> If something seems fishy about this painting, it's because it's a fish! <span class="excitingNote">Also hand-painted by trained monkeys!</span> </td><td> $10,005.00 </td><td> <img src="../img/gifts/img3.jpg"/> </td></tr> <tr class="gift" id="gift4"><td> Dead Parrot </td><td> This is an ex-parrot! <span class="excitingNote">Or maybe he's only resting?</span> </td><td> $0.50 </td><td> <img src="../img/gifts/img4.jpg"/> </td></tr> <tr class="gift" id="gift5"><td> Mystery Box </td><td> If you love suprises, this mystery box is for you! Do not place on light-colored surfaces. May cause oil staining. <span class="excitingNote">Keep your friends guessing!</span> </td><td> $1.50 </td><td> <img src="../img/gifts/img6.jpg"/> </td></tr>
------------------rest
到說明的時候:
一、chidlren並非只返回子代的第一層,而是到沒有子代的那一層,也就是說會穿透全部的,這個我之前覺得是descendants乾的事。
二、那descendants還留着幹嗎呢?
是這麼一個做用,他對每個子代都會遍歷一邊他全部的後代。
若是咱們打個比方:
a
-a1
--a11
--a12
--a13
---a131
----a1311
若是用children,其實就是原樣返回,若是用descendants的話,他會在a13的時候返回一次a1311,a131的時候又返回一次a1311。
另外