I'm trying to target a date in bs4
that has no tag associated with it.
The structure of the HTML is:
<div class="story-postdate">
<div class="label-inline">Published</div>
Feb 6, 2020, 4:40 pm SGT
</div>
I've tried .findAll(attrs={'class' : 'story-postdate'})
and then tried to use the sibling
selector. But no joy.
I've also tried to target it with .text
at the end of the CSS selector. But that still returns a none object type.
from bs4 import BeautifulSoup as soup
html = '''<div class="story-postdate">
<div class="label-inline">Published</div>
Feb 6, 2020, 4:40 pm SGT
</div>'''
b = soup(html, 'html.parser')
div_tag = b.findAll(attrs={'class' : 'story-postdate'})
for div in div_tag:
print(" ".join(div.text.split()[1:]))
Output:
Feb 6, 2020, 4:40 pm SGT
Or :
print(" ".join(div.text.split()[1:4]))
Output:
Feb 6, 2020,
Try this
from bs4 import BeautifulSoup
from simplified_scrapy import SimplifiedDoc
html = """<div class="story-postdate">
<div class="label-inline">Published</div>
Feb 6, 2020, 4:40 pm SGT
</div>"""
# using BeautifulSoup
soup = BeautifulSoup(html,features='lxml')
print (soup.find('div',attrs={'class' : 'story-postdate'}).div.next_sibling)
# using SimplifiedDoc
doc = SimplifiedDoc(html)
print (doc.select('div.story-postdate>div').nextText())
Here are more examples of SimplifiedDoc Library: https://github.com/yiyedata/simplified-scrapy-demo/tree/master/doc_examples
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.