2016-04-05 10 views
2

私は、人気のある車のサイトのクイックスクレーパーを構築しようとしています。私は1台の車の結果を得ることができますが、私はページ上のすべての車を返す方法を見つけることができません。 findAll()がエラーを投げています。助けを歓迎するページ要素を繰り返すbeautifulsoup

from bs4 import BeautifulSoup 
import requests 

#search = input('Enter car to search: ') 
url = 'https://www.donedeal.ie/cars?words=bmw' #+ search 
site = requests.get(url) 
page = site.content 
soup = BeautifulSoup(page, 'html.parser') 
print("URL: ", site.url) 

if site.status_code == 200: 
    print("HTTP Status: ", site.status_code, "\n") 
else: 
    print("Bad HTTP response", "\n") 

cars = soup.find('div', attrs={'class': 'top-info'}) 
county = soup.find('span', attrs={'class': 'county-disp icon-pin'}) 
span = cars.find('span') 

for result in span: 
    for result2 in county: 
     print(result, "-", result2) 
+1

例ではfindAll()は使用されていません。また、どのようなエラーがスローされますか? – vds

+0

上記のコードではエラーは発生していません。コードは1つの値だけを返しています。 –

答えて

2

私は抽出したい情報が不明です。

>>> cars = soup.findAll('div', attrs={'class': 'top-info'}) 
>>> for car in cars: 
...  loc = car.find('span', attrs={'class': 'county-disp icon-pin'}) 
...  if loc: 
...   print('type:', car.text, 'location:', loc.text) 
...  else: 
...   print('type:', car.text) 
type: Bmw 320 CdTipperary location: Tipperary 
type: Bmw 520d MsportDonegal location: Donegal 
type: BMW2004 
type: BMW2010 
type: Bmw2010 
type: Bmw2000 
type: Bmw2001 
type: Bmw2004 
type: Bmw2004 
type: bmw2003 
type: BMW2009 
type: Bmw2010 
type: Bmw1990 
type: BMW2004 
type: BMW2012 
type: Bmw2000 
type: bmw2001 
type: BMW2004 
type: BMW2008 
type: BMW2005 
type: Bmw2006 
type: Bmw2002 
type: BMW2004 
type: Bmw2000 
type: BMW2003 
type: BMW2011 
type: BMW2001 
type: Bmw2000 
type: Bmw2002 
type: BMW2007 

ノートその1ページのためだけ:あなたは車の種類や郡の情報が欲しいと仮定すると、findAll()このようなもので動作します。あなたは他のページのURLを行う必要があります。