【问题标题】:Beautiful soup and extracting values美丽的汤和提取价值
【发布时间】:2015-05-08 05:01:53
【问题描述】:

如果您能给我一些指导,告诉我如何在使用 beautifulsoup 时获取下面的出生日期“1723 年 6 月 16 日”,我将不胜感激。现在使用我的代码,我设法获取了您在下面的结果中看到的值,但是我只需要获取 1723 年 6 月 16 日的值。有什么建议吗?

我的代码:

birth = soup.find("table",{"class":"infobox"})

test = birth.find(text='Born')

next_cell = test.find_parent('th').find_next_sibling('td').get_text()

print next_cell 

结果:

16 June 1723 NS (5 June 1723 OS)Kirkcaldy, Scotland,Great Britain

【问题讨论】:

  • 您的数据可能更容易从td 标记中的子标记中提取。是否可以显示该表格的 HTML?
  • 出生
  • 出生 1723 年 6 月 16 日 NS

标签: python html beautifulsoup html-parsing


【解决方案1】:

添加这个而不是最后一个打印语句

print ' '.join(str(next_cell).split()[:3])

【讨论】:

    猜你喜欢
    • 2021-04-03
    • 1970-01-01
    • 2020-01-19
    • 2016-09-11
    • 1970-01-01
    • 2018-09-13
    • 2021-05-24
    • 2015-10-21
    • 1970-01-01
    相关资源
    最近更新 更多