【问题标题】:How to scrape date from li tag using beautifulsoup?如何使用beautifulsoup从li标签中刮取日期?
【发布时间】:2021-10-18 20:56:18
【问题描述】:

我抓取了所有具有特定类的 li 标签并得到了输出:

<li>Aug 14-18, <a href="https://ai4good.org/fragile-earth-2021/">Fragile Earth 2021</a>, develop radically new technological foundations for advancing and meeting the Sustainable Development Goals. Online KDD-21 workshop.
</li>

<li>Aug 19-26, <a href="https://ijcai-21.org/">IJCAI-21: 30th Int. Joint Conference on Artificial Intelligence</a>. Montreal-themed Virtual Reality, Online.
</li>

我可以分别刮掉 href 和 text,但是我也想将日期存储在数据框的列中,或者至少单独获取日期。知道我该怎么做吗?

这是网站的链接:https://www.kdnuggets.com/meetings/index.html#Y21-10

【问题讨论】:

  • 试试link_text.split(', &lt;')[0] :)
  • edit你的问题展示你到目前为止所做的尝试
  • 请附上您的代码以及到目前为止您尝试过的内容。

标签: python beautifulsoup jupyter-notebook google-colaboratory


【解决方案1】:

我认为应该这样做:

from bs4 import BeautifulSoup

soup = BeautifulSoup("""<li>Aug 14-18, <a href="https://ai4good.org/fragile-earth-2021/">Fragile Earth 2021</a>, develop radically new technological foundations for advancing and meeting the Sustainable Development Goals. Online KDD-21 workshop.
</li>

<li>Aug 19-26, <a href="https://ijcai-21.org/">IJCAI-21: 30th Int. Joint Conference on Artificial Intelligence</a>. Montreal-themed Virtual Reality, Online.
</li>""", "lxml")
dates = [x.text.split(',')[0] for x in soup.find_all('li')]
print(dates)

输出:

['Aug 14-18', 'Aug 19-26']

【讨论】:

    【解决方案2】:

    您可以使用.contents 方法访问特定元素:

    from bs4 import BeautifulSoup
    
    
    html = """
    <li>Aug 14-18, <a href="https://ai4good.org/fragile-earth-2021/">Fragile Earth 2021</a>, develop radically new technological foundations for advancing and meeting the Sustainable Development Goals. Online KDD-21 workshop. </li>
    <li>Aug 19-26, <a href="https://ijcai-21.org/">IJCAI-21: 30th Int. Joint Conference on Artificial Intelligence</a>. Montreal-themed Virtual Reality, Online. </li>
    """
    
    soup = BeautifulSoup(html, "html.parser")
    
    dates = [tag.contents[0].strip(', ') for tag in soup.find_all("li")]
    print(dates)
    

    输出:

    ['Aug 14-18', 'Aug 19-26']
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-03
      • 1970-01-01
      • 2020-10-19
      • 2017-12-02
      • 1970-01-01
      • 2020-08-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多