Use BeautifulSoup to get a value after a specific tag

后端 未结 1 1598
攒了一身酷
攒了一身酷 2021-01-05 12:07

I\'m having a very hard time getting BeautifulSoup to scrape some data for me. What\'s the best way to access the date (the actual numbers, 2008) from this code sample? It\'

相关标签:
1条回答
  • 2021-01-05 12:17

    Find the dt tag by text and find the next dd sibling:

    soup.find('div', class_='detail_date').find('dt', text='Date').find_next_sibling('dd').text
    

    The complete code:

    from bs4 import BeautifulSoup
    
    data = """
    <div class='dl_item_container clearfix detail_date'>
        <dt>Date</dt>
        <dd>
        2008
        </dd>
    </div>
    """
    
    soup = BeautifulSoup(data)
    date_field = soup.find('div', class_='detail_date').find('dt', text='Date')
    print date_field.find_next_sibling('dd').text.strip()
    

    Prints 2008.

    0 讨论(0)
提交回复
热议问题