Use BeautifulSoup to get a value after a specific tag

后端 未结 1 1611
攒了一身酷
攒了一身酷 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 = """
    
    Date
    2008
    """ 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)
提交回复
热议问题