In [23]:
# -*- coding:utf-8 -*-
import requests
from bs4 import BeautifulSoup
url = 'http://nthr.nantou.gov.tw/CustomerSet/032_population/u_population_v.asp'
id = "?id={8C3E3E30-CA6D-4819-87D7-CC6C52B1EA67}"
find_location = [
u"光輝里",
u"光華里",
u"光榮里",
u"光明里",
u"營北里",
u"營南里",
u"內新里",
u"內興里"
]
res = requests.get(url + id +"&yy=102&&mm=08")
# str type
print type(res.content)
# 需傳入content才會正確產生格式
soup = BeautifulSoup(res.content,"html.parser")
# utf-8
print type(soup.original_encoding)
# 印出內容
# print soup.contents
table = soup.find('table',attrs = {'class':'C-tableA0'})
for tr in table.select('tr'):
for location in find_location:
#比對此row是否有包含欲尋找的里名稱
if tr.find(string=location):
# 取出 list td
td_list = tr.find_all('td')
# 顯示名稱與總人口數
print td_list[0].text,td_list[-1].text
# td.get_text() or text will not show unicode encode ,it will show real text
In [ ]: