获取indexerror:列表索引超出范围

时间:2014-12-02 22:51:58

标签: 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()
      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)

很抱歉,这是完整代码,我收到错误。

1 个答案:

答案 0 :(得分:0)

那是因为页面中提到的课程。 尝试表示常量的查询字词。