「PythonとJavaScriptではじめるデータビジュアライゼーション」を読む

5.2requestsを使ったデータファイルの取得

import requests

response = requests.get("http://en.wikipedia.org/wiki/Nobel_Prize")
for resule in response:
    print(resule)

結果
膨大なHTML Docが返されてくる
取得したページ
f:id:bitop:20170911085839p:plain

import requests

response = requests.get("http://en.wikipedia.org/wiki/Nobel_Prize")
print(dir(response))

結果

__attrs__
__bool__
__class__
__delattr__
__dict__
__dir__
__doc__
__eq__
__format__
__ge__
__getattribute__
__getstate__
__gt__
__hash__
__init__
__iter__
__le__
__lt__
__module__
__ne__
__new__
__nonzero__
__reduce__
__reduce_ex__
__repr__
__setattr__
__setstate__
__sizeof__
__str__
__subclasshook__
__weakref__
_content
_content_consumed
apparent_encoding
close
connection
content
cookies
elapsed
encoding
headers
history
is_permanent_redirect
is_redirect
iter_content
iter_lines
json
links
ok
raise_for_status
raw
reason
request
status_code
text
url

-

import requests

response = requests.get("http://en.wikipedia.org/wiki/Nobel_Prize")
print(response.status_code)

結果
200

import requests

response = requests.get("http://en.wikipedia.org/wiki/Nobel_Prize")
print(response.headers)

結果(一部)

f:id:bitop:20170911091053p:plain

import requests

response = requests.get("http://en.wikipedia.org/wiki/Nobel_Prize")
print(response.text.encode('utf-8'))

結果(量が膨大なので省略)

import requests

response = requests.get("https://chhs.data.ca.gov/api/views/pbxw-hhq8/rows.json?accessType=DOWNLOAD")
print(response.status_code)

結果
404
サイト内で"pbxw-hhq8"を検索してみると https://data.chhs.ca.gov/dataset/food-affordability-2006-2010がそのデータらしい。データ形式CSV,PDF,XLSが用意されていたがjsonはなかった。