【问题标题】:Python, Beautiful Soup, WebScraping, Pandas, DataframePython, Beautiful Soup, WebScraping, Pandas, Dataframe
【发布时间】:2015-11-27 10:04:45
【问题描述】:

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')))

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

我环顾四周,有例子,但大多数都没有访问“缩写标题”谢谢

【问题讨论】:

  • 顺便说一句,请注意问题的法律方面:carfolio.com/legal
  • 我的理解是只要我个人使用应该没问题,这是我的本意。 . .也许,我错了。谢谢。
  • 无论如何,我已经发邮件去查了。

标签: python html pandas beautifulsoup


【解决方案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')

【讨论】:

  • 查看评论@mmachine 简而言之,我可以“通用化”它以使其适用于每辆车吗?谢谢。
【解决方案2】:

您可以从 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']

【讨论】:

  • 有没有办法让汽车保持空白(car=?)并打印每辆车的信息?我不知何故记得看到这是可能的。
  • 最终的原因是我想练习将信息最终下载到数据库(可能是idk SQL),并且每辆车都需要很长时间。
  • 你可能会得到带有汽车 ID 的元组:[(data.a.attrs['href'].split('=')[1],data.get_text()) 用于汤中的数据。选择('li')]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-22
  • 2018-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-15
  • 2019-08-10
相关资源
最近更新 更多