0

I am using python and I want to get token value and store it in a variable. Can someone please tell me how to get token value as I am a beginner in python.

Below is the string from which I want to extract the value:

`jQuery18209123624774221071_1620226978533({"status":"success","message":"Login successfully","data":`{"user_id":"UJiH0","user_fname":"MyName","user_mname":"","user_lname":"MyLastName","user_gender":"M","user_email":"mymailid@gmail.com","user_email_mig":"","user_lastdt":"2021-05-06 10:50:59","entdate":"2020-12-03 08:16:59","user_mobile":"919999999999","user_defview":"4","user_nick":"ashmit","email_verify":"1","mobile_verify":"1","user_add1":"","user_add2":"","user_state":"MyState","user_country":"MyCountry","user_pincode":"MyPincode","user_hint":"","user_fax":"","user_dob":"MyDOB","user_city":"MyCity","user_occupation":"Student","income_status":"MyIncome","industry":"Information Technology","marital_status":"","user_defsort":"","user_defcolor":"","user_stkworth":"","user_mfworth":"","user_ulipworth":"","user_loansworth":"","user_debtworth":"","user_metalsworth":"","user_asstworth":"","user_borrworth":"","user_propworth":"","user_list":"","portfolio_mailer":"","news_mailer":"","mf_mailer":"","user_refrate":"","user_seenflag":"","user_rel_optin":"","user_portviewperm":"","user_oldid":"","port_unsub_flag":"","user_mobile_updated":"91-9999999999","user_isd_code":"91","usertype":"email","user_country_code":"MyCountryCode","user_state_code":"MyStateCode","user_city_code":"MyCityCode","member_type":"New","userimg":"http:\/\/img-d02.moneycontrol.co.in\/images\/messageboard\/home\/img.jpg","dispname":"ashmit"},"token":"PemRcaHEDX0bPl9OF1tUD5Xv9J7V8DrR0k3tNjoLLc5hs3xSg9vI5cMj75qbsF6g3-03NJnNEw"})
James Z
  • 12,209
  • 10
  • 24
  • 44
  • Remove everything up to the first `(`, remove the last `)`, then use `json.loads()` to parse the rest. – Barmar May 06 '21 at 16:11
  • Where are you getting that from? That's a JSONP response, usually the API will also have a JSON option. – Barmar May 06 '21 at 16:12
  • I am getting it while scrapping some data – Ashmit Sharma May 06 '21 at 16:27
  • @Barmar can you also help in this Question https://stackoverflow.com/questions/67405043/i-want-to-scrape-data-from-a-page-which-requires-login-and-then-the-main-data-is – Ashmit Sharma May 06 '21 at 16:28
  • I removed the thing before '(' and also removed last ')' using logreq = logreq.replace('jQuery18209123624774221071_1620226978533(','') logreq = logreq.replace(')','') . Now what ? @Barmar – Ashmit Sharma May 06 '21 at 16:47
  • Then use `d = json.loads(logreq)` to parse it. – Barmar May 06 '21 at 16:48
  • BTW, `logreq.replace(')', '')` won't replace just the last `)`, it will replace *all* `)`. – Barmar May 06 '21 at 16:49
  • I can't see any other ')' in whole string so I used it. – Ashmit Sharma May 06 '21 at 16:53
  • There isn't one this time, but maybe there will be the next time you run the program. – Barmar May 06 '21 at 16:55
  • I used d = json.loads(logreq) but its printing the same string again I want only token value. I am sorry I am beginner I don't know much. @Barmar – Ashmit Sharma May 06 '21 at 16:57
  • Now it's a dictionary, you can access whichever elements you want using normal Python operations. – Barmar May 06 '21 at 16:57
  • See https://stackoverflow.com/questions/48193502/access-a-particular-field-in-arbitrarily-nested-json-data – Barmar May 06 '21 at 16:58
  • Actually, it turns out not to be valid JSON because of the backticks in the `data:` property. That's JavaScript, not JSON, you need to run a JS interpreter from Python. Not simple if you're a beginner. – Barmar May 06 '21 at 17:01
  • You could just use a regular expression to match the string after `"token":`. If you don't know how to write regular expressions, read the tutorial at regular-expression.info. – Barmar May 06 '21 at 17:02
  • I used this "d = json.loads(logreq['token'])" .. It is giving error "d = json.loads(logreq['token']) TypeError: string indices must be integers" @Barmar – Ashmit Sharma May 06 '21 at 17:03
  • `d = json.loads(logreg)` then `token = d['token']` – Barmar May 06 '21 at 17:04
  • But it won't work because it's not actually JSON, like I said above. – Barmar May 06 '21 at 17:05
  • Thank You!!! It worked @Barmar – Ashmit Sharma May 06 '21 at 17:06

0 Answers0