【问题标题】:Find a html element using part of the class name? [duplicate]使用部分类名查找 html 元素? [复制]
【发布时间】:2020-07-06 23:28:34
【问题描述】:

我正在使用 python 的 BeautifulSoup 库来抓取文本,但我想抓取所有包含类名的元素。

因此,如果多个元素具有“ProductCard”字样,但每个元素前面的字母排列略有不同(例如“ProductCard-HbksbD”、“ProductCard-UfbraH”),有没有办法获得所有这些?

for item in soup.find_all('a', {'class': 'ProductCard'}):
    print(item)

谢谢

【问题讨论】:

标签: python html class web-scraping beautifulsoup


【解决方案1】:

我相信 python 的 BeautifulSoup 库支持 CSS 选择器,所以你可以使用这个:

for item in soup.select('a[class*="ProductCard"]'):
    print(item)

【讨论】:

    猜你喜欢
    • 2015-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多