【问题标题】:Extract data from span tag within a div tag从 div 标签内的 span 标签中提取数据
【发布时间】:2018-11-10 15:28:09
【问题描述】:

我有下面的 html 代码,我正在尝试将 3:40 提取为文本以在我的 python 脚本中使用。我将如何获取这些信息?

example element

【问题讨论】:

  • 不放图,写代码。
  • 你在用什么? XPath?

标签: python html


【解决方案1】:

我会使用BeautifulSoup 库。在知道您已经拥有 HTML 文件的情况下,我将通过以下方式获取此信息:

from bs4 import BeautifulSoup

with open(html_path) as html_file:
    html_page = BeautifulSoup(html_file, 'html.parser')
    div = html_page.find('div', class_='playbackTimeline__duration')
    span = div.find('span', {'aria-hidden': 'true'})
    text = span.get_text()

我不确定它是否有效,但它可以让您了解如何执行此类操作。如果您想了解更多信息,请检查“网络抓取”。 :)

【讨论】:

    猜你喜欢
    • 2021-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多