如何删除div括号?我只想要剩下的车型名称:&;e - pace &;; &;F-Pace svr &;;…
非常感谢你的意见。我是新的编码,所以会很高兴得到你的解决方案。
谢谢代码:
import requests
import time
#Inputs/URLs to scrape:
URL_model = ('https://carbuzz.com/cars/jaguar')
(response := requests.get(URL_model)).raise_for_status()
soup = BeautifulSoup(response.text, 'lxml')
overview = soup.find()
for car_model in soup.find_all(class_='sub-model-with-score-preview__name'):
car_model
print(car_model)
输出:
E-Pace </div>
<div class="sub-model-with-score-preview__name">
F-Pace SVR </div>
<div class="sub-model-with-score-preview__name">
F-Pace </div>
<div class="sub-model-with-score-preview__name">
I-Pace </div>
<div class="sub-model-with-score-preview__name">
F-Type Coupe </div>
简单地使用get_text()
从标签中提取文本,并使用strip参数来摆脱空白/尾随空格或换行符:
print(car_model.get_text(strip=True)