samedi 30 octobre 2021

BeautifulSoup children() vs descendants()

I read a book that has the following:


from urllib.request import urlopen

from bs4 import BeautifulSoup

html = urlopen('https://ift.tt/3CuSdBb)

bs = BeautifulSoup(html, 'html.parser')

for child in bs.find('table',{'id':'giftList'}).children:

print(child)

This code prints the list of product rows in the giftList table, including the initial row of column labels. If you were to write it using the descendants() function instead of the children() function, about two dozen tags would be found within the table and printed, including img tags, span tags, and individual td tags.


I tested it and I did not see the two outputs had difference when using .children or .descendants. Can anyone please tell me what exactly it will print when using .children and using .descendants.




Aucun commentaire:

Enregistrer un commentaire