【问题标题】:getting indexerror : list index out of range获取 indexerror:列表索引超出范围
【发布时间】:2014-12-02 22:51:58
【问题描述】:
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)

对不起,这是我得到错误的完整代码。

【问题讨论】:

  • 你应该添加完整的回溯

标签: python-2.7


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2015-01-18
    • 1970-01-01
    • 1970-01-01
    • 2011-10-31
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多