【发布时间】:2021-03-26 21:26:10
【问题描述】:
我正在尝试在每页显示 25 个条目的零售网站上收集有关二手车的数据(型号、公里数、价格)。它适用于前 5 个条目,但随后它停止并且不从其余 20 个条目中收集数据。所有 25 个条目的标签都是相同的,所以我不明白为什么它在 5 之后停止。感谢您的帮助!
import requests
from bs4 import BeautifulSoup
counter=0
page = requests.get('https://www.willhaben.at/iad/gebrauchtwagen/auto/ford-gebrauchtwagen/mustang')
soup = BeautifulSoup(page.content, 'html.parser')
for Inhalt_1 in soup.find_all('div', class_='Box-wfmb7k-0 hkyQgZ'):
print(Inhalt_1.h3.text)
print()
for Inhalt_2 in soup.find_all('span', class_='Text-sc-10o2fdq-0 fiVXiu'):
print(Inhalt_2.text)
print()
for Inhalt_3 in soup.find_all('div', class_='Text-sc-10o2fdq-0 fTyYiu'):
if (counter+1)%2==0:
print(Inhalt_3.span.text, 'km')
counter +=1
输出:
Ford Mustang Cabrio 2,3 Turbo Ecoboost 317 Ps Autom Leder Klima
Ford Mustang V6
Ford Mustang 5,0 Ti-VCT V8 GT
Ford Mustang Shelby GT500
Ford Mustang Ford Mustang Mach I Coupe
€ 32.900
€ 16.500
€ 32.500
€ 79.800
€ 49.500
77.900 km
113.000 km
111.000 km
21.879 km
100.000 km
【问题讨论】:
-
我假设网站一开始只加载 5 个元素,并在使用滚动到页面底部时填充它们。