python代码在本地运行良好,但在AWS EC2(ubuntu)中运行不佳

时间:2018-12-17 07:54:52

标签: python json ubuntu amazon-ec2 python-requests

我正在尝试使用AWS EC2上的请求模块解析网站。

import requests
some_data = {'a':'',
             'b':''}
with requests.Session() as s:
    result = s.post('http://website.com',data=some_data)
    print(result.text)

页面的响应如下:

{
    "arrangetype":"U",
    "list": [
        {
            "product_no":43,
            "display_order":4,
            "is_selling":"T",
            "product_empty":"F",
            "fix_position":null,
            "is_auto_sort":false
        },
        {
            "product_no":44,
            "display_order":6,
            "is_selling":"T",
            "product_empty":"F",
            "fix_position":null,
            "is_auto_sort":false
        }
    ],
    "length":2
}

因此,我在Roadrunner的帮助下做了以下工作(parsing and getting list from response of get request

import requests
from json import loads
some_data = {'a':'',
             'b':''}

with requests.Session() as s:
    result = s.post('http://website.com',data=some_data)
    json_dict = loads(result.text)
    print([x["product_no"] for x in json_dict["list"]])

它可以在本地PC(windows10)上完美运行,但是现在我正在AWS EC2上运行代码,但是会发生错误。 该文件的名称为timesale.py btw。

Traceback (most recent call last):
  File "timesale.py", line 657, in <module>
    Timesale_Automation()
  File "timesale.py", line 397, in Timesale_Automation
    status_check()
  File "timesale.py", line 345, in status_check
    json_dict = loads(now_sale_page2.text)
  File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.6/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.6/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

我不知道为什么会发生此错误。 我查找了overstackflow,并且有一些有关解码json的文章,但是

result.text.decode('utf-8')

返回,我无法解码str ....

有趣的是,我在代码之间加上了print(result),但它不是“ None” 为什么会这样?

0 个答案:

没有答案