python - 网站抓取价格为零的问题

标签 python regex web-scraping beautifulsoup

嗨,我想从此代码中获取价格

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/

相关文章:

python - 无法使用相同的 AES 对象解密给定的密文两次

c++ - 从 C++ 中查找 python 函数参数

java - 正则表达式检测字符串中的数字

php - 如何确定一个字符串是否是 PHP 中的数学语句?

python - 从 ClinicalTrials.gov 抓取数据

python - Scrapy返回多个项目

python - Pipeline.py 显示异常

python - 如何找出 uWSGI 杀死 worker 的原因?

python - 错误: (-215) reprojectImageTo3D opencv

python - 替换部分 sympy 表达式