简体   繁体   English

从 python 中的 span 标签获取 class 名称

[英]Getting class name from span tag in python

I am new to data scraping and I am using BeautifulSoup to grap some data from a webpage.我是数据抓取的新手,我正在使用 BeautifulSoup 从网页中获取一些数据。 I'm trying to get the r100 in the span tag.我正在尝试在 span 标签中获取 r100。 I know r100 is the class name, but this data is required.我知道 r100 是 class 名称,但此数据是必需的。 Is it possible?可能吗?

my code我的代码

st2=st1.find("span",attrs={"class":"rating"})
print(st2)

out出去

<span class="rating r100"></span>

Extract the class then index.提取 class 然后索引。 It is a multi-valued class with 2 values ie 1 at index 0 and the other at index 1.它是一个多值 class,有 2 个值,即 1 在索引 0 处,另一个在索引 1 处。

st2['class'][1]

One you have <span class="rating r100"></span> in st2 .一个你有<span class="rating r100"></span>st2 You can use text您可以使用text

Code st2.text代码st2.text

Also, you can use get_text() which will allow you to pass more arguments such as此外,您可以使用get_text()这将允许您传递更多 arguments 例如

(separator, strip, types)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM