[問題] 爬蟲問題

看板Python作者 (逢甲阿法)時間7年前發表 (2018/05/21 02:20), 7年前編輯推噓4(4010)
留言14則, 6人參與, 7年前最新討論串3/5 (看更多)
大家早安 我是個爬蟲新手... 我現在想要抓水庫的每日資料 抓到一半 就卡住了... 下面是我的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) 該放甚麼? 麻煩各位大大了... -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.134.166.72 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1526869252.A.DB8.html

05/21 10:41, 7年前 , 1F
一開始在py檔宣告utf8是說明這份檔案是utf8編碼 但如果
05/21 10:41, 1F

05/21 10:41, 7年前 , 2F
你抓爬蟲的資料 你要用str.decode(“utf8”)轉碼
05/21 10:41, 2F
不好意思 我現在抓下來的資料是list 他似乎沒有str這個屬性 還是我要用迴圈把她轉換成str?

05/21 10:42, 7年前 , 3F
至於第二個問題要看你的code喔
05/21 10:42, 3F
※ 編輯: MAGICXX (140.134.166.72), 05/21/2018 10:58:05

05/21 12:01, 7年前 , 4F
第二個問題應該是這網頁用javascript動態改變table的資
05/21 12:01, 4F

05/21 12:02, 7年前 , 5F
料,所以你爬原本的url只會是第一項
05/21 12:02, 5F

05/21 12:04, 7年前 , 6F
可以google看怎麼爬這類的網頁
05/21 12:04, 6F

05/22 00:37, 7年前 , 7F
一定要用READ_HTML嗎?
05/22 00:37, 7F

05/22 00:37, 7年前 , 8F
還是可以用別的(?)
05/22 00:37, 8F

05/22 00:39, 7年前 , 9F
不用read也可以 只是我不知道其他方式而已…
05/22 00:39, 9F

05/26 15:58, 7年前 , 10F
好奇你是用sublimerepl嗎?我試過中文路徑的code進
05/26 15:58, 10F

05/26 15:58, 7年前 , 11F
去會有亂碼,似乎是package造成的,供參考
05/26 15:58, 11F

06/01 00:00, 7年前 , 12F
你如果是windows環境,改utf-8-sig試試
06/01 00:00, 12F

06/01 01:17, 7年前 , 13F
等等,你要不要先試試存檔後用文字編輯器開看看,而不是
06/01 01:17, 13F

06/01 01:17, 7年前 , 14F
直接用print的在標準輸出介面印出來?
06/01 01:17, 14F
文章代碼(AID): #1R0Yq4su (Python)
討論串 (同標題文章)
完整討論串 (本文為第 3 之 5 篇):
問題
1
9
問題
1
8
問題
4
14
問題
0
1
問題
4
8
文章代碼(AID): #1R0Yq4su (Python)