2017-02-12 7 views
1

Я пытаюсь использовать BeautifulSoup для анализа информации, хранящейся в таблице HTML, и сохранения ее в dict. Я смог добраться до стола и перебирать значения, но в таблице все еще много мусора, что я не уверен, как позаботиться.Анализ данных таблицы HTML с помощью BeautifulSoup в dict

# load the HTML file 
r = requests.get("http://www.ebay.com/itm/222378225962") 
soup = BeautifulSoup(r.content, "html.parser") 

# navigate to the item attributes table 
table = soup.find('div', 'itemAttr') 

# iterate through the attribute information 
attr = [] 
for i in table.findAll("tr"): 
    attr.append(i.text.strip().replace('\t', '')) 

С помощью этого метода данные выглядят так. Как вы видите, там много мусора, а некоторые строки содержат несколько элементов, таких как Year и VIN.

[u'Condition:\nUsed', 
u'Seller Notes:\n\u201cExcellent Condition\u201d', 
u'Year: \n\n2015\n\n VIN (Vehicle Identification Number): \n\n2G1FJ1EW2F9192023', 
u'Mileage: \n\n29,000\n\n Transmission: \n\nManual', 
u'Make: \n\nChevrolet\n\n Body Type: \n\nCoupe', 
u'Model: \n\nCamaro\n\n Warranty: \n\nVehicle has an existing warranty', 
u'Trim: \n\nSS Coupe 2-Door\n\n Vehicle Title: \n\nClear', 
u'Engine: \n\n6.2L 6162CC 376Cu. In. V8 GAS OHV Naturally Aspirated\n\n Options: \n\nLeather Seats', 
u'Drive Type: \n\nRWD\n\n Safety Features: \n\nAnti-Lock Brakes, Driver Airbag, Passenger Airbag, Side Airbags', 
u'Power Options: \n\nAir Conditioning, Cruise Control, Power Locks, Power Windows, Power Seats\n\n Sub Model: \n\n1LE', 
u'Fuel Type: \n\nGasoline\n\n Color: \n\nWhite', 
u'For Sale By: \n\nPrivate Seller\n\n Interior Color: \n\nBlack', 
u'Disability Equipped: \n\nNo\n\n Number of Cylinders: \n\n8', 
u''] 

В конечном счете, я хочу, чтобы данные хранились в словаре, как показано ниже. Я знаю, как создать словарь, но не знаю, как очистить данные, которые нужно войти в словарь без грубой силы find-and-replace.

{'Condition' : 'Used', 
'Seller Notes' : 'Excellent Condition', 
'Year': '2015', 
'VIN (Vehicle Identification Number)': '2G1FJ1EW2F9192023', 
'Mileage': '29,000', 
'Transmission': 'Manual', 
'Make': 'Chevrolet', 
'Body Type': 'Coupe', 
'Model': 'Camaro', 
'Warranty': 'Vehicle has an existing warranty', 
'Trim': 'SS Coupe 2-Door', 
'Vehicle Title' : 'Clear', 
'Engine': '6.2L 6162CC 376Cu. In. V8 GAS OHV Naturally Aspirated', 
'Options': 'Leather Seats', 
'Drive Type': 'RWD', 
'Safety Features' : 'Anti-Lock Brakes, Driver Airbag, Passenger Airbag, Side Airbags', 
'Power Options' : 'Air Conditioning, Cruise Control, Power Locks, Power Windows, Power Seats', 
'Sub Model' : '1LE', 
'Fuel Type' : 'Gasoline', 
'Exterior Color' : 'White', 
'For Sale By' : 'Private Seller', 
'Interior Color' : 'Black', 
'Disability Equipped' : 'No', 
'Number of Cylinders': '8'} 

ответ

3

Вместо того, чтобы разобрать данные из tr элементов, лучший подход был бы итерацию по элементам данных td.attrLabels. Вы можете использовать эти метки в качестве ключа, а затем использовать смежные элементы родства как значение.

В приведенном ниже примере, селектор CSS div.itemAttr td.attrLabels используется, чтобы выбрать все элементы с td.attrLabels классов, которые являются потомками div.itemAttr. Оттуда метод .find_next_sibling() используется для поиска смежного элемента-брата.

r = requests.get("http://www.ebay.com/itm/222378225962") 
soup = BeautifulSoup(r.content, 'lxml') 

data = [] 
for label in soup.select('div.itemAttr td.attrLabels'): 
    data.append({ label.text.strip(): label.find_next_sibling().text.strip() }) 

Выход:

