首页 > 解决方案 > Python Web Scraping:从非结构化数据中拆分数量

问题描述

我对 Web Scraping 和 python 领域比较陌生。我正在尝试从超市/在线杂货店抓取数据。我在清理刮取的数据时遇到问题-刮取的数据样本

考虑到上述数据样本,我想将数量与产品名称分开。所需格式名称 - Tata Salt Lite, Low Sodium, 数量 -1kg 名称 - Fortune Kachi Ghani 纯芥末油
数量 - 1L 等等...我试图用正则表达式将其分开

re.split("[,/._-]+", i)

但取得了部分成功。任何人都可以帮助我如何处理数据集。提前致谢。

标签: python-3.xseleniumweb-scrapingweb-crawlerdata-cleaning

解决方案


您可以尝试对每个字符串实施以下解决方案:

text_content = "Tata Salt Lite, Low Sodium, 1kg"
quantity = re.search("(\d+\s?(kg|g|L))", text_content).group()
name = text_content.rsplit(quantity)[0].strip().rstrip(',')
description = "Name - {}, Quantity - {}".format(name, quantity)

推荐阅读