1

所以我的代码在终端中的工作方式与我想要的完全一样,但我无法让它在 IDLE(随自制软件提供)或 PythonAnywhere 中工作。

当我尝试调用应该收集的数据时,稍后会出现错误,但函数本身不会返回错误。

def GetQuantityNDescription(orderID, itemNum):
    payload = {'login_pass': 'password', 'login_user': 'user','submit':'go'}  ## Log in Paramaters
    r = requests.get("http://website.com/?orderID="+str(orderID), params=payload)  ##Get Order Page

    tree = html.fromstring(r.text)  ## turn raw string into html tagged data

    rawdata = tree.xpath('//*[@class="LargeBody"]')   ## Get raw data found based on class LargeBody
    quantity = []  
    description =[]
    even = True
    for item in rawdata:   ## For each item in rawdata, add it to one of the following lists (always multiple of two)
        #print item.text_content()
        if even == True:  ## First take quanity
            quantity.append(item.text_content())
            even = False
        else:             ## Second take desciption
            description.append(item.text_content())
            even = True
        ## Repeat

    orderInfo = [quantity[itemNum-1], description[itemNum-1]]
    print quantity[itemNum-1]
    print description[itemNum-1]
    return  orderInfo

当我在终端中运行它时,rawdata 返回 [数量项目 1、描述项目 1、数量项目 2、描述项目 2 等]

当我在 IDLE 或 PythonAnywhere 中运行它时,它返回 []。

我收到错误消息:

orderInfo = [quantity[itemNum-1], description[itemNum-1]]
IndexError: list index out of range 

知道这可能是什么原因,或者我如何进行故障排除?我需要能够在我的终端之外运行它。最好在 PythonAnywhere 上。

我在用:

Python 2.7.6

OS X 10.8.5

请求==2.4.3

lxml ==3.3.6

和任何地方的免费版python

编辑::

IDLE 正在使用 Python 2.7.6(显示在 shell 顶部。)

请求正在使用 Python2.7 控制台 869203

终端正在使用 Python 2.7.6($ python --version 返回 Python 2.7.6)

4

1 回答 1

2

我的猜测是,无论出于何种原因,不同的环境都会从网站上得到不同的响应。在解析之前尝试打印响应。

于 2014-11-01T11:34:40.677 回答