Does anyone know how to get the div
using for-loop
?
(carss and fun place for kids)
<div>
<div>Toy Shop</div>
<div>#03-538</div>
<div>Fun place for kids</div>
</div>
<div>
<div>car Shop</div>
<div>#01-342</div>
<div>carss</div>
</div>
I am trying to scrape the div
content but does not know how to get content in div
that does not have class or id.
CodePudding user response:
Based on little details from question you could use css selectors
to select with pseudo classes :last-child
or :last-of-type
.
Example
from bs4 import BeautifulSoup
html = '''
<div>
<div>Toy Shop</div>
<div>#03-538</div>
<div>Fun place for kids</div>
</div>
<div>
<div>car Shop</div>
<div>#01-342</div>
<div>carss</div>
</div>
'''
soup = BeautifulSoup(html)
for e in soup.select('div>div:last-child'):
print(e.text)
Output
Fun place for kids
carss
CodePudding user response:
you should create a BeautifulSoup
object, something like this
soup = BeautifulSoup(html, 'html.parser')
then you declare a function for finding all div elements
divs = soup.find_all('div')
and lastly i think you could iterate it through the div elements using a for loop which kinda should look like this
for div in divs:
print(div.text)
that should scrape the divs perhaps.