> [{'Year:': '2015'}, {'VIN (Vehicle Identification Number):': '2G1FJ1EW2F9192023'}, {'Mileage:': '29,000'}, {'Transmission:': 'Manual'}, {'Make:': 'Chevrolet'}, {'Body Type:': 'Coupe'}, {'Model:': 'Camaro'}, {'Warranty:': 'Vehicle has an existing warranty'}, {'Trim:': 'SS Coupe 2-Door'}, {'Vehicle Title:': 'Clear'}, {'Engine:': '6.2L 6162CC 376Cu. In. V8 GAS OHV Naturally Aspirated'}, {'Options:': 'Leather Seats'}, {'Drive Type:': 'RWD'}, {'Safety Features:': 'Anti-Lock Brakes, Driver Airbag, Passenger Airbag, Side Airbags'}, {'Power Options:': 'Air Conditioning, Cruise Control, Power Locks, Power Windows, Power Seats'}, {'Sub Model:': '1LE'}, {'Fuel Type:': 'Gasoline'}, {'Exterior Color:': 'White'}, {'For Sale By:': 'Private Seller'}, {'Interior Color:': 'Black'}, {'Disability Equipped:': 'No'}, {'Number of Cylinders:': '8'}] 

Если вы хотите, чтобы получить заголовок таблицы th элементы, то вы можете выбрать элемент таблицы, а затем использовать селектор CSS th, td.attrLabels для того, чтобы восстановить как этикеты:

r = requests.get("http://www.ebay.com/itm/222378225962") 
soup = BeautifulSoup(r.content, 'lxml') 
table = soup.find('div', 'itemAttr') 

data = [] 
for label in table.select('th, td.attrLabels'): 
    data.append({ label.text.strip(): label.find_next_sibling().text.strip() }) 

Выход:

> [{'Condition:': 'Used'}, {'Seller Notes:': '“Excellent Condition”'}, {'Year:': '2015'}, {'VIN (Vehicle Identification Number):': '2G1FJ1EW2F9192023'}, {'Mileage:': '29,000'}, {'Transmission:': 'Manual'}, {'Make:': 'Chevrolet'}, {'Body Type:': 'Coupe'}, {'Model:': 'Camaro'}, {'Warranty:': 'Vehicle has an existing warranty'}, {'Trim:': 'SS Coupe 2-Door'}, {'Vehicle Title:': 'Clear'}, {'Engine:': '6.2L 6162CC 376Cu. In. V8 GAS OHV Naturally Aspirated'}, {'Options:': 'Leather Seats'}, {'Drive Type:': 'RWD'}, {'Safety Features:': 'Anti-Lock Brakes, Driver Airbag, Passenger Airbag, Side Airbags'}, {'Power Options:': 'Air Conditioning, Cruise Control, Power Locks, Power Windows, Power Seats'}, {'Sub Model:': '1LE'}, {'Fuel Type:': 'Gasoline'}, {'Exterior Color:': 'White'}, {'For Sale By:': 'Private Seller'}, {'Interior Color:': 'Black'}, {'Disability Equipped:': 'No'}, {'Number of Cylinders:': '8'}] 

Если вы хотите, чтобы вырезать не алфавитно-цифровой символ (ы) для ключей, то вы можете использовать:

r = requests.get("http://www.ebay.com/itm/222378225962") 
soup = BeautifulSoup(r.content, 'lxml') 
table = soup.find('div', 'itemAttr') 

data = [] 
for label in table.select('th, td.attrLabels'): 
    key = re.sub(r'\W+', '', label.text.strip()) 
    value = label.find_next_sibling().text.strip() 

    data.append({ key: value }) 

Выход:

> [{'Condition': 'Used'}, {'SellerNotes': '“Excellent Condition”'}, {'Year': '2015'}, {'VINVehicleIdentificationNumber': '2G1FJ1EW2F9192023'}, {'Mileage': '29,000'}, {'Transmission': 'Manual'}, {'Make': 'Chevrolet'}, {'BodyType': 'Coupe'}, {'Model': 'Camaro'}, {'Warranty': 'Vehicle has an existing warranty'}, {'Trim': 'SS Coupe 2-Door'}, {'VehicleTitle': 'Clear'}, {'Engine': '6.2L 6162CC 376Cu. In. V8 GAS OHV Naturally Aspirated'}, {'Options': 'Leather Seats'}, {'DriveType': 'RWD'}, {'SafetyFeatures': 'Anti-Lock Brakes, Driver Airbag, Passenger Airbag, Side Airbags'}, {'PowerOptions': 'Air Conditioning, Cruise Control, Power Locks, Power Windows, Power Seats'}, {'SubModel': '1LE'}, {'FuelType': 'Gasoline'}, {'ExteriorColor': 'White'}, {'ForSaleBy': 'Private Seller'}, {'InteriorColor': 'Black'}, {'DisabilityEquipped': 'No'}, {'NumberofCylinders': '8'}]