我一直在努力从名为price
(产品的也可以)的多行字符串中取出所有数字。
我正在使用Python爬取网站上的产品名称和价格,并将结果写入文件,如下所示:
Master C141,"
6
999
.
-
"
Master 220,"
6
499
.
-
"
Master C170,"
12
499
.
-
"
我尝试了来自Stackoverflow和其他几个网站的很多不同的代码示例,但都没有奏效。我想要实现的输出如下所示:
这是代码:Master C141, 6999
Master 220, 6499
Master C170, 12499
content = driver.page_source
products=[] #List to store name of the product
prices=[] #List to store price of the product
soup = BeautifulSoup(content,"html.parser")
for a in soup.findAll('div', attrs={'class':'c-product-listing__col'}):
name=a.find('h2', attrs={'class':'c-product-card__heading'})
price=a.find('div', attrs={'class':'c-price-tag__price'})
print(re.findall("\d+", price.text))
products.append(name.text)
prices.append(price.text)
df = pd.DataFrame({'Product Name':products,'Price':prices})
df.to_csv('products.txt', index=False, encoding='utf-8')
\w+
代替Master
会更好?请在此处检查演示,以查看我的答案是否有效。你可以从那里开始。 - Tim Biegeleisen