gpt4 book ai didi

python - 值错误: too many values to unpack with JSON File and Requests Post Call

转载 作者:行者123 更新时间:2023-12-01 04:23:46 32 4
gpt4 key购买 nike

我正在尝试在请求后调用的文件中使用 JSON 文件。这不起作用,我得到的错误是:

ValueError: too many values to unpack

这是我的代码:

import requests
import json

def post_to_api(self, spider):
url = 'http://106309.nitrousapp.com:3000/api/v1/imports/batch_create'
headers = {'X-Api-Key': 'TOHhsxPyJnpZcUhLMMqj0XwipSY48PQxCXzIsW8FFpMt', 'Content-Type': 'application/json'}
r = requests.post(url, files=open('items.json', 'r'), headers=headers)
print r.json

这是完整的回溯:

Traceback (most recent call last):
File "/home/nitrous/code/trendomine/local/lib/python2.7/site-packages/twisted/internet/defer.py", line 150, in maybeDeferred
result = f(*args, **kw)
File "/home/nitrous/code/trendomine/local/lib/python2.7/site-packages/scrapy/xlib/pydispatch/robustapply.py", line 57, in robustApply
return receiver(*arguments, **named)
File "/home/nitrous/code/trendomine/fancy/fancy/pipelines.py", line 41, in spider_closed
self.post_to_api(spider)
File "/home/nitrous/code/trendomine/fancy/fancy/pipelines.py", line 15, in post_to_api
r = requests.post(url, files=open('%s_items.json' % spider.name, 'r'), headers=headers)
File "/home/nitrous/code/trendomine/local/lib/python2.7/site-packages/requests/api.py", line 109, in post
return request('post', url, data=data, json=json, **kwargs)
File "/home/nitrous/code/trendomine/local/lib/python2.7/site-packages/requests/api.py", line 50, in request
response = session.request(method=method, url=url, **kwargs)
File "/home/nitrous/code/trendomine/local/lib/python2.7/site-packages/requests/sessions.py", line 454, in request
prep = self.prepare_request(req)
File "/home/nitrous/code/trendomine/local/lib/python2.7/site-packages/requests/sessions.py", line 388, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "/home/nitrous/code/trendomine/local/lib/python2.7/site-packages/requests/models.py", line 296, in prepare
self.prepare_body(data, files, json)
File "/home/nitrous/code/trendomine/local/lib/python2.7/site-packages/requests/models.py", line 444, in prepare_body
(body, content_type) = self._encode_files(files, data)
File "/home/nitrous/code/trendomine/local/lib/python2.7/site-packages/requests/models.py", line 132, in _encode_files
for (k, v) in files:
ValueError: too many values to unpack

我的 JSON 文件非常标准,并且格式正确。这是一个例子:

