簡體   English   中英

遍歷頁面元素beautifulsoup

[英]Iterating through page elements beautifulsoup

我正在嘗試為一個流行的汽車網站構建一個快速抓取工具。 我可以返回一輛車的結果,但我無法弄清楚如何返回頁面上的所有汽車。 findAll()拋出錯誤。 任何幫助,將不勝感激

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)

我不確定您要提取哪些信息。 假設您想要汽車類型和縣信息, findAll()工作方式如下:

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

請注意,僅適用於一頁。 你將不得不做其他頁面的網址。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM