2016-09-30 5 views
0

このAPIから何かを印刷しようとすると、次のような問題が発生します。私はそれを設定しようとしているので、別のヘッダーにアクセスして、そこから特定の項目を印刷することができます。しかし、代わりに私がスープを印刷しようとすると、json形式でAPI応答全体が表示されます。Glassdoor APIが印刷されないカスタムレスポンス

import requests, json, urlparse, urllib2 
from BeautifulSoup import BeautifulSoup 

url = "apiofsomesort"     

#Create Dict based on JSON response; request the URL and parse the JSON 
#response = requests.get(url) 
#response.raise_for_status() # raise exception if invalid response 
hdr = {'User-Agent': 'Mozilla/5.0'} 
req = urllib2.Request(url,headers=hdr) 
response = urllib2.urlopen(req) 
soup = BeautifulSoup(response) 

print soup 

それは以下のようになります。印刷した場合:

{ 
    "success": true, 
    "status": "OK", 
    "jsessionid": "0541E6136E5A2D5B2A1DF1F0BFF66D03", 
    "response": { 
    "attributionURL": "http://www.glassdoor.com/Reviews/airbnb-reviews-SRCH_KE0,6.htm", 
    "currentPageNumber": 1, 
    "totalNumberOfPages": 1, 
    "totalRecordCount": 1, 
    "employers": [{ 
     "id": 391850, 
     "name": "Airbnb", 
     "website": "www.airbnb.com", 
     "isEEP": true, 
     "exactMatch": true, 
     "industry": "Hotels, Motels, & Resorts", 
     "numberOfRatings": 416, 
     "squareLogo": "https://media.glassdoor.com/sqll/391850/airbnb-squarelogo-1459271200583.png", 
     "overallRating": 4.3, 
     "ratingDescription": "Very Satisfied", 
     "cultureAndValuesRating": "4.4", 
     "seniorLeadershipRating": "4.0", 
     "compensationAndBenefitsRating": "4.3", 
     "careerOpportunitiesRating": "4.1", 
     "workLifeBalanceRating": "3.9", 
     "recommendToFriendRating": "0.9", 
     "sectorId": 10025, 
     "sectorName": "Travel & Tourism", 
     "industryId": 200140, 
     "industryName": "Hotels, Motels, & Resorts", 
     "featuredReview": { 
     "attributionURL": "http://www.glassdoor.com/Reviews/Employee-Review-Airbnb-RVW12111314.htm", 
     "id": 12111314, 
     "currentJob": false, 
     "reviewDateTime": "2016-09-28 16:44:00.083", 
     "jobTitle": "Employee", 
     "location": "", 
     "headline": "An amazing place to work!", 
     "pros": "Wonderful people and great culture. Airbnb really strives to make you feel at home as an employee, and everyone is genuinely excited about the company mission.", 
     "cons": "The limitations of Rails 3 and the company infrastructure make developing difficult sometimes.", 
     "overall": 5, 
     "overallNumeric": 5 
     }, 
     "ceo": { 
     "name": "Brian Chesky", 
     "title": "CEO & Co-Founder", 
     "numberOfRatings": 306, 
     "pctApprove": 95, 
     "pctDisapprove": 5, 
     "image": { 
      "src": "https://media.glassdoor.com/people/sqll/391850/airbnb-brian-chesky.png", 
      "height": 200, 
      "width": 200 
     } 
     } 
    }] 
    } 
} 

私は「雇用者のような特定の項目をプリントアウトしたい:名前、産業等...

答えて

0

あなたはJSONを読み込むことができますdictに応答して、他のdictと同じようにしたい値を探します。

データを取得して外部のJSONファイルに保存しました。 API。これは私のために働いた。

import json 

# Load JSON from external file 
with open (r'C:\Temp\json\data.json') as json_file: 
    data = json.load(json_file) 

# Print the values 
print 'Name:', data['response']['employers'][0]['name'] 
print 'Industry:', data['response']['employers'][0]['industry'] 

APIからデータを取得しているので、このようなものが動作するはずです。

import json 
import urlib2 

url = "apiofsomesort"     

# Load JSON from API 
hdr = {'User-Agent': 'Mozilla/5.0'} 
req = urllib2.Request(url, headers=hdr) 
response = urllib2.urlopen(req) 
data = json.load(response.read()) 

# Print the values 
print 'Name:', data['response']['employers'][0]['name'] 
print 'Industry:', data['response']['employers'][0]['industry'] 
+0

問題がjson.loadのためにあなたがread関数定義されjson.loads(response.read())を持つオブジェクトなどのファイルを渡すべきであるということです –

0
import json, urlib2 

url = "http..." 

hdr = {'User-Agent': 'Mozilla/5.0'} 
req = urllib2.Request(url, headers=hdr) 
response = urllib2.urlopen(req) 
data = json.loads(response.read()) 

# Print the values 
print 'numberOfRatings:', data['response']['employers'][0]['numberOfRatings'] 
+0

興味があるだけ、なぜならばAを投稿する気に許容できない? – pnuts

関連する問題