Glassdoor API无法打印自定义响应

时间:2016-09-30 18:04:38

标签: python json api

当我尝试从此API打印时,我遇到以下问题。我正在尝试设置它,以便我可以访问不同的标题,然后从中打印特定的项目。但是当我尝试打印汤时,它以json格式给出了整个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
        }
      }
    }]
  }
}

我想打印出像雇主这样的特定项目“:名称,行业等......

2 个答案:

答案 0 :(得分:0)

您可以将JSON响应加载到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']

答案 1 :(得分: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']