2016-09-30 92 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