python - Beautiful Soup 返回包裹在 <div> 标签中的元素。我该如何剥离它们?

标签 python beautifulsoup

我正在尝试解析此网页中的信息:http://bigcharts.marketwatch.com/quickchart/quickchart.asp?symb=AAPL

Python 代码

list = [td.find('div') for td in soup1.find_all('td')]

返回 20 项。例如

print list[10]

返回

<div>100.60</div>

我怎样才能简单地得到 BeautifulSoup 返回“100.60”。或者,我怎样才能去掉标签?

最佳答案

您可以使用.text.string方法获取标签内的text。在你的情况下,两者都会起作用。 .text 将返回 unicode 字符串,.string 将返回 NavigableString 对象。

print list[10].text

或者,

print list[10].string

另请检查 difference between .text and .string .

您还可以使用 string 对象的 strip 方法来剥离它们。就像,

list[10].text.strip()

关于python - Beautiful Soup 返回包裹在 <div> 标签中的元素。我该如何剥离它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26003399/

相关文章:

python - soup.select() 中的 CSS 选择器返回 null

python - 如何使用 Jetson nano GPIO 在 GPIO.TEGRA_SOC 模式与 GPIO.BCM 模式下设置 GPIO 引脚?

python - 仅向 matplotlib 中图例的一个组件添加背景颜色

python - 如何将列表拆分为元素中特定字符的列表列表?

python & BeautifulSoup : How to extract a tags' value which is in many others tags?

python - 美汤元素如何添加元素

python - BeautifulSoup 实例化超时?

python - Sublime 代码片段中的内联 Python 代码

javascript - 如何在 chameleon/zpt 模板( Pyramid )中正确包含 javascript 代码?

python - 美丽汤 : Fetched all the links on a webpage how to navigate through them without selenium?