gpt4 book ai didi

python - Facebook 将不正确的数据传递给数据库

转载 作者:行者123 更新时间:2023-11-29 13:44:46 25 4
gpt4 key购买 nike

我需要帮助构建一个应用程序,让我使用 python3 将 facebook 公共(public)数据存储在数据库中,结果来自 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()

最佳答案

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

你需要解码它才能得到文本

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

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

关于python - Facebook 将不正确的数据传递给数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50272309/

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