Python,美丽的汤,WebScraping,熊猫,数据帧

时间:2015-09-02 03:00:22

标签: python html pandas beautifulsoup

Complex Beautiful Soup query

我正在熟悉Beautiful Soup和Pandas的Dataframe,但我似乎无法将两者结合起来。

import urllib.request
from bs4 import BeautifulSoup
import pandas as pd


connection = urllib.request.urlopen('http://www.carfolio.com/specifications/models/?man=557')
soup = BeautifulSoup(connection, "html.parser", from_encoding='utf-7')

soup.decode('utf-7','ignore')

href_tag = soup.find_all(span="detail")
for href_tag in soup.body.stripped_strings:
    print(str(href_tag.encode('utf-7')))

最终,我的目标是抓住每辆车并创建一个带有相关信息(“细节”)的数据框,例如马力,扭矩,重量等。我只是不知道如何“抓住” “ 细节。 Relevant HTML Code

我环顾四周,有例子,但大多数都没有访问“abbr title”谢谢

2 个答案:

答案 0 :(得分:1)

如果你可以为列表中的每辆车提出额外的请求,那么这里有一个示例工作演示如何抓住汽车特性:

>>> import requests
>>> from bs4 import BeautifulSoup
>>> 
>>> soup = BeautifulSoup(requests.get("http://www.carfolio.com/specifications/models/car/?car=427691").content)
>>> for item in soup.select("div.summary dl dt"):
...     print(item.get_text(strip=True), item.find_next_sibling("dd").get_text(strip=True))
... 
(u'What body style?', u'hatchback with 4/5 seats')
(u'How long?', u'3973mm')
(u'How heavy?', u'1110kg')
(u'What size engine?', u'1 litre, 999cm3')
(u'How many cylinders?', u'3, Straight')
(u'How much power?', u'95PS/ 94bhp/ 70kW@ 5000-5500rpm')
(u'How much torque?', u'160Nm/ 118ft.lb/ 16.3kgm@ 1500-3500rpm')
(u'How quick?', u'0-100km/h: 10.9s')
(u'How fast?', u'186km/h, 116mph')
(u'How economical?', u'5.0/3.7/4.2 l/100km urban/extra-urban/combined')
(u'Whatcarbon dioxide emissions?', u'97.0CO2g/km')

答案 1 :(得分:1)

对于每辆车,您可以从li标签获取信息:

>>>from bs4 import BeautifulSoup

>>>url="""<li class="detail"><a href="car/?car=214027" class="addstable"><span class="automobile"><span class="manufacturer" title="Manufacturer">Manexall</span>, <span class="modelyear" title="Model year">1921 <abbr title="model year">MY</abbr></span> </span></a><span class="detail"> <abbr title="front engine, rear wheel drive">FR</abbr> 1143 <abbr title="cubic centimetres">cm<sup>3</sup></abbr> 13.2 <abbr title="Pferdestärke">PS</abbr> 13 <abbr title="brake horsepower">bhp</abbr> 9.7 <abbr title="kilowatts">kW</abbr> 363 <abbr title="kilograms">kg</abbr></span></li>"""

>>>soup = BeautifulSoup(url)
>>>[data.get_text() for data in soup.select('li')]
['Manexall, 1921 MY \n FR 1143 cm3 13.2 PS 13 bhp 9.7 kW 363 kg']