如何在<tr>中抓取多个项目,并使用BS4将其拆分为3个变量?

daupos2t  于 2022-10-22  发布在  Python
关注(0)|答案(1)|浏览(157)

我正在尝试用多个括号刮一个网站。我的计划是有3个变量(oem,model,leadtime)来生成期望的输出。然而,我不知道如何在3个变量中抓取这个网页。
具有3个变量和命令的期望输出:
打印(oem、型号、交付周期)

Audi, A1 Sportback, 27 weeks
Audi, A3 Sportback, 27 weeks
...
Volvo, XC90, 27 weeks

截至目前的代码错误:

AttributeError: 'NavigableString' object has no attribute 'select'

截至目前的代码:

from bs4 import BeautifulSoup
import requests

response = requests.get("https://www.carwow.co.uk/new-car-delivery-times#gref").text
soup = BeautifulSoup(response, 'html.parser')

for tbody in soup.select('tbody'):
    for tr in tbody:
        oem = tr.select('td > a')[0].get('href').split('/')[3].capitalize()
        model = tr.select('td > a')[0].get('href').split('/')[4].capitalize()
        lead_time = tr.select('td')[1].getText(strip=True)
        print(oem, model, lead_time)

我对Python和美味汤很陌生。

ryevplcw

ryevplcw1#

尝试:

import requests
from bs4 import BeautifulSoup

response = requests.get(
    "https://www.carwow.co.uk/new-car-delivery-times#gref"
).text
soup = BeautifulSoup(response, "html.parser")

for tbody in soup.select("tbody"):     # for each table
    for tr in tbody.select("tr")[1:]:  # skip header
        brand, leadtime = [
            td.get_text(strip=True, separator=" ") for td in tr.select("td")
        ][:2]
        oem, model = brand.split(maxsplit=1)
        print("{:<20} {:<20} {}".format(oem, model, leadtime))

打印:

...

Toyota               RAV4                 45
Toyota               Yaris                18
Volkswagen           Golf                 41
Volkswagen           Golf GTI             45
Volkswagen           Golf R               45
Volkswagen           Polo                 32
Volkswagen           T-Cross              23
Volkswagen           Tiguan               52
Volkswagen           Touareg              52
Volkswagen           ID3                  52
Volkswagen           ID4                  52
Volvo                V60                  27
Volvo                V90                  27
Volvo                XC40                 18
Volvo                XC60                 27
Volvo                XC90                 27

相关问题