嗨,我想从这段代码中获取价格
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 :(得分:1)
我想,您希望返回0
属性中的content
,这可能很接近:
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中查看它如何与某些示例输入匹配。
答案 1 :(得分: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': 'overview'})
for item in res2:
z1 = re.findall(r'content="([0-9])"',str(item))
list3 += z1
z2 = re.findall(r'<!-- <p class="cost"><span itemprop="price" content="(\d*)">',str(item))
list3 += z2
for item in list3:
print(item)
感谢您的帮助。