问题
I currently am successfully scraping the data I need by chaining bs4 .contents together following a find_all('div')
, but that seems inherently fragile. I'd like to go directly to the tag I need by class, but my "class_=" search is returning None
.
I ran the following code on the html below, which returns None
:
soup = BeautifulSoup(text) # this works fine
tag = soup.find(class_ = "loan-section-content") # this returns None
Also tried soup.find('div', class_ = "loan-section-content")
- also returns None
.
My html is:
<div class="loan-section">
<div class="loan-section-title">
<span class="text-light"> Some Text </span>
</div>
<div class="loan-section-content">
<div class="row">
<div class="col-sm-6">
<strong>More text</strong>
<br/>
<strong>
<a href="https://www.google.com/maps/place/Dakar,+Senegal/" target="_blank">Dakar</a>, Senegal
</strong>
回答1:
try this
soup.find(attrs={'class':'loan-section-content'})
or
soup.find('div','loan-section-content')
attrs
will search on attributes
Demo:
来源:https://stackoverflow.com/questions/26952723/bs4-searching-by-class-returning-empty