In [52]:
# -*- coding:utf-8 -*-
import requests
from bs4 import BeautifulSoup

find_location = [
		u"光輝里",
		u"光華里",
		u"光榮里",
		u"光明里",
		u"營北里",
		u"營南里",
		u"內新里",
		u"內興里"
	]


url = 'http://nthr.nantou.gov.tw/CustomerSet/032_population/u_population_v.asp'
id = "?id={8C3E3E30-CA6D-4819-87D7-CC6C52B1EA67}"

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'):
    td_list = tr.find_all('td')
    for td in td_list:
        print td.text
    # td.get_text() or text will not show unicode encode ,it will show real text


<type 'str'>
<type 'unicode'>
總計
810
34,414
51,169
50,891
102,060
三民里
18
364
495
519
1,014
三和里
25
1,202
1,750
1,780
3,530
三興里
42
2,164
3,310
3,307
6,617
千秋里
11
359
607
585
1,192
仁和里
11
215
292
305
597
內新里
18
551
974
814
1,788
內興里
39
2,626
3,987
4,060
8,047
平山里
40
2,110
3,124
3,258
6,382
平和里
50
2,632
3,453
3,652
7,105
永興里
8
273
514
426
940
永豐里
20
1,094
1,716
1,601
3,317
光明里
33
564
587
697
1,284
光華里
33
468
529
603
1,132
光榮里
33
614
698
704
1,402
光輝里
13
231
334
361
695
東山里
9
172
270
222
492
南投里
12
272
348
361
709
軍功里
25
1,845
2,844
2,890
5,734
振興里
22
788
1,288
1,164
2,452
崇文里
14
434
603
597
1,200
康壽里
18
646
844
890
1,734
新興里
33
1,910
2,982
2,945
5,927
嘉和里
25
1,816
2,621
2,623
5,244
嘉興里
15
787
1,287
1,208
2,495
彰仁里
10
176
230
242
472
漳和里
53
2,732
4,244
4,179
8,423
漳興里
27
1,057
1,544
1,560
3,104
福山里
22
691
1,185
1,077
2,262
福興里
23
1,148
1,718
1,672
3,390
鳳山里
19
431
721
582
1,303
鳳鳴里
9
269
420
366
786
龍泉里
12
418
566
588
1,154
營北里
46
2,482
3,637
3,750
7,387
營南里
22
873
1,447
1,303
2,750

In [ ]: