如何从具有自定义特征的span标签中获取数据?(美汤)



我有下面的span标记。如何刮xuRMlBoIUcI7nAJktBcJvPByp1DLE4aPGzq3JNiRKsdNqUkVSJBY%2BggxRhp0GcRx4Gw4lWQxbTk%3D哪个分配给数据段塞?

<span data-ju-jspjrvxy="" 
data-slug="xuRMlBoIUcI7nAJktBcJvPByp1DLE4aPGzq3JNiRKsdNqUkVSJBY%2BggxRhp0GcRx4Gw4lWQxbTk%3D" 
data-gtm-clickedelement="CTA button" data-gtm-offer="" data-ju-wvxjoly-pk="303795"
data-gtm-voucher-id="303795" class="businessinsiderus-voucher-button-holder clear">

如果我对您的问题的理解是正确的,那么您需要抓取标记的属性。如果这确实是您的问题,以下链接将提供解决方案:用beautuloup 提取属性值

如果s是您的数据字符串,则使用正则表达式模块:

import re
match = re.findall('data-slug="()"',str(s))
from bs4 import BeautifulSoup as BS
content = 'your html span text here'
soup = BS(content,parser='html', features='lxml')
dict_of_spantag_attributes_and_values = soup.span.attrs
for i,j in dict_of_spantag_attributes_and_values.items():
print(f'{i}:{j}')

最新更新