[問題] 抓取股票個股年成交資訊csv檔資料是空的
主要是發生在
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
07/05 22:30, 1F
→
07/05 22:32,
5年前
, 2F
07/05 22:32, 2F
推
07/06 10:32,
5年前
, 3F
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