【发布时间】:2020-04-10 10:12:58
【问题描述】:
我正在尝试查找包含子字符串“auction-results”的所有 div,然后提取类名。这是一个例子:
<div class="auction-results high-bid has-price"></div>
我可以像这样找到所有包含“拍卖结果”的 div:
results = soup.select("div[class*=auction-results]")
type(results)
results
Out: [<div class="auction-results high-bid has-price">
<i class="icon"></i>
<span class="lot-price"> $700,000</span>
</div>]
Out: bs4.element.ResultSet
我想要的是将整个类名 'auction-results high-bid has-price' 存储在 pandas 列中,如下所示:
class_text = ['auction-results high-bid has-price']
'auction-results high-bid has-price'
scraped_data = pd.DataFrame({'class_text': class_text})
scraped_data
class_text
0 auction-results high-bid has-price
我还没有找到解决方案,所以希望有人能帮助我,谢谢!
【问题讨论】:
-
您可以将结果作为字符串处理。子字符串/切片应该可以工作。stackoverflow.com/questions/663171/…
-
请编辑您的问题以显示您的数据框的外观;从问题中不清楚。
-
@Jack Fleeting 我添加了一个我想要的输出的示例,希望对您有所帮助。
-
@Sureshmani 感谢您的提示,但我需要为许多不同的页面提取类“名称”,这些页面都以“拍卖结果”开头但结尾不同。上面的示例显示“high-bid has-price”,但其他页面可能显示“sold has-price”。这就是为什么我需要通过子字符串 'auction-results' 搜索但提取整个类名。
标签: python web-scraping beautifulsoup web-crawler