2011-12-14 19 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')と追加しました。 元のテストからCURLに渡された結果が返されました。 – cnowacek

関連する問題