【发布时间】:2017-12-13 15:16:26
【问题描述】:
我正在尝试从我抓取的这些数据中获取特定列表以将其转换为 Pandas 数据框,但我收到以下错误:
TypeError: 列表索引必须是整数或切片,而不是 str
这是数据;
r = requests.get(url).json()
print(r)
输出:
[{'category': {'id': 34,
'name': 'Tech',
'shortname': 'tech',
'sort_name': 'Tech'},
'city': 'Edinburgh',
'country': 'GB',
'created': 1450173286000,
'description': "<p>We're passionate about security, as are you.</p>\n<p>We want to invite the security community in Scotland to engage 5 or 6 times a year to discuss all things security. A informal forum to share ideas, make contacts, encourage debate.</p>\n<p>Our MeetUps will 100% NOT be sales-led. There will be no vendors, no sponsors, no obligation to talk to anyone, nor cost any money to attend.</p>\n<p>They will be hosted at a number of venues, but there will be no hosting-company focus, we merely organise and host the events, with a choice of speakers as well as the obligatory refreshments!</p>",
'id': 19213863,
'join_mode': 'open',
'key_photo': {'base_url': 'https://secure.meetupstatic.com',
'highres_link': 'https://secure.meetupstatic.com/photos/event/d/e/c/4/highres_445137028.jpeg',
'id': 445137028,
'photo_link': 'https://secure.meetupstatic.com/photos/event/d/e/c/4/600_445137028.jpeg',
'thumb_link': 'https://secure.meetupstatic.com/photos/event/d/e/c/4/thumb_445137028.jpeg',
'type': 'event'},
'lat': 55.94,
'link': 'https://www.meetup.com/Security-MeetUp-Scotland/',
'localized_country_name': 'United Kingdom',
'localized_location': 'Edinburgh, United Kingdom',
'lon': -3.2,
'members': 1059,
'meta_category': {'category_ids': [34],
'id': 292,
'name': 'Tech',
'photo': {'base_url': 'https://secure.meetupstatic.com',
'highres_link': 'https://secure.meetupstatic.com/photos/event/2/e/a/d/highres_450131949.jpeg',
'id': 450131949,
'photo_link': 'https://secure.meetupstatic.com/photos/event/2/e/a/d/600_450131949.jpeg',
'thumb_link': 'https://secure.meetupstatic.com/photos/event/2/e/a/d/thumb_450131949.jpeg',
'type': 'event'},
'shortname': 'tech',
'sort_name': 'Tech'},
'name': 'Security MeetUp Scotland',
'next_event': {'id': '245752465',
'name': 'Security Scotland Chapter 10 - hosted by Skyscanner!',
'time': 1516820400000,
'utc_offset': 0,
'yes_rsvp_count': 130},
'organizer': {'bio': 'I do Security stuff. Currently at Capital One.\nOrganiser of Security Scotland: https://www.meetup.com/Security-MeetUp-Scotland\nHusband, proud daddy, guitarist, drummer, muso, ex-DJ/producer, Fleetwood Mac aficionado, Scottish Leeds fan.',
'id': 192768669,
'name': 'Stu Hirst',
'photo': {'base_url': 'https://secure.meetupstatic.com',
'highres_link': 'https://secure.meetupstatic.com/photos/member/8/d/9/7/highres_250956247.jpeg',
'id': 250956247,
'photo_link': 'https://secure.meetupstatic.com/photos/member/8/d/9/7/member_250956247.jpeg',
'thumb_link': 'https://secure.meetupstatic.com/photos/member/8/d/9/7/thumb_250956247.jpeg',
'type': 'member'}},
'score': 1.0,
'state': 'U8',
'status': 'active',
'timezone': 'Europe/London',
'urlname': 'Security-MeetUp-Scotland',
'visibility': 'public',
'who': 'Scot Security Folks'},
{'category': {'id': 34, ...
]
我知道这有很多词典,我想获取主要的。我试过这样;
for item in r['category']:
print (item['name'])
print (item['city'])
print (item['members'])
for item in r['meta_category']:
print (item['name'])
print (item['country'])
print (item['status'])
还有更多,但那是我收到错误的时候。你能帮我用'name', 'city, 'country', 'lat', 'lon', 'description', 'members', 'status', 'url-name'从'category'和'meta_category'创建一个DataFrame吗
【问题讨论】:
-
数据是否来自api.meetup.com/2/…?
-
另外,还有一件事。您可能还没有检查或理解响应的结构。从您的代码中,这将永远不会起作用。看看
r['results'],看看你会得到什么。 -
是的,就是这个
-
另外,我看到了
results。results内部是一个字典列表。在每个字典中,我只看到category。 -
嗯...与
r['results']我得到TypeError: list indices must be integers or slices, not str
标签: python json pandas dictionary dataframe