Github API を使用する際、
キー値 q
に対して、:
や+
などの文字列を設定しリクエストする必要があります。
https://api.github.com/search/repositories?q=language:java+stars:>=200
requests
を使用して get
リクエスト送信をするため、
以下のような記述していましたが、
import requests
payload = {'q': 'language:java+stars:>=200'}
r = requests.get("https://api.github.com/search/repositories", params=payload)
print(r.content)
以下、エラーが発生しました。
b'{"message":"Validation Failed","errors":[{"message":"None of the search qualifiers apply to this search type.","resource":"Search","field":"q","code":"invalid"}],"documentation_url":"https://developer.github.com/v3/search/"}'
r.url
を確認すると、:
や、+
がエンコードされています。
print(r.url)
https://api.github.com/search/repositories?q=language%3Ajava%2Bstars%3A%3E%3D200
別のキー値のクエリストリングも設定するため、エンコードしない方法を調べたところ、
以下の記事を見つけました。
requests
として除外設定があったりするわけではなく、
get
メソッド呼び出し前に文字列変換し、設定すればよいようです。
import requests
payload = {'q': 'language:java+stars:>=200'}
payload_str = "&".join("%s=%s" % (k,v) for k,v in payload.items())
r = requests.get("https://api.github.com/search/repositories", params=payload_str)
print(r.content)
print
の結果
b'{"total_count":3704,"incomplete_results":false,"items":....
戻りが取得できるようになりました。
以上です。
コメント