BeautifulSoup: <div class <span class></span><span class>TEXT I WANT</span>
python
Solution
spans = soup.find_all('span', attrs={'id':'titleDescriptionID'})
for span in spans:
print span.string
In your code, `wrapper_href.descendants` contains at least 4 elements, 2 span tags and 2 string enclosed by the 2 span tags. It searches its children recursively.
Problem
I am trying to extract the string enclosed by the span with id="titleDescription" using BeautifulSoup. ``` <div class="itemText"> <div class="wrapper"> <span class="itemPromo">Customer Choice Award Winner</span> <a href="http://www.newegg.com/Product/Product.aspx?Item=N82E16819116501" title="View Details" > <span class="itemDescription" id="titleDescriptionID" style="display:inline">Intel Core i7-3770K Ivy Bridge 3.5GHz (3.9GHz Turbo) LGA 1155 77W Quad-Core Desktop Processor Intel HD Graphics 4000 BX80637I73770K</span> <span class="itemDescription" id="lineDescriptionID" style="display:none">Intel Core i7-3770K Ivy Bridge 3.5GHz (3.9GHz Turbo) LGA 1155 77W Quad-Core Desktop Processor Intel HD Graphics 4000 BX80637I73770K</span> </a> </div> ``` Code snippet ``` f = open('egg.data', 'rb') content = f.read() content = content.decode('utf-8', 'replace') content = ''.join([x for x in content if ord(x) < 128]) soup = bs(content) for itemText in soup.find_all('div', attrs={'class':'itemText'}): wrapper = itemText.div wrapper_href = wrapper.a for child in wrapper_href.descendants: if child['id'] == 'titleDescriptionID': print(child, "\n") ``` Traceback Error: ``` Traceback (most recent call last): File "egg.py", line 66, in <module> if child['id'] == 'titleDescriptionID': TypeError: string indices must be integers ```