Python 2.7 获取索引器:列表索引超出范围 从美化组导入美化组 进口机械化 进口稀土 def价格与沃尔玛匹配(关键字): url=”http://www.walmart.com/search/?query=“+关键字 br=mechanize.Browser() br.设置手柄机器人(错误) br.打开(url) html=br.response().read() 结果\u soup=BeautifulSoup(html) find_results=result_soup.findAll('div',attrs={'class':'js-tile-landscape'}) 所有结果=[] 对于查找结果中的结果: title=result.find('a',attrs={'class':'js-product-title'}) links=result.find('a',href=True) 在_decimal=str(result.find('span',attrs={'class':'price-price-display'})之前。拆分('$')[1]。拆分(''.[0] 在_decimal=str(result.find('span',attrs={'class':'price display'})之后。拆分(“”)[1]。拆分(“”)[0] 价格=小数点前+'。+小数点后 inArray=[浮动(价格),”http://www.walmart.com“+链接['href']] 所有结果。追加(inArray) 打印(所有结果)
很抱歉,我得到的错误是完整的代码。这是因为所提到的类在页面中不存在。Python 2.7 获取索引器:列表索引超出范围 从美化组导入美化组 进口机械化 进口稀土 def价格与沃尔玛匹配(关键字): url=”http://www.walmart.com/search/?query=“+关键字 br=mechanize.Browser() br.设置手柄机器人(错误) br.打开(url) html=br.response().read() 结果\u soup=BeautifulSoup(html) find_results=result_soup.findAll('div',attrs={'class':'js-tile-landscape'}) 所有结果=[] 对于查找结果中的结果: title=result.find('a',attrs={'class':'js-product-title'}) links=result.find('a',href=True) 在_decimal=str(result.find('span',attrs={'class':'price-price-display'})之前。拆分('$')[1]。拆分(''.[0] 在_decimal=str(result.find('span',attrs={'class':'price display'})之后。拆分(“”)[1]。拆分(“”)[0] 价格=小数点前+'。+小数点后 inArray=[浮动(价格),”http://www.walmart.com“+链接['href']] 所有结果。追加(inArray) 打印(所有结果),python-2.7,Python 2.7,很抱歉,我得到的错误是完整的代码。这是因为所提到的类在页面中不存在。 尝试表示常量的查询项您应该添加完整的回溯 from BeautifulSoup import BeautifulSoup import mechanize import re def price_walmart_match(keyword): url = "http://www.walmart.com/search/?query="+keyword br = mechanize.Browser()
尝试表示常量的查询项您应该添加完整的回溯
from BeautifulSoup import BeautifulSoup
import mechanize
import re
def price_walmart_match(keyword):
url = "http://www.walmart.com/search/?query="+keyword
br = mechanize.Browser()
br.set_handle_robots(False)
br.open(url)
html = br.response().read()
result_soup = BeautifulSoup(html)
found_results = result_soup.findAll('div' , attrs={'class':'js-tile tile-landscape'})
all_results=[]
for result in found_results:
title = result.find('a' , attrs={'class':'js-product-title'})
links = result.find('a' , href=True)
before_decimal= str(result.find('span', attrs={'class':'price price-display'})).split('<span class="sup">$</span>')[1].split('<span class="visuallyhidden">')[0]
after_decimal= str(result.find('span', attrs={'class':'price price-display'})).split('</span><span class="sup">')[1].split('</span>')[0]
prices = before_decimal+'.'+after_decimal
inArray = [float(prices), "http://www.walmart.com"+links['href']]
all_results.append(inArray)
print(all_result)