Issue
I want to scrape data from a website. (which generate cards using javascript I tried this and i am using chrome driver just for information)
Title = []
driver.get(url)
content = driver.page_source
soup = BeautifulSoup(content,'html.parser')
for a in soup.findAll('div',attrs={'class':'card-body text-center'}):
title = a.find('h4',attrs={'class':'card-title'})
Title.append(title.text)
print(Title)
I am getting my list Title empty Website Code looks like below from which i try to scrap data
<div class="card-body text-center">
<h4 class="card-title ">
Title</h4>
<h7>Risen_star</h7>
<br>
<h7>2022</h7>
<p style="height:57px" class="card-text ">
hi
</p>
<a href="/details" class="btn btn-md waves-effect">
Read More
</a>
</div>
Solution
It looks like things are working fine:
from bs4 import BeautifulSoup
Title = []
content = """<div class="card-body text-center">
<h4 class="card-title ">
Title</h4>
<h7>Risen_star</h7>
<br>
<h7>2022</h7>
<p style="height:57px" class="card-text ">
hi
</p>
<a href="/details" class="btn btn-md waves-effect">
Read More
</a>
</div>
"""
soup = BeautifulSoup(content,'html.parser')
for a in soup.findAll('div',attrs={'class':'card-body text-center'}):
title = a.find('h4',attrs={'class':'card-title'})
Title.append(title.text)
print(Title)
This results in:
$ python3 test.py
['\nTitle']
May be you content
variable is not correctly populated with the HTML page content ..
Answered By - Jib
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.