Python BeautifulSoup循环 [英] Python BeautifulSoup Loop

查看:118
本文介绍了Python BeautifulSoup循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

借助此板,我设法使用此代码检索了我想要的商品的名称和价格:

Thanks to this board I have managed to retrieve the name and the price of the item I want using this code:

import urllib2  
from BeautifulSoup import BeautifulSoup
import re

html = urllib2.urlopen('http://www.toolventure.co.uk/hand-tools/saws/').read()

soup = BeautifulSoup(html)
item = re.sub('\s+', ' ', soup.h2.a.text)
price = soup.find('p', '*price').text
price = re.search('\d+\.\d+', price).group(0)

print item, price

这很棒,因为它完美地返回了一个结果.继续前进,我现在正在尝试检索页面上的所有结果.我一直在玩循环,但是对此很陌生,无法弄清楚如何循环.

This is great as it returns one result perfectly. Moving on I am now trying to retrieve ALL the results on the page. I have been playing around with loops but am very new to this and am unable to work out how to loop it.

更有知识的人能指出我正确的方向吗?

Can someone more knowledgeable point me in the right direction?

非常感谢

推荐答案

为此,我将使用findAll:

soup = BeautifulSoup(html)

mostwant = {'class': 'productlist_mostwanted_item '}
griditem = {'class': 'productlist_grid_item '}

divs = soup.findAll(attrs = mostwant) + soup.findAll(attrs = griditem)

for product in divs:
    item = product.h2.a.text.strip()
    price = re.search('\d+\.\d+', product.findAll('p')[1].text).group(0)
    print(f"{item} - {price}")

这篇关于Python BeautifulSoup循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