2011-12-14 4 views
0
을 제공 urlopen

그래서 나는 웹 사이트에서 일부 JSON 데이터를 검색하고있어 urllib2가에서 표준 urlopen()를 사용하여 :CURL과 충돌하는 결과

코드 :

url = 'http://api.nytimes.com/svc/politics/v3/us/legislative/congress/senate/votes/2011-12-14/2011-12-14.json?api-key=%s' % (api_key,) 
print urlopen(url).read() 

결과 :

{ 
    "status":"OK", 
    "copyright":"Copyright (c) 2011 The New York Times Company. All Rights Reserved.", 
    "results": { 
     "chamber": "Senate", 
     "start_date": "2011-12-14", 
     "end_date": "2011-12-14", 
     "num_results": "0", 
     "votes": [ 
       ] 
    } 
} 

이 결과는 잘못되었습니다. 내 브라우저에 직접 URL을 입력하는 경우 :

입력하기 :

http://api.nytimes.com/svc/politics/v3/us/legislative/congress/senate/votes/2011-12-14/2011-12-14.json?api-key=a55a8988c8102a532221d2d465ca010a:1:65328279 

내가 얻을 : 내가 CURL을 사용하여 데이터를 검색 할 때

{ 
    "status":"OK", 
    "copyright":"Copyright (c) 2011 The New York Times Company. All Rights Reserved.", 
    "results": { 
     "chamber": "Senate", 
     "start_date": "2011-12-14", 
     "end_date": "2011-12-14", 
     "num_results": "1", 
    "votes": [ 
      { 
     "congress": "112", 
     "session": "1", 
     "roll_call": "228", 
"vote_uri":"http://api.nytimes.com/svc/politics/v3/us/legislative/congress/112/senate/sessions/1/votes/228.json", 
     "bill_number": "S.J.Res.24", 
     "question": "On the Joint Resolution", 
     "description": "Joint resolution proposing a balanced budget amendment to the Constitution of the United States.", 
     "vote_type": "1/2", 
     "date": "2011-12-14", 
     "time": "11:09:00", 
     "result": "Joint Resolution Defeated", 
     "democratic": { 
     "yes": "20", 
     "no": "31", 
     "present": "0", 
     "not_voting": "0", 
     "majority_position": "No" 
     }, 
     "republican": { 
     "yes": "1", 
     "no": "46", 
     "present": "0", 
     "not_voting": "0", 
     "majority_position": "No" 
     }, 
     "independent": { 
     "yes": "0", 
     "no": "2", 
     "present": "0", 
     "not_voting": "0" 
     }, 
     "total": { 
     "yes": "21", 
     "no": "79", 
     "present": "0", 
     "not_voting": "0" 
     } 
     }     ] 
} 

}

또한

, 내가 얻을 :

$curl http://api.nytimes.com/svc/politics/v3/us/legislative/congress/senate/votes/2011-12-14/2011-12-14.json?api-key=a55a8988c8102a532221d2d465ca010a:1:65328279 
{ 
    "status":"OK", 
    "copyright":"Copyright (c) 2011 The New York Times Company. All Rights Reserved.", 
    "results": { 
     "chamber": "Senate", 
     "start_date": "2011-12-14", 
     "end_date": "2011-12-14", 
     "num_results": "2", 
     "votes": [ 
      { 
     "congress": "112", 
     "session": "1", 
     "roll_call": "229", 
     "vote_uri": "http://api.nytimes.com/svc/politics/v3/us/legislative/congress/112/senate/sessions/1/votes/229.json", 
     "bill_number": "S.J.Res.10", 
     "question": "On the Joint Resolution", 
     "description": "Joint resolution proposing a balanced budget amendment to the Constitution of the United States.", 
     "vote_type": "2/3", 
     "date": "2011-12-14", 
     "time": "11:39:00", 
     "result": "Joint Resolution Defeated", 
     "democratic": { 
     "yes": "0", 
     "no": "51", 
     "present": "0", 
     "not_voting": "0", 
     "majority_position": "No" 
     }, 
     "republican": { 
     "yes": "47", 
     "no": "0", 
     "present": "0", 
     "not_voting": "0", 
     "majority_position": "Yes" 
     }, 
     "independent": { 
     "yes": "0", 
     "no": "2", 
     "present": "0", 
     "not_voting": "0" 
     }, 
     "total": { 
     "yes": "47", 
     "no": "53", 
     "present": "0", 
     "not_voting": "0" 
     } 
     },    { 
     "congress": "112", 
     "session": "1", 
     "roll_call": "228", 
     "vote_uri": "http://api.nytimes.com/svc/politics/v3/us/legislative/congress/112/senate/sessions/1/votes/228.json", 
     "bill_number": "S.J.Res.24", 
     "question": "On the Joint Resolution", 
     "description": "Joint resolution proposing a balanced budget amendment to the Constitution of the United States.", 
     "vote_type": "1/2", 
     "date": "2011-12-14", 
     "time": "11:09:00", 
     "result": "Joint Resolution Defeated", 
     "democratic": { 
     "yes": "20", 
     "no": "31", 
     "present": "0", 
     "not_voting": "0", 
     "majority_position": "No" 
     }, 
     "republican": { 
     "yes": "1", 
     "no": "46", 
     "present": "0", 
     "not_voting": "0", 
     "majority_position": "No" 
     }, 
     "independent": { 
     "yes": "0", 
     "no": "2", 
     "present": "0", 
     "not_voting": "0" 
     }, 
     "total": { 
     "yes": "21", 
     "no": "79", 
     "present": "0", 
     "not_voting": "0" 
     } 
     }     ] 
} 

}

동일한 웹 요청에 대해 3 가지 결과가 있습니다. 여기에 무슨 문제가 있니? 내가 간과하고있는 캐싱 메카니즘이 있습니까?

+0

참조하십시오. – soulcheck

+0

피들러를 사용하여 요청에 대해 다른 점을 확인하십시오. ... –

+0

urlopen URL에 "2011-12-14s.json"이 있고 다른 2 개에 "2011-12-14 .json "? – chauncey

답변

1

요청에 'Accept-Encoding'헤더가 설정되어 있어야합니다.

그냥 경우에, 당신은 다음과 같이 설정할 수 있습니다 urlopen의 사용자 에이전트를 변경 도움이된다면

request = urllib2.Request(your_url) 
request.add_header('Accept-Encoding',whatever_in_this_case) 
opener = urllib2.build_opener() 
print opener.open(request).read() 
+0

이 대답이 효과적입니다. 위의 코드 조각을'request.add_header ('Accept-Encoding', 'gzip, identity')와 함께 추가했습니다. 원래 테스트 (예 : 2 개의 결과가 반환 됨)에서 CURL로 전달 된 결과가 나타납니다. – cnowacek