嗨,我想从此代码中获取价格
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”价格。 有人可以帮助我吗?
最佳答案
我猜,您希望在 content
属性中返回 0
,这可能很接近:
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'})
# print(res2)
for item in res2:
z = re.findall(r'content="(\d*)"', str(item))
list3 += z
for item in list3:
print(item)
输出
145000000
145000000
77000000
77000000
0
116000000
116000000
106000000
106000000
105000000
105000000
0
58000000
3600000
0
0
142800000
142800000
35000000
35000000
0
<小时/>
如果您想简化/修改/探索表达式,regex101.com 的右上角面板已对此进行了解释。 。如果您愿意,也可以在 this link 观看,它如何与一些示例输入相匹配。
<小时/>关于python - 网站抓取价格为零的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58861289/