Issue
I'm trying to use BeautifulSoup 4 to scrape data from a recruitment website. Specifically, I want to extract the date when a job ad was posted. The HTML that involves this information is here:
<div class="posted-by">
Posted Today by
<a href="/jobs/nhs-business-services-authority-72549/p72549" class="gtmJobListingPostedBy">NHS Business Services Authority</a>
<span class=" "> <span data-qa="newMobileLbl" class="label label-new" > New</span> </span>
</div>
I want to get "Today" from this code. To do this, I wrote the following code:
for job in jobs:
postdate=job.find("div", attrs={"class":"posted-by"})
print(postdate.text)
But this code returns
Posted Today by NHS Business Services Authority New
instead of just "Today."
How can I correct my code?
Solution
use .next_element . The .next_element attribute of a string or tag points to whatever was parsed immediately afterwards.
print(soup_obj.find('div',{'class': 'posted-by'}).next_element)
Answered By - Samsul Islam
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.