python json delete first element list or tuple - python

This is my part of code:
script = soup.find("script", {"id":"product-form-data"})
data = json.loads(script.text)
all_p = data["products"]
for x in all_p.items():
print(x)
I have a problem with this json data. I'm not able to remove everything outside the {} because like this i dont know hot to get id, name and unitsAvailable.
This is my output:
(u'193654', {u'unitsAvailable': 2, u'id': 193654, u'name': u'US 10,5'})
(u'193655', {u'unitsAvailable': 1, u'id': 193655, u'name': u'US 11'})
(u'193656', {u'unitsAvailable': 1, u'id': 193656, u'name': u'US 11,5'})
(u'193650', {u'unitsAvailable': 2, u'id': 193650, u'name': u'US 8,5'})
(u'193651', {u'unitsAvailable': 3, u'id': 193651, u'name': u'US 9'})
(u'193652', {u'unitsAvailable': 2, u'id': 193652, u'name': u'US 9,5'})
(u'193653', {u'unitsAvailable': 1, u'id': 193653, u'name': u'US 10'})
(u'193647', {u'unitsAvailable': 1, u'id': 193647, u'name': u'US 7'})

When using the items() methods you ask the dictionary to return a list of tuple combining the key and its value (key, value) Python Dictionary | items() method
To sort key and value, use instead:
for key, value in all_p.items():
print(value)

Related

How do I populate my dictionary keys so that it gives me the value of the variable rather than the string?

I want to organise my data so I can view/access my chunkId's associated with each taskId. Each taskId has multiple chunkId's under it. I am thinking a dictionary, with taskId as key and chunkId as value is the way to go.
How do I populate my dictionary keys so that it gives me the value of the variable taskId, rather than the string 'taskId'?
My code:
result = {u'content': [{u'status': u'succeeded', u'task': [{u'id': 20170913000001}], u'id': 51924665}, {u'status': u'succeeded', u'task': [{u'id': 20170913000002}], u'id': 51924675}, {u'status': u'succeeded', u'task': [{u'id': 20170913000003}], u'id': 51924684}, {u'status': u'succeeded', u'task': [{u'id': 20170913000004}], u'id': 51924689}, {u'status': u'succeeded', u'task': [{u'id': 20170913000005}], u'id': 51924692}], u'next': None, u'message': None, u'previous': None, u'error': None}
for chunkData in result['content']:
chunkId = chunkData['id']
for taskData in chunkData['task']:
taskId = taskData['id']
dicto = dict(taskId=chunkId)
print dicto
Output:
{'taskId': 51924665}
{'taskId': 51924675}
{'taskId': 51924684}
{'taskId': 51924689}
{'taskId': 51924692}
Don't use the type-contructor, use a literal:
dicto = {taskId:chunkId}

Python :: nested JSON result in Spotify

