【发布时间】:2020-03-10 16:16:22
【问题描述】:
嗨,我想从这段代码中获取价格
import re
import requests
from bs4 import BeautifulSoup
list3 = []
r = requests.get('https://bama.ir/car/peugeot/')
soup = BeautifulSoup(r.text, 'html.parser')
res2 = soup.find_all('div', attrs={'class':'listdata'})
for item in res2:
z = re.findall(r'<span itemprop="price" content="([^"]*)">[^<]*</span>',str(item))
list3 += z
for item in list3:
print(item)
但此代码不返回“0”价格。我也想要“0”价格。 有人可以帮我吗?
【问题讨论】:
-
根据您的正则表达式,所有价格都不是
0 -
如何在这个正则表达式中添加一个零?
-
[^"]已经占零了。它是一个否定集,意思是“找到所有不是双引号的东西”,而零不是双引号。正则表达式是正确的,但您的items 都不包含零。 -
我的一些项目是“0”。这个正则表达式不显示“0”
-
你的 HTML 是什么样的?
标签: python regex web-scraping beautifulsoup