美丽的汤环不断失败

2024-04-26 23:31:21 发布

您现在位置:Python中文网/ 问答频道 /正文

编辑:我已经根据需要更改了代码,但是抛出了不同的错误消息。你知道吗

我是一个相对noob美丽的汤和编码到一定程度上,只是寻找一个快速的指针,看看哪里我错了。基本上我是轮胎刮我的网站,并返回价格和产品名称的清单。你知道吗

import csv
from datetime import datetime

quote_page = 'http://www.golfspikesdirect.com/all-golf-spikes/'

page = urllib2.urlopen(quote_page)

soup = BeautifulSoup(page,'html.parser')

product_name = {'class': 'card-title '}
product_price = {'class': 'price--withoutTax '}

divs = soup.findAll(class_ = "card-title") + soup.findAll(class_ = "price--withoutTax")

for product in divs:
    name = product.find(attrs=product_name).text.strip()
    price = product.find(attrs=product_price).text.strip()
    print "%s - (%s)" % (name, price)

Tags: nameimportdatetimetitlepagefindproductcard
1条回答
网友
1楼 · 发布于 2024-04-26 23:31:21

product_nameproduct_price是普通词典,词典没有方法text,但您将它们视为BeautifulSoup方法find。你知道吗

你需要

name = product.find(attrs=product_name).text.strip()

price = product.find(attrs=product_price).text.strip()

相关问题 更多 >