gpt4 book ai didi

python - 如何从 craigslist 中仅抓取低于 x 的价格

转载 作者:太空宇宙 更新时间:2023-11-03 20:39:51 24 4
gpt4 key购买 nike

我有一辆 bs4 在 craigslist 上抓取二手车。现在它返回所有帖子,但我试图获得低于 2000 美元的帖子。我知道我要么需要一个嵌套的 if 语句,要么需要一个单独的函数。有什么帮助吗?

# Loop through returned results
for result in results:
# Error handling
try:
# Identify and return title of listing
title = result.find('a', class_="result-title").text
# Identify and return price of listing
price = result.a.span.text
# Identify and return link to listing
link = result.a['href']

# Print results only if title, price, and link are available
if (price and title and link):
print('-------------')
print(title)
print(price)
print(link)
next
except AttributeError as e:
print(e)

最佳答案

您可以检查是否int(price) >= 2_000,如果使用继续则跳过打印:

for result in results:
title = result.find('a', class_="result-title").text
price = result.a.span.text
link = result.a['href']

try:
if int(price) >= 2_000:
continue
except ValueError:
continue


if all(price, title, link):
print('-------------')
print(title, price, link, sep='\n')

关于python - 如何从 craigslist 中仅抓取低于 x 的价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56927630/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com