【问题标题】:Using BeautifulSoup to extract date a post was created使用 BeautifulSoup 提取帖子的创建日期
【发布时间】:2021-05-23 06:01:11
【问题描述】:

我正在尝试使用 BeautifulSoup 4 从招聘网站上抓取数据。具体来说,我想提取发布招聘广告的日期。涉及此信息的 HTML 在这里:

<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>

我想从此代码中获取“今天”。为此,我编写了以下代码:

for job in jobs:
    postdate=job.find("div", attrs={"class":"posted-by"})
    print(postdate.text)

但是这段代码返回

今天发布者 NHS 商业服务管理局 新的

而不仅仅是“今天”。

如何更正我的代码?

【问题讨论】:

    标签: python web-scraping beautifulsoup code-formatting


    【解决方案1】:

    使用 .next_element 。字符串或标签的 .next_element 属性指向之后立即解析的内容。

    print(soup_obj.find('div',{'class': 'posted-by'}).next_element)
    

    【讨论】:

    • 请点赞,接受这个/任何对你有用的答案
    猜你喜欢
    • 2019-12-18
    • 2012-12-09
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    • 2017-10-19
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多