Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
401 views
in Technique[技术] by (71.8m points)

python - Beautiful Soup find children for particular div

I have am trying to parse a webpage that looks like this with Python->Beautiful Soup:enter image description here

I am trying to extract the contents of the highlighted td div. Currently I can get all the divs by

alltd = soup.findAll('td')

   
for td in alltd:
    print td

But I am trying to narrow the scope of that to search the tds in the class "tablebox" which still will probably return 30+ but is more managable a number than 300+.

How can I extract the contents of the highlighted td in picture above?

question from:https://stackoverflow.com/questions/13202087/beautiful-soup-find-children-for-particular-div

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It is useful to know that whatever elements BeautifulSoup finds within one element still have the same type as that parent element - that is, various methods can be called.

So this is somewhat working code for your example:

soup = BeautifulSoup(html)
divTag = soup.find_all("div", {"class": "tablebox"})

for tag in divTag:
    tdTags = tag.find_all("td", {"class": "align-right"})
    for tag in tdTags:
        print tag.text

This will print all the text of all the td tags with the class of "align-right" that have a parent div with the class of "tablebox".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...