python - 迭代页面元素 beautifulsoup

标签 python loops web-scraping beautifulsoup element

我正在尝试为一个受欢迎的汽车网站构建一个快速抓取工具。我可以返回一辆车的结果,但我不知道如何返回页面上的所有汽车。 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

请注意,仅适用于一页。您将必须执行其他页面的网址。

关于python - 迭代页面元素 beautifulsoup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36421119/

相关文章:

python - 如何去除列表中的 unicode

python本地模块

c - 我无法理解 for 循环 block 和 if 语句

c - printf 打印额外的 * 字符

c - Else 语句在 strcmp 中返回错误结果(比较哈希值)(已更新)

python - BeautifulSoup:获取特定表的内容

python - 将 url 解码为 utf-8-sig 时起始字节无效

python - psycopg2 "select for update"

javascript - 如何使用 PhantomJS 更改选择值

python - 如何在 DynamoDB 中立即获取表的行数?