2016-09-30 63 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反應到一個字典,然後找你要喜歡你會在其他任何字典中的值。

我把你的數據,並將其保存在外部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你應該通過一個文件像讀取功能定義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