Last active
January 31, 2020 06:18
-
-
Save drtaka/0518d71067e267eae2fb9fe7185ef846 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"golang.org/x/text/encoding/japanese" | |
"golang.org/x/text/transform" | |
"io" | |
"log" | |
"net/http" | |
"net/url" | |
"os" | |
"strings" | |
) | |
/** | |
[株価データ・株主優待情報・先物データ・ランキングデータ・CSVダウンロード無料 | 株式投資メモ・株価データベース](https://kabuoji3.com/) から | |
銘柄コード、対象年で株価データのcsvファイルのダウンロードを行う。 | |
ダウンロードされる csv ファイルは、ShiftJIS。ヘッダーだけ日本語が入っている。UTF8にエンコードしておく。 | |
*/ | |
func main() { | |
code := "3048" | |
year := "2019" | |
client := &http.Client{} | |
endpoint := "https://kabuoji3.com/stock/file.php" | |
values := url.Values{} | |
values.Add("code", code) | |
values.Add("year", year) | |
req, err := http.NewRequest(http.MethodPost, endpoint, strings.NewReader(values.Encode())) | |
if err != nil { | |
log.Fatal(err) | |
} | |
req.Header.Add("Content-Type", "application/x-www-form-urlencoded") | |
resp, err := client.Do(req) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer resp.Body.Close() | |
log.Printf("[%s-%s]: Resp Status: %v\n", code, year, resp.StatusCode) | |
if resp.StatusCode != http.StatusOK { | |
log.Fatalf("[%s-%s]: bad resposne status, %v\n", code, year, resp.StatusCode) | |
} | |
file, err := os.Create(fmt.Sprintf("%s_%s.csv", code, year)) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer file.Close() | |
if _, err := io.Copy(file, transform.NewReader(resp.Body, japanese.ShiftJIS.NewDecoder())); err != nil { | |
log.Fatal(err) | |
} | |
log.Printf("[%s-%s]: Done\n", code, year) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment