作为一个实践项目,我目前正在为AutoTrader的web scrape编写一些代码。我打印所需的结果有困难。你知道吗
所需输出应为:
Car 1
Specs Car 1
相反,它是
Car 1
Specs Car 1
Specs Car 2
Specs Car X
car 2
我的循环结构哪里出错了?你知道吗
from bs4 import BeautifulSoup
import requests
page_link = ("https://www.autotrader.co.uk/car-search?sort=price-asc&radius=1500&postcode=lu15jq&onesearchad=Used&onesearchad=Nearly%20New&onesearchad=New&make=AUDI&model=A5&price-to=8500&year-from=2008&maximum-mileage=90000&transmission=Automatic&exclude-writeoff-categories=on")
LN = 0
r = requests.get(page_link)
c = r.content
soup = BeautifulSoup(c,"html.parser")
all = soup.find_all("h2",{"class":"listing-title title-wrap"})
all2 = soup.find_all('ul',{"class" :'listing-key-specs '})
上面的代码块很好。下面的块打印输出。你知道吗
LN = -1
ListTotal = len(all)
for item in all:
if LN <= ListTotal:
LN += 1
print(item.find("a", {"class": "js-click-handler listing-fpa-link"}).text)
for carspecs in all2:
print (carspecs.text)
else:
break
谢谢
因为你每次都在打印
我猜你想
仅供参考,我用更好的逻辑和名称清理了代码,去掉了多余的东西,并使其服从python style guide
相关问题 更多 >
编程相关推荐