Это должно получить URL-адреса блога, которым следует ваш tumblr:
import pytumblr
client = pytumblr.TumblrRestClient(...) # replace ... with your credentials
usrFollowing = client.following()
for blog in usrFollowing['blogs']:
print(blog['url'])
Я думаю, что способ понять эту структуру - это один фрагмент за раз.
Вариант usrFollowing
словарь
...
usrFollowing = client.following()
for key in usrFollowing.keys():
print(key)
# outputs:
# blogs
# _links
# total_blogs
Итак, чтобы получить доступ к каждому блогу, мы можем перебрать их, используя ключ blogs
:
...
usrFollowing = client.following()
for blog in usrFollowing['blogs']:
print(blog)
# outputs something like:
# {u'updated': 1539793245, u'uuid': u't:CwoihvyyOxn8Mk5TUS0KDg', u'title': u'Tumblr Engineering', u'url': u'https://engineering.tumblr.com/', u'name': u'engineering', u'description': u'Dispatches from the intrepid tinkerers behind technology at Tumblr.'}
# {u'updated': 1545058816, u'uuid': u't:0aY0xL2Fi1OFJg4YxpmegQ', u'title': u'Tumblr Staff', u'url': u'https://staff.tumblr.com/', u'name': u'staff', u'description': u''}
Существует несколько способов вывода объектов в более "человеческий формат, используйте pprint
или преобразуйте объект в JSON, указав величину отступа:
...
import pprint
import json
print('Python pretty-printed')
for blog in usrFollowing['blogs']:
pprint.pprint(blog)
print('')
print('JSON pretty-printed')
for blog in usrFollowing['blogs']:
print(json.dumps(blog, indent=2))
# outputs something like:
# Python pretty-printed
# {u'description': u'Dispatches from the intrepid tinkerers behind technology at Tumblr.',
# u'name': u'engineering',
# u'title': u'Tumblr Engineering',
# u'updated': 1539793245,
# u'url': u'https://engineering.tumblr.com/',
# u'uuid': u't:CwoihvyyOxn8Mk5TUS0KDg'}
# {u'description': u'',
# u'name': u'staff',
# u'title': u'Tumblr Staff',
# u'updated': 1545058816,
# u'url': u'https://staff.tumblr.com/',
# u'uuid': u't:0aY0xL2Fi1OFJg4YxpmegQ'}
#
# JSON pretty-printed
# {
# "updated": 1539793245,
# "uuid": "t:CwoihvyyOxn8Mk5TUS0KDg",
# "title": "Tumblr Engineering",
# "url": "https://engineering.tumblr.com/",
# "name": "engineering",
# "description": "Dispatches from the intrepid tinkerers behind technology at Tumblr."
# }
# {
# "updated": 1545058816,
# "uuid": "t:0aY0xL2Fi1OFJg4YxpmegQ",
# "title": "Tumblr Staff",
# "url": "https://staff.tumblr.com/",
# "name": "staff",
# "description": ""
# }
В этих словарях есть ключ url
, поэтому вы можете печатать их с помощью:
...
usrFollowing = client.following()
for blog in usrFollowing['blogs']:
print(blog['url'])
# outputs something like:
# https://engineering.tumblr.com/
# https://staff.tumblr.com/