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

ターミナルで実行すると、生データが返されます [数量項目 1、説明項目 1、数量項目 2、説明項目 2 など]

IDLEまたはPythonAnywhereで実行すると、[]が返されます。

エラーが発生しています:

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

これの原因は何か、またはトラブルシューティングの方法はありますか? これを端末の外で実行できる必要があります。できればPythonAnywhereで。

私は使っている:

パイソン 2.7.6

OS X 10.8.5

リクエスト==2.4.3

lxml ==3.3.6

pythonの無料版はどこでも

編集::

IDLE は Python 2.7.6 を使用しています (シェルの上に表示されます)。

リクエストは Python2.7 コンソールを使用しています 869203

ターミナルは Python 2.7.6 を使用しています ($ python --version は Python 2.7.6 を返します)

4

1 に答える 1

2

なんらかの理由で、環境が異なれば Web サイトからの応答も異なると思います。応答を解析する前に出力してみてください。

于 2014-11-01T11:34:40.677 に答える