Facebook将错误的数据传递给数据库

时间:2018-05-10 11:50:43

标签: python database postgresql facebook facebook-graph-api

我需要帮助构建一个应用程序,它允许我使用python3将facebook公共数据存储在数据库中,所以来自Facebook的所有数据变成了奇怪的字符串 Weird String而不是python输出中显示的值 enter image description here

我的代码

host = "https://graph.facebook.com/v3.0"
path = "/me"
params = urllib.parse.urlencode({
"access_token": ACCESS_TOKEN
})

url = "{host}{path}?{params}".format(host = host, path = path, params = 
params)
print(url)
resp = urllib.request.urlopen(url).read()

print(resp)

    params = urllib.parse.urlencode({
"fields": 'name',
"access_token": ACCESS_TOKEN
})
url = "{host}{path}?{params}".format(host = host, path = path, params = 
params)
uName = urllib.request.urlopen(url).read()
pprint.pprint(uName)


params = urllib.parse.urlencode({
"fields": 'id',
"access_token": ACCESS_TOKEN
})
url = "{host}{path}?{params}".format(host = host, path = path, params = 
params)
uId = urllib.request.urlopen(url).read()
pprint.pprint(uId)


cur.execute(""
"
insert into COMPANY(ID, NAME, AGE, ADDRESS, SALARY, STRING) VALUES( % s, % 
s, % s, % s, % s, % s)
""
", (temp_id,uName,age,address,salary,uId,))
conn.commit() conn.close()

1 个答案:

答案 0 :(得分:0)

请注意,在python输出中,uName实际上是二进制数据(b'stuff'表示二进制),实际上是JSON。

您需要对其进行解码才能获得文字

uName = uName.decode('utf-8')

然后,如果您需要,decode the json