I'm having a really hard time to get a track id in Spotify search endpoint.
It is deeply nested.
So, if I do this:
results = sp.search(q='artist:' + 'Nirvava + ' track:' + 'Milk it', type='track')
pprint.pprint(results)
I am able to get:
{u'tracks': {u'href': u'https://api.spotify.com/v1/search?query=artist%3ANirvana+track%3AMilk+it&type=track&offset=0&limit=10',
u'items': [{u'album': {u'album_type': u'album',
u'artists': [{u'external_urls': {u'spotify': u'https://open.spotify.com/artist/6olE6TJLqED3rqDCT0FyPh'},
u'href': u'https://api.spotify.com/v1/artists/6olE6TJLqED3rqDCT0FyPh',
u'id': u'6olE6TJLqED3rqDCT0FyPh',
u'name': u'Nirvana',
u'type': u'artist',
u'uri': u'spotify:artist:6olE6TJLqED3rqDCT0FyPh'}],
u'available_markets': [u'CA',
u'MX',
u'US'],
u'external_urls': {u'spotify': u'https://open.spotify.com/album/7wOOA7l306K8HfBKfPoafr'},
u'href': u'https://api.spotify.com/v1/albums/7wOOA7l306K8HfBKfPoafr',
u'id': u'7wOOA7l306K8HfBKfPoafr',
u'images': [{u'height': 640,
u'url': u'https://i.scdn.co/image/3dd2699f0fcf661c35d45745313b64e50f63f91f',
u'width': 640},
{u'height': 300,
u'url': u'https://i.scdn.co/image/a6c604a82d274e4728a8660603ef31ea35e9e1bd',
u'width': 300},
{u'height': 64,
u'url': u'https://i.scdn.co/image/f52728b0ecf5b6bfc998dfd0f6e5b6b5cdfe73f1',
u'width': 64}],
u'name': u'In Utero - 20th Anniversary Remaster',
u'type': u'album',
u'uri': u'spotify:album:7wOOA7l306K8HfBKfPoafr'},
u'artists': [{u'external_urls': {u'spotify': u'https://open.spotify.com/artist/6olE6TJLqED3rqDCT0FyPh'},
u'href': u'https://api.spotify.com/v1/artists/6olE6TJLqED3rqDCT0FyPh',
u'id': u'6olE6TJLqED3rqDCT0FyPh',
u'name': u'Nirvana',
u'type': u'artist',
u'uri': u'spotify:artist:6olE6TJLqED3rqDCT0FyPh'}],
u'available_markets': [u'CA', u'MX', u'US'],
u'disc_number': 1,
u'duration_ms': 234746,
u'explicit': False,
u'external_ids': {u'isrc': u'USGF19960708'},
u'external_urls': {u'spotify': u'https://open.spotify.com/track/4rtZtLpriBscg7zta3TZxp'},
u'href': u'https://api.spotify.com/v1/tracks/4rtZtLpriBscg7zta3TZxp',
u'id': u'4rtZtLpriBscg7zta3TZxp',
u'name': u'Milk It',
u'popularity': 43,
u'preview_url': None,
u'track_number': 8,
u'type': u'track',
-----> u'uri':u'spotify:track:4rtZtLpriBscg7zta3TZxp'},
QUESTION:
now, how do I fetch the last 'uri' (u'uri': u'spotify:track:4rtZtLpriBscg7zta3TZxp'}, under the name 'Milk It'?
>>> print results['tracks']['items'][0]['uri']
spotify:track:4rtZtLpriBscg7zta3TZxp

Paginate and collect posts and comments from Facebook (Python, Json) [duplicate]

I'm trying to pull posts and comments from Facebook for a project, but can't seem to pull everything. It seems that I get two links with previous and next
Here's my code:
import facebook
import requests
def some_action(post):
#print posts['data']
print(post['created_time'])
access_token = INSERT ACCESS TOKEN HERE
user = 'walkers'
graph = facebook.GraphAPI(access_token)
profile = graph.get_object(user)
posts = graph.get_connections(profile['id'], 'posts')
x = 0
while x < 900000:
#while True:
try:
posts = requests.get(posts['paging']['next']).json()
print (posts)
except KeyError:
break
x = x+1
My results are something like this:
{u'paging': {u'next': u' https://graph.facebook.com/v2.0/53198517648/posts?limit=25&__paging_token=enc_AdDr64IO8892JzsoPWiKkMDcF4lTDosOcP0H0ZB1mIpIW5EYRrCylZAji6ZBSCVBAVUYiS80oNtWtAL9GazXxRf0yva&access_token=INSERT ACCESS TOKEN HERE, u'previous': u' https://graph.facebook.com/v2.0/53198517648/posts?limit=25&__paging_token=enc_AdCqTjKBhfOsNBoKe3CJbnM2gU2RvLEYLgAQt1pHEcERVeK4qiw1dQAFHjt2sSInSZAIioZCqotwLMx8azzfZClIuCN&since=1430997206&access_token=INSERT ACCESS TOKEN HERE'}, u'data': [{u'picture': u'https://scontent.xx.fbcdn.net/hphotos-xfp1/v/t1.0-9/p130x130/11200605_10153202855322649_2472649283560371030_n.jpg?oh=50a0b3998e7bae8bb10c3a5f0854af46&oe=56556974', u'story': u'Walkers added 8 new photos to the album: 20 Years of Gary.', u'likes': {u'paging': {u'cursors': {u'after': u'MzgxNzk1MDQ4NjQ3MTI3', u'before': u'MTAxNTI0OTY1MDIwNDIyODI='}}, u'data': [{u'id': u'10152496502042282', u'name': u'Aaron Hanson'}, {u'id': u'10203040513950876', u'name': u'Gary GazRico Hinchliffe'}, {u'id': u'10152934096109345', u'name': u'Stuart Collister'}, {u'id': u'10152297022606059', u'name': u'Helen Preston'}, {u'id': u'326285380900188', u'name': u'Rhys Edwards'}, {u'id': u'10204744346589601', u'name': u'Aaron Benfield'}, {u'id': u'10200910780691953', u'name': u'Mike S Q Wilkins'}, {u'id': u'10204902354187051', u'name': u'Paul Owen Davies'}, {u'id': u'10152784755311784', u'name': u'Dafydd Ifan'}, {u'id': u'1517704468487365', u'name': u'Stephen Collier'}, {u'id': u'10202198826115234', u'name': u'John McKellar'}, {u'id': u'10151949129487143', u'name': u'Lucy Morrison'}, {u'id': u'1474199509524133', u'name': u'Christine Leek'}, {u'id': u'381795048647127', u'name': u'Sandra Taylor'}]}, u'from': {u'category': u'Product/Service', u'name':
These are the two links with the missing information:
u'https://graph.facebook.com/v2.0/53198517648/posts?limit=25&__paging_token=enc_AdDr64IO8892JzsoPWiKkMDcF4lTDosOcP0H0ZB1mIpIW5EYRrCylZAji6ZBSCVBAVUYiS80oNtWtAL9GazXxRf0yva&access_token=INSERT ACCESS TOKEN HERE',
u'previous': u'https://graph.facebook.com/v2.0/53198517648/posts?limit=25&__paging_token=enc_AdCqTjKBhfOsNBoKe3CJbnM2gU2RvLEYLgAQt1pHEcERVeK4qiw1dQAFHjt2sSInSZAIioZCqotwLMx8azzfZClIuCN&since=1430997206&access_token=INSERT ACCESS TOKEN HERE'
Obviously where I've put "INSERT ACCESS TOKEN HERE" I've removed the access token. Is there any way of getting all the data?

How to convert a JSON string into a Python data structure

'[{"append":null,"appendCanExplainable":false,"appendList":[],"auction":{"aucNumId":"35179051643","auctionPic":"http://img.taobaocdn.com/bao/uploaded/i3/TB12WchGXXXXXb5XpXXXXXXXXXX_!!0-item_pic.jpg_40x40.jpg","link":"http://item.taobao.com/item.htm?id=35179051643","sku":"\xd1\xab\xb7\xd6\xc0\xe0:\xc9\xab\xbb\xd2\xcf\xdf\xbd\xf4\xc9\xed\xb3\xa4\xbf\xe3 &nbsp\xb3\xdf\xc2\xeb:M-170M-55-62KG","thumbnail":"","title":"\xcb\xb9\xbd\xf4\xc9\xed\xbf\xe3 \xb5\xaf\xc1\xa6\xd7\xe3\xc7\xf2\xd4\xaf\xbd\xa1\xc9\xed\xbf\xe3 PRO \xc4\xd0 \xb4\xf2\xb5\xd7\xd1\xb5\xc1\xb7\xb3\xa4\xbf\xe3\xcb\xd9\xb8\xc9"},"award":"","bidPriceMoney":{"amount":35,"cent":3500,"centFactor":100,"currency":{"currencyCode":"CNY","defaultFractionDigits":2,"symbol":"\xa3\xa4"},"currencyCode":"CNY","displayUnit":"\xd4\xaa"},"buyAmount":1,"content":"\xba\xc3\xc6\xc0\xa3\xa1","creditFraudRule":0,"date":"2014\xc4\xea12\xd4\xc220\xc8\xd5 15:41","dayAfterConfirm":0,"enableSNS":false,"from":"","lastModifyFrom":0,"payTime":{"date":18,"day":4,"hours":13,"minutes":4,"month":11,"seconds":37,"time":1418879077000,"timezoneOffset":-480,"year":114},"photos":[],"promotionType":"\xbb\xee\xb6\xaf\xb4\xd9\xcf\xfa ","propertiesAvg":"0.0","rate":"1","rateId":231421178840,"raterType":0,"reply":null,"shareInfo":{"lastReplyTime":"","pic":0,"reply":0,"share":false,"userNumIdBase64":""},"showCuIcon":true,"showDepositIcon":false,"spuRatting":[],"status":0,"tag":"","useful":0,"user":{"anony":true,"avatar":"http://a.tbcdn.cn/app/sns/img/default/avatar-40.png","displayRatePic":"b_red_3.gif","nick":"y***6","nickUrl":"","rank":65,"rankUrl":"","userId":"","vip":"","vipLevel":0},"validscore":1,"vicious":""},{"append":null,"appendCanExplainable":false,"appendList":[],"auction":{"aucNumId":"35179051643","auctionPic":"http://img.taobaocdn.com/bao/uploaded/i3/TB12WchGXXXXXb5XpXXXXXXXXXX_!!0-item_pic.jpg_40x40.jpg","link":"http://item.taobao.com/item.htm?id=35179051643","sku":"\xd1\xd5\xc9\xab\xb7\xd6\xc0\xe0:\xba\xda\xc9\xab\xba\xda\xcf\xdf\xbd\xf4\xc9\xed\xb3\xa4\xbf\xe3 &nbsp\xb3\xdf\xc2\xeb:S-160m-45~55KG","thumbnail":"","title":"\xc7\xf2\xc9\xed\xbf\xe3\xb4\xf2\xb5\xd7\xd1\xb5\xc1\xb7\xb3\xa4\xbf\xe3\xcb\xd9\xb8\xc9"},"award":"","bidPriceMoney":{"amount":35,"cent":3500,"centFactor":100,"currency":{"currencyCode":"CNY","defaultFractionDigits":2,"symbol":"\xa3\xa4"},"currencyCode":"CNY","displayUnit":"\xd4\xaa"},"buyAmount":1,"content":"\xba\xc3\xc6\xc0\xa3\xa1","creditFraudRule":0,"date":"2014\xc4\xea12\xd4\xc220\xc8\xd5 15:37","dayAfterConfirm":0,"enableSNS":false,"from":"","lastModifyFrom":0,"payTime":{"date":17,"day":3,"hours":17,"minutes":43,"month":11,"seconds":47,"time":1418809427000,"timezoneOffset":-480,"year":114},"photos":[],"promotionType":"\xbb\xee\xb6\xaf\xb4\xd9\xcf\xfa ","propertiesAvg":"0.0","rate":"1","rateId":231441191365,"raterType":0,"reply":null,"shareInfo":{"lastReplyTime":"","pic":0,"reply":0,"share":false,"userNumIdBase64":""},"showCuIcon":true,"showDepositIcon":false,"spuRatting":[],"status":0,"tag":"","useful":0,"user":{"anony":true,"avatar":"http://a.tbcdn.cn/app/sns/img/default/avatar-40.png","displayRatePic":"b_blue_3.gif","nick":"\xc2\xb7***0","nickUrl":"","rank":1235,"rankUrl":"","userId":"","vip":"","vipLevel":0},"validscore":1,"vicious":""}]'
How can I convert this str to list of dicts ? I have tried some methods, but failed. The string represents a list containing 2 big dicts, and one dict contains nested small dicts. The expected result is:
[{dict1},{dict2}]
You have a JSON string, so use the json module to decode this:
import json
decoded = json.loads(encoded)
decoded is then a Python list; you can then address each dictionary in a list, or use unpacking to assign two dictionaries to two names:
dictionary1, dictionary2 = decoded
If you are using the requests library then you can use the response.json() method to load the content:
decoded = response.json()
In this specific case you appear to have GBK encoded data however (or perhaps GB2312, a predecessor).
This goes well outside the JSON standard (which actually requires one of the UTF codecs to be used), and you'll need to tell json.loads() about the codec used:
decoded = json.loads(encoded, 'gbk')
The requests library will use whatever codec the server sent along with the response, or will otherwise use a characterset detection technique to try and find the right codec to use.
The result, when decoded, then looks like:
>>> decoded = json.loads(encoded, 'gbk')
>>> pprint(decoded)
[{u'append': None,
u'appendCanExplainable': False,
u'appendList': [],
u'auction': {u'aucNumId': u'35179051643',
u'auctionPic': u'http://img.taobaocdn.com/bao/uploaded/i3/TB12WchGXXXXXb5XpXXXXXXXXXX_!!0-item_pic.jpg_40x40.jpg',
u'link': u'http://item.taobao.com/item.htm?id=35179051643',
u'sku': u'\u52cb\u5206\u7c7b:\u8272\u7070\u7ebf\u7d27\u8eab\u957f\u88e4 &nbsp\u5c3a\u7801:M-170M-55-62KG',
u'thumbnail': u'',
u'title': u'\u65af\u7d27\u8eab\u88e4 \u5f39\u529b\u8db3\u7403\u8f95\u5065\u8eab\u88e4 PRO \u7537 \u6253\u5e95\u8bad\u7ec3\u957f\u88e4\u901f\u5e72'},
u'award': u'',
u'bidPriceMoney': {u'amount': 35,
u'cent': 3500,
u'centFactor': 100,
u'currency': {u'currencyCode': u'CNY',
u'defaultFractionDigits': 2,
u'symbol': u'\uffe5'},
u'currencyCode': u'CNY',
u'displayUnit': u'\u5143'},
u'buyAmount': 1,
u'content': u'\u597d\u8bc4\uff01',
u'creditFraudRule': 0,
u'date': u'2014\u5e7412\u670820\u65e5 15:41',
u'dayAfterConfirm': 0,
u'enableSNS': False,
u'from': u'',
u'lastModifyFrom': 0,
u'payTime': {u'date': 18,
u'day': 4,
u'hours': 13,
u'minutes': 4,
u'month': 11,
u'seconds': 37,
u'time': 1418879077000,
u'timezoneOffset': -480,
u'year': 114},
u'photos': [],
u'promotionType': u'\u6d3b\u52a8\u4fc3\u9500 ',
u'propertiesAvg': u'0.0',
u'rate': u'1',
u'rateId': 231421178840,
u'raterType': 0,
u'reply': None,
u'shareInfo': {u'lastReplyTime': u'',
u'pic': 0,
u'reply': 0,
u'share': False,
u'userNumIdBase64': u''},
u'showCuIcon': True,
u'showDepositIcon': False,
u'spuRatting': [],
u'status': 0,
u'tag': u'',
u'useful': 0,
u'user': {u'anony': True,
u'avatar': u'http://a.tbcdn.cn/app/sns/img/default/avatar-40.png',
u'displayRatePic': u'b_red_3.gif',
u'nick': u'y***6',
u'nickUrl': u'',
u'rank': 65,
u'rankUrl': u'',
u'userId': u'',
u'vip': u'',
u'vipLevel': 0},
u'validscore': 1,
u'vicious': u''},
{u'append': None,
u'appendCanExplainable': False,
u'appendList': [],
u'auction': {u'aucNumId': u'35179051643',
u'auctionPic': u'http://img.taobaocdn.com/bao/uploaded/i3/TB12WchGXXXXXb5XpXXXXXXXXXX_!!0-item_pic.jpg_40x40.jpg',
u'link': u'http://item.taobao.com/item.htm?id=35179051643',
u'sku': u'\u989c\u8272\u5206\u7c7b:\u9ed1\u8272\u9ed1\u7ebf\u7d27\u8eab\u957f\u88e4 &nbsp\u5c3a\u7801:S-160m-45~55KG',
u'thumbnail': u'',
u'title': u'\u7403\u8eab\u88e4\u6253\u5e95\u8bad\u7ec3\u957f\u88e4\u901f\u5e72'},
u'award': u'',
u'bidPriceMoney': {u'amount': 35,
u'cent': 3500,
u'centFactor': 100,
u'currency': {u'currencyCode': u'CNY',
u'defaultFractionDigits': 2,
u'symbol': u'\uffe5'},
u'currencyCode': u'CNY',
u'displayUnit': u'\u5143'},
u'buyAmount': 1,
u'content': u'\u597d\u8bc4\uff01',
u'creditFraudRule': 0,
u'date': u'2014\u5e7412\u670820\u65e5 15:37',
u'dayAfterConfirm': 0,
u'enableSNS': False,
u'from': u'',
u'lastModifyFrom': 0,
u'payTime': {u'date': 17,
u'day': 3,
u'hours': 17,
u'minutes': 43,
u'month': 11,
u'seconds': 47,
u'time': 1418809427000,
u'timezoneOffset': -480,
u'year': 114},
u'photos': [],
u'promotionType': u'\u6d3b\u52a8\u4fc3\u9500 ',
u'propertiesAvg': u'0.0',
u'rate': u'1',
u'rateId': 231441191365,
u'raterType': 0,
u'reply': None,
u'shareInfo': {u'lastReplyTime': u'',
u'pic': 0,
u'reply': 0,
u'share': False,
u'userNumIdBase64': u''},
u'showCuIcon': True,
u'showDepositIcon': False,
u'spuRatting': [],
u'status': 0,
u'tag': u'',
u'useful': 0,
u'user': {u'anony': True,
u'avatar': u'http://a.tbcdn.cn/app/sns/img/default/avatar-40.png',
u'displayRatePic': u'b_blue_3.gif',
u'nick': u'\u8def***0',
u'nickUrl': u'',
u'rank': 1235,
u'rankUrl': u'',
u'userId': u'',
u'vip': u'',
u'vipLevel': 0},
u'validscore': 1,
u'vicious': u''}]

JSON array and python

I am having some problems to parse a JSON object that I get when I GET a URL:
[{"id":1,"version":23,"external_id":"2312","url":"https://example.com/432","type":"typeA","date":"2","notes":"notes","title":"title","abstract":"dsadasdas","details":"something","accuracy":0,"reliability":0,"severity":12,"thing":"32132","other":["aaaaaaaaaaaaaaaaaa","bbbbbbbbbbbbbb","cccccccccccccccc","dddddddddddddd","eeeeeeeeee"],"nana":8},{"id":2,"version":23,"external_id":"2312","url":"https://example.com/432","type":"typeA","date":"2","notes":"notes","title":"title","abstract":"dsadasdas","details":"something","accuracy":0,"reliability":0,"severity":12,"thing":"32132","other":["aaaaaaaaaaaaaaaaaa","bbbbbbbbbbbbbb","cccccccccccccccc","dddddddddddddd","eeeeeeeeee"],"nana":8}]
Like you can see the JSON start with "[" and ends "]"
I am using this code:
import json
import urllib2
data = json.load(urllib2.urlopen('http://someurl/path/to/json'))
print data
And I get this:
[{u'severity': 12, u'title': u'title', u'url': u'https://example.com/432', u'external_id': u'2312', u'notes': u'notes', u'abstract': u'dsadasdas', u'other': [u'aaaaaaaaaaaaaaaaaa', u'bbbbbbbbbbbbbb', u'cccccccccccccccc', u'dddddddddddddd', u'eeeeeeeeee'], u'thing': u'32132', u'version': 23, u'nana': 8, u'details': u'something', u'date': u'2', u'reliability': 0, u'type': u'typeA', u'id': 1, u'accuracy': 0}, {u'severity': 12, u'title': u'title', u'url': u'https://example.com/432', u'external_id': u'2312', u'notes': u'notes', u'abstract': u'dsadasdas', u'other': [u'aaaaaaaaaaaaaaaaaa', u'bbbbbbbbbbbbbb', u'cccccccccccccccc', u'dddddddddddddd', u'eeeeeeeeee'], u'thing': u'32132', u'version': 23, u'nana': 8, u'details': u'something', u'date': u'2', u'reliability': 0, u'type': u'typeA', u'id': 2, u'accuracy': 0}]
If the JSON is too large I don't get the full info.
What I am doing wrong?
Thank you
There is nothing wrong with [] in json. It simply means a list. To pretty print your json try this:
import json
import urllib2
data = json.load(urllib2.urlopen('http://someurl/path/to/json'))
print json.dumps(data, sort_keys=True, indent=4, separators=(',', ': '))
To find particular object just do this:
obj = next((obj for obj in data if obj["id"] == 2), None)

Categories

Resources