DRYな備忘録

Don't Repeat Yourself.

Pythonのrequestsモジュール使ってみる【HTTP Request】【import requests】【Python2.7.1】

簡単にHTTPリクエストできるPythonのモジュール無いかなー、って思ってたらあった。requestsモジュールってのがあるらしい。

log

% sudo easy_install requests
Password:
Searching for requests
Reading http://pypi.python.org/simple/requests/
Best match: requests 1.2.3
Downloading https://pypi.python.org/packages/source/r/requests/requests-1.2.3.tar.gz#md5=adbd3f18445f7fe5e77f65c502e264fb
Processing requests-1.2.3.tar.gz
Running requests-1.2.3/setup.py -q bdist_egg --dist-dir /tmp/easy_install-6l64Gf/requests-1.2.3/egg-dist-tmp-99ukyB
Adding requests 1.2.3 to easy-install.pth file

Installed /Library/Python/2.7/site-packages/requests-1.2.3-py2.7.egg
Processing dependencies for requests
Finished processing dependencies for requests

入った

% python
Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> r = requests.get('http://gdata.youtube.com/feeds/api/videos?q=keion&alt=json')
>>> print r
<Response [200]>
>>> # まじかよ
...
>>> print r.content
{"version":"1.0","encoding":"UTF-8","feed":{"xmlns$app":"http://purl.org/atom/app#","xmlns":"http://www.w3.org/2005/Atom","xmlns$media":"http://search.yahoo.com/mrss/","xmlns$openS
earch":"http://a9.com/-/spec/opensearchrss/1.0/","xmlns$gd":"http://schemas.google.com/g/2005","xmlns$yt":"http://gdata.youtube.com/schemas/2007","id":{"$t":"http://gdata.youtube.c
om/feeds/api/videos"},"updated":{"$t":"2013-07-10T11:50:02.828Z"},"category":[{"scheme":"http://schemas.google.com/g/2005#kind","term":"http://gdata.youtube.com/schemas/2007#video"
}],"title":{"$t":"Videos matching: keion","type":"text"},"logo":{"$t":"http://www.youtube.com/img/pic_youtubelogo_123x63.gif"},"link":[{"rel":"alternate","type":"text/html","href":
"http://www.youtube.com"},{"rel":"http://schemas.google.com/g/2005#feed","type":"application/atom+xml","href":"http://gdata.youtube.com/feeds/api/videos"},{"rel":"http://schemas.go
ogle.com/g/2005#batch","type":"application/atom+xml","href":"http://gdata.youtube.com/feeds/api/videos/batch"},{"rel":"self","type":"application/atom+xml","href":"http://gdata.yout
ube.com/feeds/api/videos?alt=json&q=keion&start-index=1&max-results=25"},{"rel":"next","type":"application/atom+xml","href":"http://gdata.youtube.com/feeds/api/videos?alt=json&q=ke
ion&start-index=26&max-results=25"}],"author":[{"name":{"$t":"YouTube"},"uri":{"$t":"http://www.youtube.com/"}}],"generator":{"$t":"YouTube data API","version":"2.1","uri":"http://
gdata.youtube.com"},"openSearch$totalResults":{"$t":18431},"openSearch$startIndex":{"$t":1},"openSearch$itemsPerPage":{"$t":25},"entry":[{"id":{"$t":"http://gdata.youtube.com/feeds
/api/videos/-yrlGgQIoNQ"},"published":{"$t":"2009-11-12T13:57:07.000Z"},"updated":{"$t":"2013-07-08T13:16:16.000Z"},"category":[{"scheme":"http://schemas.google.com/g/2005#kind","t
erm":"http://gdata.youtube.com/schemas/2007#video"},{"scheme":"http://gdata.youtube.com/schemas/2007/categories.cat","term":"Film","label":"Film & Animation"}],"title":{"$t":"MMD
】けいおん!  ギー太に首ったけ( 体育館編)","type":"text"},"content":{"$t":"けいおん キャラクターソング 平沢唯(CV:豊崎愛生) ギー太に首ったけ 体育館作っていたら当初の予定とは違ったものができてしまった・・・ MMD keion guitar ni kubittake.","type":"text"},
# 以下略
>>> exit()
%

簡単すぎた

f:id:otiai10:20130710205717p:plain