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\'
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
.