0

I have a code which sends usernames from an excel sheet as an input which is stored in a variable xy to an instagram API which in return gives the results. I load the URL to a JSON to get it in a json schema.

For example when my variable xy contains "shawn_123" the result from the API is:

{
"meta":  {
"code": 200
},
"data":  [
 {
  "username": "shawn_123",
  "profile_picture": "https://scontent.cdninstagram.com/t51.2885-  19/s150x150/11417456_1610194859266611_592197892_a.jpg",
  "id": "641567093",
  "full_name": "shawn ritz"
},
 {
  "username": "shawn_12345",
  "profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/s150x150/11324946_808347519273018_2073555780_a.jpg",
  "id": "2074312361",
  "full_name": "shawney"
}
]
}

And my code is

for r in range(1,10):
var=r,sheet.cell(row=r,column=1).value
xy=var[1]
myopener=Myopener()
url=myopener.open('https://api.instagram.com/v1/users/search?q='+xy+'&count=1&access_token=641567093.1fb234f.a0ffbe574e844e1c818145097050cf33')
beta=json.load(url)
for item in beta['data']:
    print(item['id'])

As it is retrieving two outputs from a same username.

Note: I want a regular expression which searches the exact username from the json and save the user_id of only that record.

Tim007
  • 2,557
  • 1
  • 11
  • 20
rohit nair
  • 240
  • 4
  • 16

3 Answers3

1

You can check if the username matches before printing the id:

for item in beta['data']:
    if item['username'] == xy: # here check the username from your input
        print(item['id'])

Or, using next operator:

user_id = next((item['id'] for item in beta['data'] if item['username'] == xy), None)
AKS
  • 18,983
  • 3
  • 43
  • 54
  • So, you are saying I dont need any regular expression here? – rohit nair Apr 27 '16 at 06:26
  • You don't need to! – AKS Apr 27 '16 at 06:27
  • If you don't mind, can you give a brief explanation about the 2nd option using next operator which you have given. It would be really helpful. I meant how would it work in my loop? Now instead of printing the user_id, I am appending it to a list. >> if(conditions are true) then list.append(item['id']) – rohit nair Apr 27 '16 at 07:04
  • There already is a very good explanation how `next` works: ... _[best way to get the first item from an iterable matching a condition?](http://stackoverflow.com/questions/2361426/what-is-the-best-way-to-get-the-first-item-from-an-iterable-matching-a-condition)_ – AKS Apr 27 '16 at 07:08
  • 1
    Thank you for your helpful response! – rohit nair Apr 27 '16 at 07:13
  • Glad to be of help. Please accept the answer if it solves your query. – AKS Apr 27 '16 at 07:20
0
d = {"data":  [ {  "username": "shawn_123",  "profile_picture": "https://scontent.cdninstagram.com/t51.2885-  19/s150x150/11417456_1610194859266611_592197892_a.jpg",  "id": "641567093",  "full_name": "shawn ritz"}, {  "username": "shawn_12345",  "profile_picture": "https://scontent.cdninstagram.com/t51.2885-19/s150x150/11324946_808347519273018_2073555780_a.jpg",  "id": "2074312361",  "full_name": "shawney" }]}

#### To get list
user_name = 'something'
print [i['username'] for i in d['data'] if i['username'] == user_name]

#### To get the username

print [i['username'] for i in d['data'] if i['username'] == user_name][0]
SuperNova
  • 25,512
  • 7
  • 93
  • 64
  • Thank you! I am actually trying to use the variables rather than the string itself, Because I have more than a 1000 usernames which i will be looping through. – rohit nair Apr 27 '16 at 07:16
0

Try this

"shawn_123".*?"id":\s"(\d+)"

Regex demo

Explanation:
.: Any character except line break sample
*: Zero or more times sample
?: Once or none sample
\s: "whitespace character": space, tab, newline, carriage return, vertical tab sample
( … ): Capturing group sample
\: Escapes a special character sample
+: One or more sample

Python:

import re
p = re.compile(ur'"shawn_123".*?"id":\s"(\d+)"', re.DOTALL)
test_str = u"{\n\"meta\":  {\n\"code\": 200\n},\n\"data\":  [\n {\n  \"username\": \"shawn_123\",\n  \"profile_picture\": \"https://scontent.cdninstagram.com/t51.2885-  19/s150x150/11417456_1610194859266611_592197892_a.jpg\",\n  \"id\": \"641567093\",\n  \"full_name\": \"shawn ritz\"\n},\n {\n  \"username\": \"shawn_12345\",\n  \"profile_picture\": \"https://scontent.cdninstagram.com/t51.2885-19/s150x150/11324946_808347519273018_2073555780_a.jpg\",\n  \"id\": \"2074312361\",\n  \"full_name\": \"shawney\"\n}"
m = re.findall(p, test_str)
print m
#[u'641567093']
Tim007
  • 2,557
  • 1
  • 11
  • 20
  • Thank you for the regex demo! I am new to it and needed just this to get to know about regular expressions. – rohit nair Apr 27 '16 at 07:17