在python上使用REST API从wordpress网站提取数据时出现错误

时间:2018-11-22 09:26:37

标签: python-3.x wordpress-rest-api

任何人都可以通过woocommerce REST API for Python帮助我吗?我只需要从woocommerce网站提取数据,但显示错误。 代码是

from wordpress import API as WAPI
import time
import json

wpapi = WAPI(
url="https://xxxxk",
consumer_key="xxxxxx",
consumer_secret="xxxxxxx",
api="wp-json",
version="wp/v2",
wp_user="xxxx",
wp_pass="xxxxx",
oauth1a_3leg=True,
callback='https://xxxxxx',
creds_store="~/.wc-api-creds.json"
)

from woocommerce import API

wcapi = API(
url="xxxxxxxx",
consumer_key="xxxxxxx",
consumer_secret="xxxxxxxx",
wp_user="xxxx",
wp_pass="xxxx",
wp_api=True, # Enable the WP REST API integration
version="wc/v3", # WooCommerce WP REST API version
timeout=60
)

dataToFetch = 'products'
r = wcapi.get(dataToFetch).json()

with open(dataToFetch + '.json', 'w') as outfile: 
json.dump(r, outfile,indent = 4, ensure_ascii = False)

with open(dataToFetch + '.json') as infile:
data = json.load(infile)
print(infile)

错误是

Traceback (most recent call last):
File "C:\Users\Arsalan\AppData\Local\Programs\Python\Python37\MY WORK FILES\REST API for building materials.py", line 63, in <module>
r = wcapi.get(dataToFetch).json()
File "C:\Users\Arsalan\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\models.py", line 897, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Users\Arsalan\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\Arsalan\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\Arsalan\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 5 (char 4)
>>>

请指出错误并提供解决方案。预先感谢。

0 个答案:

没有答案