我在python中使用facebook.graphAPI搜索某些特定城市的餐馆。它返回的结果是DICT类型。
我的代码:
import facebook
import json
# get access_token from https://developers.facebook.com/tools/access_token/
graph = facebook.GraphAPI(access_token='EAAHuWhpN91gBAEcZCammDhlB2w0Q4FO1TH1J09IfZBBtJE8ruu78KNjdOxu38AOp9GxB0gv5YlSetJ70WLscYmMkpY2SrQkGWRQcojaZBZCb52kkHKs1yE868DJ9MEjrCRIMfeVZBVRwNfuT27gUFhaVomOzpyd3lHxYelx82qQZDZD', version='2.7')
profile = graph.get_object("me")
print profile
rests=graph.request('search', {'q': 'Restaurants in Lahore', 'type': 'place'})
print type(rests)
for key in rests:
print "key: %s , value: %s" % (key, rests[key])
#TO get posts of Restaurant with id 816463355086343
posts = graph.get_connections('816463355086343', 'posts')
结果如下图所示。显然不可读。 enter image description here
所以我试着让它可读。看起来像这样
key: data , value:
[
{
u'category': u'Hotel',
u'id': u'106037262807337',
u'location':
{
u'city': u'Lahore',
u'zip': u'',
u'country': u'Pakistan',
u'longitude': 74.3356207,
u'state': u'',
u'street': u'PC Hotel Lahore, Mall Road Pakistan',
u'latitude': 31.5480206
},
u'category_list':
[
{
u'id': u'165679780146824',
u'name': u'Food & Restaurant'
},
{
u'id': u'180699075298665',
u'name': u'Hospitality Service'
},
{
u'id': u'164243073639257',
u'name': u'Hotel'
}
],
u'name': u'Pearl Continental, Lahore'
},
{
u'category': u'Local business',
u'id': u'125484227592406',
u'location':
{
u'city': u'Lahore',
u'zip': u'',
u'country': u'Pakistan',
u'longitude': 74.417206363636,
u'state': u'',
u'street': u'masjad chok',
u'latitude': 31.612784545455
},
u'category_list':
[
{
u'id': u'150534008338515',
u'name': u'Barbecue Restaurant'
}
],
u'name': u'Lahore Defence'
},
....
我想把结果存入数据库。我不知道怎么做?请帮帮我,我们将不胜感激。
所以我需要关于如何使用数据库驱动程序来存储和检索数据的说明
首先要有一个可访问的数据库服务器。或者在本地设置一个,或者如果你有办法,远程设置一个。 您附加到问题的图像看起来像一个windows终端,因此请下载MySQL社区服务器for windowshere
设置应该相当直接。 保留您的登录凭据(最常用的是->;username:root, password:root)安全的地方。系统会询问您是否需要密码,以及您需要哪个密码
当服务器启动并运行时,默认情况下它将在
127.0.0.1:3306
上可用。现在,如果您对MySQL有一定的经验,您将知道如何用一些表来设置一个基本的数据库,以便将数据放入其中(我想你有。)一旦启动并运行,通过执行以下命令安装mysql for python
pip install MySQL-python
(据我所知,这适用于python2.7,对python3+使用pip install mysqlclient
)。这将安装一个可以在代码中使用的python库。只需在代码顶部为您选择的库添加import语句:import <library_name>
现在我无法给出确切的代码,因为我知道您使用的是哪个python版本,但是无论您选择哪个版本,都会有关于如何使用它的文档
具体步骤如下:
相关问题 更多 >
编程相关推荐