{
"product":[{"name": "A.P.C. Locker Hoody (Dark Navy)", "country": "USA", "price": "215", "category_old": null, "image_url": "http://thingd-media-ec6.thefancy.com/default/1011706283071373547_87a8a65d3b2a.jpg", "interest": "1", "link": "https://fancy.com/things/1011706283071373547/A.P.C.-Locker-Hoody-(Dark-Navy)", "company": "Fancy"},
{"name": "Darth Vader's Lightsaber MimoPowerTube2 Star Wars Series", "country": "USA", "price": "30", "category_old": null, "image_url": "http://thingd-media-ec1.thefancy.com/default/1011467404649176319_e719110203ae.jpg", "interest": "3", "link": "https://fancy.com/things/1011467404649176319/Darth-Vader's-Lightsaber-MimoPowerTube2-Star-Wars-Series", "company": "Fancy"},
{"name": "Boba Fett MimoPowerTube2 Star Wars Series", "country": "USA", "price": "30", "category_old": null, "image_url": "http://thingd-media-ec3.thefancy.com/default/1011472861069581593_f2390073e692.jpg", "interest": "2", "link": "https://fancy.com/things/1011472861069581593/Boba-Fett-MimoPowerTube2-Star-Wars-Series", "company": "Fancy"},
{"name": "Funko POP! Iron Man Ceramic Mug", "country": "USA", "price": "17", "category_old": null, "image_url": "http://thingd-media-ec4.thefancy.com/default/1011471256807021839_7b11976ede50.jpg", "interest": "1", "link": "https://fancy.com/things/1011471256807021839/Funko-POP!-Iron-Man-Ceramic-Mug", "company": "Fancy"},
{"name": "Charcoal Boiled Wool Sweatpant by Apolis", "country": "USA", "price": "248", "category_old": "Men", "image_url": "http://thingd-media-ec1.thefancy.com/default/986743205628222511_1853b853a79b.jpg", "interest": "2", "link": "https://fancy.com/things/986743205628222511/Charcoal-Boiled-Wool-Sweatpant-by-Apolis", "company": "Fancy"},
{"name": "Khaki Convertible Backpack by Hex", "country": "USA", "price": "100", "category_old": "Men", "image_url": "http://thingd-media-ec3.thefancy.com/default/986174222386597367_42a175cb4c86.jpg", "interest": "4", "link": "https://fancy.com/things/986174222386597367/Khaki-Convertible-Backpack-by-Hex", "company": "Fancy"},
{"name": "Funko POP! Hulk Ceramic Mug", "country": "USA", "price": "17", "category_old": null, "image_url": "http://thingd-media-ec1.thefancy.com/default/1011471115609971981_831322b6e212.jpg", "interest": "None", "link": "https://fancy.com/things/1011471115609971981/Funko-POP!-Hulk-Ceramic-Mug", "company": "Fancy"},
{"name": "Coolest Cooler", "country": "USA", "price": "485", "category_old": "Food", "image_url": "http://thingd-media-ec3.thefancy.com/default/671492680575882582_be703be1c98c.jpg", "interest": "139", "link": "https://fancy.com/things/671492680575882582/Coolest-Cooler-po", "company": "Fancy"},
{"name": "Funko POP! Spider-Man Ceramic Mug", "country": "USA", "price": "17", "category_old": null, "image_url": "http://thingd-media-ec4.thefancy.com/default/1011473609744456989_f62ee350ecbf.jpg", "interest": "1", "link": "https://fancy.com/things/1011473609744456989/Funko-POP!-Spider-Man-Ceramic-Mug", "company": "Fancy"},
{"name": "Funko POP! Spider-Man Salt & Pepper Shaker Set", "country": "USA", "price": "22", "category_old": null, "image_url": "http://thingd-media-ec1.thefancy.com/default/1011477384920046901_2a177ed28ef6.jpg", "interest": "None", "link": "https://fancy.com/things/1011477384920046901/Funko-POP!-Spider-Man-Salt-&-Pepper-Shaker-Set", "company": "Fancy"},
{"name": "Get Out Of My Life Tee", "country": "USA", "price": "37", "category_old": null, "image_url": "http://thingd-media-ec3.thefancy.com/default/1011529914114907917_071fb0dd5655.jpg", "interest": "1", "link": "https://fancy.com/things/1011529914114907917/Get-Out-Of-My-Life-Tee", "company": "Fancy"},
{"name": "Fur Real Greeting Card", "country": "USA", "price": "5", "category_old": null, "image_url": "http://thingd-media-ec5.thefancy.com/default/1011526354610429681_3718a08c1cd9.jpg", "interest": "2", "link": "https://fancy.com/things/1011526354610429681/Fur-Real-Greeting-Card", "company": "Fancy"},
{"name": "Geodesic Playhouse", "country": "USA", "price": "300", "category_old": "Kids", "image_url": "http://thingd-media-ec4.thefancy.com/default/955512370883461577_260ed730ea0e.jpg", "interest": "13", "link": "https://fancy.com/things/955512370883461577/Geodesic-Playhouse", "company": "Fancy"},
{"name": "Catnipped Tank Top", "country": "USA", "price": "24", "category_old": null, "image_url": "http://thingd-media-ec3.thefancy.com/default/1011535748509212461_e0ebf6f78f6a.jpg", "interest": "3", "link": "https://fancy.com/things/1011535748509212461/Catnipped-Tank-Top", "company": "Fancy"},
{"name": "Creep It Real Crewneck", "country": "USA", "price": "26", "category_old": null, "image_url": "http://thingd-media-ec3.thefancy.com/default/1011538479613484857_f4419054bbfa.jpg", "interest": "2", "link": "https://fancy.com/things/1011538479613484857/Creep-It-Real-Crewneck", "company": "Fancy"},
{"name": "Harvard Naw Crewneck", "country": "USA", "price": "50", "category_old": null, "image_url": "http://thingd-media-ec6.thefancy.com/default/1011555628595814261_0e08275d489e.jpg", "interest": "2", "link": "https://fancy.com/things/1011555628595814261/Harvard-Naw-Crewneck", "company": "Fancy"},
{"name": "QBracelet Smartphone Charger", "country": "USA", "price": "110", "category_old": "Gadgets", "image_url": "http://thingd-media-ec2.thefancy.com/default/698272167082598052_01939a626fe5.jpg", "interest": "18", "link": "https://fancy.com/things/698272167082598052/QBracelet-Smartphone-Charger-po", "company": "Fancy"},
{"name": "F*ck Off Crop Tank", "country": "USA", "price": "30", "category_old": null, "image_url": "http://thingd-media-ec3.thefancy.com/default/1011572077062266779_84283d6694da.jpg", "interest": "1", "link": "https://fancy.com/things/1011572077062266779/F*ck-Off-Crop-Tank", "company": "Fancy"},
{"name": "Black Cat-O-Lantern Tee", "country": "USA", "price": "35", "category_old": null, "image_url": "http://thingd-media-ec2.thefancy.com/default/1011559329523834755_0c5541944a26.jpg", "interest": "1", "link": "https://fancy.com/things/1011559329523834755/Black-Cat-O-Lantern-Tee", "company": "Fancy"},
{"name": "Monday Blues Ringer Tee", "country": "USA", "price": "42", "category_old": null, "image_url": "http://thingd-media-ec1.thefancy.com/default/1011544620326199127_9d35a8431b9b.jpg", "interest": "1", "link": "https://fancy.com/things/1011544620326199127/Monday-Blues-Ringer-Tee", "company": "Fancy"},
{"name": "Pinemeowpple Crop Top", "country": "USA", "price": "26", "category_old": null, "image_url": "http://thingd-media-ec4.thefancy.com/default/1011563553640947595_83b04d570969.jpg", "interest": "5", "link": "https://fancy.com/things/1011563553640947595/Pinemeowpple-Crop-Top", "company": "Fancy"},
{"name": "Gasometro Ring", "country": "USA", "price": "199", "category_old": "Women", "image_url": "http://thingd-media-ec5.thefancy.com/default/1011573460410834847_1420988eda40.jpg", "interest": "2", "link": "https://fancy.com/things/1011573460410834847/Gasometro-Ring", "company": "Fancy"},
{"name": "Matit\u00e0 Coat Rack", "country": "USA", "price": "199", "category_old": "Home", "image_url": "http://thingd-media-ec2.thefancy.com/default/1011576991335325607_5aebe93b44ec.jpg", "interest": "4", "link": "https://fancy.com/things/1011576991335325607/Matit%C3%A0-Coat-Rack", "company": "Fancy"},
{"name": "Mystical Fire Colorant", "country": "USA", "price": "75", "category_old": "Sports & Outdoors", "image_url": "http://thingd-media-ec1.thefancy.com/default/701418813568058872_5171d5c216c7.jpg", "interest": "113", "link": "https://fancy.com/things/701418813568058872/Mystical-Fire-Colorant", "company": "Fancy"}]}

最佳答案

您没有设置files argument正确。替换:

files=open('items.json', 'r')

与:

files={'file': open('items.json', 'r')}

关于python - 值错误: too many values to unpack with JSON File and Requests Post Call,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33383102/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com