Beautiful Soup Nested Tag Search

后端 未结 3 640
孤城傲影
孤城傲影 2021-01-12 04:35

I am trying to write a python program that will count the words on a web page. I use Beautiful Soup 4 to scrape the page but I have difficulties accessing nested HTML tags (

3条回答
  •  感动是毒
    2021-01-12 05:14

    Maybe I'm guessing what you are trying to do is first looking in a specific div tag and the search all p tags in it and count them or do whatever you want. For example:

    soup = bs4.BeautifulSoup(content, 'html.parser') 
    
    # This will get the div
    div_container = soup.find('div', class_='some_class')  
    
    # Then search in that div_container for all p tags with class "hello"
    for ptag in div_container.find_all('p', class_='hello'):
        # prints the p tag content
        print(ptag.text)
    

    Hope that helps

提交回复
热议问题