美丽集团+;PythonHTMLUL目标,创建列表并附加到变量

美丽集团+;PythonHTMLUL目标,创建列表并附加到变量,python,web-scraping,beautifulsoup,html-lists,Python,Web Scraping,Beautifulsoup,Html Lists,我正在努力拼凑一份统计数据和名字的excel 我一直在尝试在没有任何类或ID的情况下循环一个html“ul”元素,并在python列表中组织该信息,然后将各个li元素附加到表中的不同字段中 正如你所看到的,我能够针对标题和价格元素,但“ul”真的很棘手。。。好。。。以我的技术水平 我正在努力解决的具体代码: for i in range(1, 2): response = get('https://www.autotrader.co.uk/car-search?sort=sponsore

我正在努力拼凑一份统计数据和名字的excel

我一直在尝试在没有任何类或ID的情况下循环一个html“ul”元素,并在python列表中组织该信息,然后将各个li元素附加到表中的不同字段中

正如你所看到的,我能够针对标题和价格元素,但“ul”真的很棘手。。。好。。。以我的技术水平

我正在努力解决的具体代码:

for i in range(1, 2):
    response = get('https://www.autotrader.co.uk/car-search?sort=sponsored&seller-type=private&page=' + str(i))
    html_soup = BeautifulSoup(response.text, 'html.parser')
    ad_containers = html_soup.find_all('h2', class_ = 'listing-title title-wrap')
    price_containers = html_soup.find_all('section', class_ = 'price-column')

    for container in ad_containers:
        name = container.find('a', class_ ="js-click-handler listing-fpa-link").text
        names.append(name)
        # Trying to loop through the key specs list and assigned each 'li' to a different field in the table
        lis = []
        list_container = container.find('ul', class_='listing-key-specs')
        for li in list_container.find('li'):
            lis.append(li)
        year.append(lis[0])
        body_type.append(lis[1])
        milage.append(lis[2])
        engine.append(lis[3])
        hp.append(lis[4])
        transmission.append(lis[5])
        petrol_type.append(lis[6])
        lis = [] # Clearing dictionary to get ready for next set of data
我得到的错误信息如下:

完整代码如下:

from requests import get
from bs4 import BeautifulSoup
import pandas
# from time import sleep, time
# import random

# Create table fields
names = []
prices = []
year = []
body_type = []
milage = []
engine = []
hp = []
transmission = []
petrol_type = []

for i in range(1, 2):
    # Make a get request
    response = get('https://www.autotrader.co.uk/car-search?sort=sponsored&seller-type=private&page=' + str(i))
    # Pause the loop
    # sleep(random.randint(4, 7))
    # Create containers
    html_soup = BeautifulSoup(response.text, 'html.parser')
    ad_containers = html_soup.find_all('h2', class_ = 'listing-title title-wrap')
    price_containers = html_soup.find_all('section', class_ = 'price-column')

    for container in ad_containers:
        name = container.find('a', class_ ="js-click-handler listing-fpa-link").text
        names.append(name)
        # Trying to loop through the key specs list and assigned each 'li' to a different field in the table
        lis = []
        list_container = container.find('ul', class_='listing-key-specs')
        for li in list_container.find('li'):
            lis.append(li)
        year.append(lis[0])
        body_type.append(lis[1])
        milage.append(lis[2])
        engine.append(lis[3])
        hp.append(lis[4])
        transmission.append(lis[5])
        petrol_type.append(lis[6])
        lis = [] # Clearing dictionary to get ready for next set of data
    for pricteainers in price_containers:
        price = pricteainers.find('div', class_ ='vehicle-price').text
        prices.append(price)

test_df = pandas.DataFrame({'Title': names, 'Price': prices, 'Year': year, 'Body Type': body_type, 'Mileage': milage, 'Engine Size': engine, 'HP': hp, 'Transmission': transmission, 'Petrol Type': petrol_type})
print(test_df.info())
# test_df.to_csv('Autotrader_test.csv')

ul
不是
h2
的子级。是兄弟姐妹


因此,您需要进行单独的选择,因为它不是
ad\u容器的一部分

在另一个答案的评论区,我听从了大卫的建议

代码:

输出:

                                Title Price           Year  Body Type        Mileage Engine Size      HP Transmission Petrol Type
0    Citroen C3 1.4 HDi Exclusive 5dr  £500  2002 (52 reg)  Hatchback  123,065 miles        1.4L   70bhp       Manual      Diesel
1                Volvo V40 1.6 XS 5dr  £585   1999 (V reg)     Estate  125,000 miles        1.6L  109bhp       Manual      Petrol
2  Toyota Yaris 1.3 VVT-i 16v GLS 3dr  £700   2000 (W reg)  Hatchback   94,000 miles        1.3L   85bhp    Automatic      Petrol
3               MG Zt-T 2.5 190 + 5dr  £750  2002 (52 reg)     Estate   95,000 miles        2.5L  188bhp       Manual      Petrol
4       Volkswagen Golf 1.9 SDI E 5dr  £795  2001 (51 reg)  Hatchback  153,000 miles        1.9L   68bhp       Manual      Diesel
5   Volkswagen Polo 1.9 SDI Twist 5dr  £820  2005 (05 reg)  Hatchback  106,116 miles        1.9L   64bhp       Manual      Diesel
6     Volkswagen Polo 1.4 S 3dr (a/c)  £850  2002 (02 reg)  Hatchback  125,640 miles        1.4L   75bhp       Manual      Petrol
7              KIA Picanto 1.1 LX 5dr  £990  2005 (05 reg)  Hatchback  109,000 miles        1.1L   64bhp       Manual      Petrol
8    Vauxhall Corsa 1.2 i 16v SXi 3dr  £995  2004 (54 reg)  Hatchback   81,114 miles        1.2L   74bhp       Manual      Petrol
9           Volkswagen Beetle 1.6 3dr  £995  2003 (53 reg)  Hatchback  128,000 miles        1.6L  102bhp       Manual      Petrol

作为建议,我会选择像
article.search listing
这样的东西作为真正的容器,然后在一个循环中我将能够找到所有需要的孩子。
                                Title Price           Year  Body Type        Mileage Engine Size      HP Transmission Petrol Type
0    Citroen C3 1.4 HDi Exclusive 5dr  £500  2002 (52 reg)  Hatchback  123,065 miles        1.4L   70bhp       Manual      Diesel
1                Volvo V40 1.6 XS 5dr  £585   1999 (V reg)     Estate  125,000 miles        1.6L  109bhp       Manual      Petrol
2  Toyota Yaris 1.3 VVT-i 16v GLS 3dr  £700   2000 (W reg)  Hatchback   94,000 miles        1.3L   85bhp    Automatic      Petrol
3               MG Zt-T 2.5 190 + 5dr  £750  2002 (52 reg)     Estate   95,000 miles        2.5L  188bhp       Manual      Petrol
4       Volkswagen Golf 1.9 SDI E 5dr  £795  2001 (51 reg)  Hatchback  153,000 miles        1.9L   68bhp       Manual      Diesel
5   Volkswagen Polo 1.9 SDI Twist 5dr  £820  2005 (05 reg)  Hatchback  106,116 miles        1.9L   64bhp       Manual      Diesel
6     Volkswagen Polo 1.4 S 3dr (a/c)  £850  2002 (02 reg)  Hatchback  125,640 miles        1.4L   75bhp       Manual      Petrol
7              KIA Picanto 1.1 LX 5dr  £990  2005 (05 reg)  Hatchback  109,000 miles        1.1L   64bhp       Manual      Petrol
8    Vauxhall Corsa 1.2 i 16v SXi 3dr  £995  2004 (54 reg)  Hatchback   81,114 miles        1.2L   74bhp       Manual      Petrol
9           Volkswagen Beetle 1.6 3dr  £995  2003 (53 reg)  Hatchback  128,000 miles        1.6L  102bhp       Manual      Petrol