[問題] 抓取股票個股年成交資訊csv檔資料是空的

看板Python作者 (兩個人一起)時間5年前 (2018/07/05 21:54), 5年前編輯推噓2(202)
留言4則, 2人參與, 5年前最新討論串1/1
主要是發生在 URL 的拼接上 import urllib.request as ur import os import csv import time # with open('../../DATA/over-the-counter_market.csv',newline="") as csvfile: rows = csv.DictReader(csvfile) for row in rows: url = 'http://www.twse.com.tw/exchangeReport/FMNPTK?response=csv&stock No code = row['有價證券代號'] s = url + code f = ur.urlopen(s) with open(code+'.csv', 'wb') as out_file: data = f.read().decode(f.headers.get_content_charset()) out_file.write(bytes(data, 'UTF-8')) out_file.close() print(s) time.sleep(10) 其實我有試過 完整的URL http://www.twse.com.tw/exchangeReport/FMNPTK?response=c sv 但是改成 URL 的拼接 雖然有產生2330.csv 但資料是空的 只有在完整的URL 資料才會寫 入 請問大大們這個狀況是我拼接字串有問題嗎還是??? -- 蕎麥茶香西谷米 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 101.15.181.133 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1530798881.A.D72.html

07/05 22:30, 5年前 , 1F
code=row['有價證券代號'] 這行可能沒正確抓到股票代碼
07/05 22:30, 1F

07/05 22:32, 5年前 , 2F
print(s)的內容?
07/05 22:32, 2F

07/06 10:32, 5年前 , 3F
code = row['有價證券代號']覺得這行有問題 print出來看
07/06 10:32, 3F

07/06 10:32, 5年前 , 4F
看。
07/06 10:32, 4F
謝謝各位大大 我昨天把檔案另存為UTF-8格式就可以了 但是我把有價證卷代碼print出來 是有的 都是數字 我應該是在讀取證券代碼的csv檔案的時候編碼錯誤 ※ 編輯: h0677870105 (101.15.181.133), 07/06/2018 12:06:47
文章代碼(AID): #1RFYCXro (Python)