[問題] 請問python爬蟲亂碼問題

看板Python作者 (為善不欲人知)時間10年前 (2014/08/08 17:17), 編輯推噓0(006)
留言6則, 3人參與, 最新討論串1/1
各位板大大德,小弟是最近剛踏入python殿堂的新手,用網路上範例小改了一隻屬於自己 的爬蟲 ,用第一個print印時顯示是正常的,但用split分割每個逗號,並把它存入陣列印出卻出 現以下 碼(只截取一小部分): (5\xe5\xb9\xb4)', '189.01', '0.00', '0.00%', '08/07', '', '\xe5\xa5\xa7\xe5\x9c\xb0\xe5 \x88\xa9CDS(5\xe5\xb9\xb4)', '34.65', '0.15', '0.43%', '08/07', '', '\xe4\xb8\xad\xe5\x 9c\x8bCDS(5\xe5\xb9\xb4)', '138.00', '16.00', '13.11%', '08/07', '', '\xe4\xb8\xb9\xe9\ xba\xa5CDS(5\xe5\xb9\xb4)', '25.475', '0.36', '1.43%', '08/07', '', '\xe5\x8d\x97\xe9\x9f \x93CDS(5\xe5\xb9\xb4)', '59.00', '-0.63', '-1.06%', '08/07', '', '\xe7\x91\x9e\xe5\xa3\x abCDS(5\xe5\xb9\xb4)', '30.01', '0.00', '0.00%', '08/07', '', '\xe7\xbe\x8e\xe5\x9c\x8bCDS( 5\xe5\xb9\xb4)', '16.38', '0.39', '2.44%', '08/07', '' 程式碼如下: #-*- coding:utf-8 -*- from sgmllib import SGMLParser import urllib,re,os,sys class handleFuturePrice(SGMLParser): def reset(self): SGMLParser.reset(self) self.number=0 self.new="" def start_tr(self,attrs): if attrs==[('class', 'row1')] or attrs==[('class', 'row2')]: self.number=1 def handle_data(self, data): if self.number==1: self.new+=data def end_tr(self): self.number=0 def getFuturePrice(url,parser): try: URLprice = urllib.urlopen(url) parser.feed(URLprice.read()) URLprice.close() except: return def readOneLine(new): lines="" returnLines=[] lines+=new.readline() returnLines.append(lines) return returnLines a=[] startURL = "http://www.stockq.org/" parser = handleFuturePrice() getFuturePrice(startURL,parser) new=parser.new.replace('\n',',') print new # 這裡顯示正常 #下面用逗號分割並把它存入a陣列再印出就出現亂碼了 a=new.split(',') print a python版本是使用2.7.8,請各位大大幫小弟解決疑惑...已經改3天了... 謝謝大家幫忙! -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.115.236.15 ※ 文章網址: http://www.ptt.cc/bbs/Python/M.1407489430.A.89A.html

08/08 17:37, , 1F

08/08 17:38, , 2F
其實它沒有亂碼, 你 print a[0] 就會發現正常了
08/08 17:38, 2F

08/08 17:46, , 3F
可是我在GUI介面操作為什麼會出現上述亂碼呢?
08/08 17:46, 3F

08/08 17:53, , 4F
http://d.pr/qUrn 不用謝了
08/08 17:53, 4F

08/11 22:15, , 5F
編碼問題每個新手一開始都會很困擾啊
08/11 22:15, 5F

08/11 22:16, , 6F
環境編碼、檔案編碼、資料來源/儲存編碼、內部表示編碼
08/11 22:16, 6F
文章代碼(AID): #1Jv9MMYQ (Python)