作者:
MAGICXX (逢甲阿法)
2018-05-21 10:20:49大家早安
我是個爬蟲新手...
我現在想要抓水庫的每日資料
抓到一半 就卡住了...
下面是我的code
# -*- coding: utf-8 -*-
import pandas as pd
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.options import Options
import _uniout
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
driver=webdriver.Firefox()
url='http://fhy.wra.gov.tw/ReservoirPage_2011/StorageCapacity.aspx'
driver.get(url)
sel = Select(driver.find_element_by_id('ctl00_cphMain_cboSearch'))
sel.select_by_index(2)
data=pd.read_html(url)
print data
有兩個問題
1. 我現在執行之後 會印出一串的亂碼...
https://i.imgur.com/gsXnlWz.png
我上網找過解決方式 也在一開始用了
# -*- coding: utf-8 -*-
還是亂碼 避免政府網站用的是big5之類的
也用chardet試過
>>> import urllib
>>>
data=urllib.urlopen('http://fhy.wra.gov.tw/ReservoirPage_2011/StorageCapacity.aspx').read()
>>> import chardet
>>> chardet.detect(data)
{'confidence': 0.99, 'language': '', 'encoding': 'utf-8'}
所以確定是utf-8
結果還是亂碼..那我該處理阿...?
2.我現在想要抓2018年5月20日的「水庫及攔河堰」的資料
可是我現在已經利用selenium下去將下拉式選單改成第三項
可是最後讀取之後還是讀取到第一項
請問我在data=pd.read_html(url) 該放甚麼?
麻煩各位大大了...
一開始在py檔宣告utf8是說明這份檔案是utf8編碼 但如果你抓爬蟲的資料 你要用str.decode(“utf8”)轉碼至於第二個問題要看你的code喔
作者:
eri820503 (snoopyahoo)
2018-05-21 12:01:00第二個問題應該是這網頁用javascript動態改變table的資料,所以你爬原本的url只會是第一項可以google看怎麼爬這類的網頁
一定要用READ_HTML嗎?還是可以用別的(?)
作者:
MAGICXX (逢甲阿法)
2018-05-22 00:39:00不用read也可以 只是我不知道其他方式而已…
好奇你是用sublimerepl嗎?我試過中文路徑的code進去會有亂碼,似乎是package造成的,供參考
你如果是windows環境,改utf-8-sig試試等等,你要不要先試試存檔後用文字編輯器開看看,而不是直接用print的在標準輸出介面印出來?