Facebook Python“ ValueError:太多值要解压缩“ [英] Facebook Python " ValueError: too many values to unpack"

查看:133
本文介绍了Facebook Python“ ValueError:太多值要解压缩“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下是我的代码。

  import csv 
import json
import urllib
import sys
import time
import re

class FacebookSearch:
def __init __(self,
query ='https://graph.facebook.com/search.{mode}?{query}&{access_token}'
):
access_token ='XXXXXX | XXXXX'

def search(self,q,mode ='json',** queryargs):
queryargs ['q'] = q
查询= urllib.urlencode(queryargs)
返回查询


def write_csv(fname,rows,header = None,append = False,** kwargs):
filemode ='ab',如果附加其他'wb'
with open(fname,filemode)as outf:
out_csv = csv.writer(outf,** kwargs)
如果标题:
out_csv.writerow(header)
out_csv.writerows(rows)

def main():
ts = FacebookSearch()
response,dat a = ts.search('appliance',type ='post')##这里是我收到错误的地方。
js = json.loads(data)



messages =([msg ['created_time'],msg ['id']] .get('data',[]))

write_csv('fb_washerdryer.csv',messages,append = True)

如果__name__ =='__main__':
main()

这是错误回溯的错误:



追溯(最近的最后一次通话):
文件./facebook_washer_dryer1.sh,第43行,
main()
文件./facebook_washer_dryer1 .sh,第33行,主要
响应,data = ts.search('appliance',type ='post')
ValueError:太多值要解压缩

解决方案

您的 FacebookSearch.search 方法返回一个值,一个查询字符串来粘贴到URL上。 / p>

但是当你打电话时,你试图将结果解压缩成两个变量:

  response,data = ts.search('appliance',type ='post')

这不行。



所以,为什么错误说太多价值而不是太少?那么一个字符串实际上是一个单字符串序列,所以它试图把这个单个字符串解压成几个不同的值,每个字符一个。






但是,这里有一个更大的问题。您明确 您的搜索方法返回响应和一些数据,但即使远程也不返回任何东西像一个响应和一些数据,它返回一个查询字符串。我想你实际上是要使用查询字符串构建一个URL,然后下载该URL,然后返回该下载的结果。



除非你编写的代码实际上是尝试这样做的(这可能意味着更改 __ init __ 来存储 self.query self.access_token ,使用 self.query.format 搜索中使用 urllib2.urlopen 在生成的字符串和一些其他更改),其余的代码不会做任何有用的。



如果你想存根 FacebookSearch 现在,所以你可以测试其余的代码,你需要使它返回适当假的数据,你的其余代码可以使用。例如,您可以这样做:

  def search(self,q,mode ='json',** queryargs): 
queryargs ['q'] = q
query = urllib.urlencode(queryargs)
#TODO:实际查询
返回200,'{假:数据}'


I am new to programing and Python..

Below is my code.

import csv
import json
import urllib
import sys
import time
import re

class FacebookSearch:
    def __init__(self,
        query   = 'https://graph.facebook.com/search.{mode}?{query}&{access_token}'
    ):
        access_token = 'XXXXXX|XXXXX'

    def search(self, q, mode='json', **queryargs):
        queryargs['q'] = q
        query = urllib.urlencode(queryargs)
        return query


def write_csv(fname, rows, header=None, append=False, **kwargs):
    filemode = 'ab' if append else 'wb'
    with open(fname, filemode) as outf:
        out_csv = csv.writer(outf, **kwargs)
        if header:
            out_csv.writerow(header)
        out_csv.writerows(rows)

def main():
    ts = FacebookSearch()
    response, data = ts.search('appliance', type='post')  ## here is where I am getting the error.
    js = json.loads(data)



    messages = ([msg['created_time'],  msg['id']] for msg in js.get('data', []))

    write_csv('fb_washerdryer.csv', messages, append=True)

if __name__ == '__main__':
    main()

Here is the trace back on the error:

Traceback (most recent call last): File "./facebook_washer_dryer1.sh", line 43, in main() File "./facebook_washer_dryer1.sh", line 33, in main response, data = ts.search('appliance', type='post') ValueError: too many values to unpack

解决方案

Your FacebookSearch.search method returns a single value, a query string to tack onto a URL.

But when you call it, you're trying to unpack the results to two variables:

response, data = ts.search('appliance', type='post')

And that doesn't work.

So, why does the error say "too many values" instead of "too few"? Well, a string is actually a sequence of single-character strings, so it's trying to unpack that single string into dozens of separate values, one for each character.


However, you've got a much bigger problem here. You clearly expected your search method to return a response and some data, but it doesn't return anything even remotely like a response and some data, it returns a query string. I think you wanted to actually build a URL with the query string, then download that URL, then return the results of that download.

Unless you write code that actually attempts to do that (which probably means changing __init__ to store self.query and self.access_token, using self.query.format in search, using urllib2.urlopen on the resulting string, and a bunch of other changes), the rest of your code isn't going to do anything useful.

If you want to "stub out" FacebookSearch for now, so you can test the rest of your code, you need to make it return appropriate fake data that the rest of your code can work with. For example, you could do this:

def search(self, q, mode='json', **queryargs):
    queryargs['q'] = q
    query = urllib.urlencode(queryargs)
    # TODO: do the actual query
    return 200, '{"Fake": "data"}'

这篇关于Facebook Python“ ValueError:太多值要解压缩“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