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

5.4ライブラリを使ったWEB APIへのアクセス

Tweetのアクセストークン他取得できなかったので省略

5.6 スープの取得

def get_Nobel_soup():
    response = requests.get(BASE_URL + '/wiki/List_of_Nobel_laureates',headers=HEADERS)
    return BeautifulSoup(response.content,"lxml")

BASE_URL = "http://en.wikipedia.org"
HEADERS = {'User-Agent':'Mozilla/5.0'}
get_Nobel_soup()

このコードが取得するのが下表である
f:id:bitop:20170915125245p:plain

取得したHTMLの一部
ここからテーブルの定義が始まる

b'<table class="wikitable sortable">\n  
テーブルヘッダの定義  
行タグ  
<tr>\n  
1列目のヘッダ 西暦  
<th>Year</th>\n  
2列目のヘッダ物理学賞の列、リンク先のアドレスも指定してある  
<th width="18%"><a href="/wiki/List_of_Nobel_laureates_in_Physics" title="List of Nobel laureates in Physics">Physics</a></th>\n  
3列目のヘッダ化学学賞の列、リンク先のアドレスも指定してある  
<th width="16%"><a href="/wiki/List_of_Nobel_laureates_in_Chemistry" title="List of Nobel laureates in Chemistry">Chemistry</a> 
</th>\n
4列目のヘッダ生理学・医学賞の列、リンク先のアドレスも指定してある  
<th width="18%"><a href="/wiki/List_of_Nobel_laureates_in_Physiology_or_Medicine" title="List of Nobel laureates in Physiology or Medicine">Physiology<br/>\nor Medicine</a></th>\n  
5列目のヘッダ文学賞の列、リンク先のアドレスも指定してある  
<th width="16%"><a href="/wiki/List_of_Nobel_laureates_in_Literature" title="List of Nobel laureates in Literature">Literature</a></th>\n  
6列目のヘッダ平和賞の列、リンク先のアドレスも指定してある  
<th width="16%"><a href="/wiki/List_of_Nobel_Peace_Prize_laureates" title="List of Nobel Peace Prize laureates">Peace</a></th>\n
7列目のヘッダ経済学賞の列、リンク先のアドレスも指定してある  
<th width="15%"><a class="mw-redirect" href="/wiki/List_of_Nobel_laureates_in_Economics" title="List of Nobel laureates in Economics">Economics</a></th>\n  
行タグ終了  
</tr>\n  
ここから表の中身  
行タグ  
<tr>\n  
表データ 1列目は西暦  
<td align="center">1901</td>\n  
物理学の受賞者 レントゲンのデータ  
<td>  

<span class="sortkey">  
R\xc3\xb6ntgen, Wilhelm  
</span>  

受賞者の名前とその人物ページへのアドレス  
<span class="vcard">  
<span class="fn">  
    <a href="/wiki/Wilhelm_R%C3%B6ntgen" title="Wilhelm R\xc3\xb6ntgen">Wilhelm R\xc3\xb6ntgen</a>  
</span>  
</span>  
データの終了、以降2列目から7列目までの受賞者が列記されている  
</td>\n  

最後にテーブルタグを閉じてあった。  
</table>'