I am using BeautifulSoup and Python to find a span tag that doesnt seem to have a class.
I am wanting to get the text “1hr ago” in the span tag, it has a… Variable? called “data-automation” but I can’t seem to find out how to find that using beautiful soup.
The first span has a class of “_3mgsa7- _2CsjSEq _2gpxOIH _15GBVuT _3VdCwhL _2Ryjovs
” which does produce the text using my code but it also has an error.
can anyone help me fix the error or explain how I would find the “data-automation” span tag?
MY CODE:
joblist =soup.find_all('article', class_='_37iADb_ _3BsYYYt') for job in joblist: listed = job.find('span', class_="_3mgsa7- _2CsjSEq _2gpxOIH _15GBVuT _3VdCwhL _2Ryjovs").text print(f'listed {listed}')
ERROR:
Traceback (most recent call last): File "C:UsersUserPycharmProjectsScraping1ScrapeTut 2 - scraping websites.py", line 34, in <module> listed = job.find('span', class_="_3mgsa7- _2CsjSEq _2gpxOIH _15GBVuT _3VdCwhL _2Ryjovs").text AttributeError: 'NoneType' object has no attribute 'text'
WEBSITE HTML CODE:
<span class="_3mgsa7- _2CsjSEq _2gpxOIH _15GBVuT _3VdCwhL _2Ryjovs"> <span class=""> <span aria-hidden="true" data-automation="jobListingDate">1h ago</span> </span> </span>
Advertisement
Answer
You can select a <span>
element with a specific attribute (such as data-automation
) by passing an attrs
dict as a keyword argument to .find()
or .find_all()
. See the documentation.
To find <span>
‘s where data-automation
has any value:
soup.find('span', attrs={'data-automation': True})
Where data-automation
has a specific value:
soup.find('span', attrs={'data-automation': 'jobListingDate'})