-
Notifications
You must be signed in to change notification settings - Fork 0
/
getwebx9x1.py
63 lines (52 loc) · 1.76 KB
/
getwebx9x1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/python
#-*- coding: UTF-8 -*-
import sys
import os
import time
from selenium import webdriver
from bs4 import BeautifulSoup
#import requests
from bs4 import BeautifulSoup
webdriver = webdriver.Chrome("./chromedriver")
def chk_datepath():
nowget =time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
yy = nowget[0:4]
mm = nowget[5:7]
mydate = "day"+yy+"M"+mm
os.chdir("\py3_prj\pyweb\stock_data")# dir path select
ll=os.listdir()
if mydate not in ll:
os.mkdir(mydate)
return mydate
def load_stockweb(stock_sn,datepath):
nowget =time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
#stock_sn="3231"
#weblink = "http://traderoom.cnyes.com/tse/quote2FB.aspx?code="+stock_sn
weblink = "https://stock.pchome.com.tw/stock/sto0/ock3/sid"+stock_sn+".html"
#r = requests.get(weblink,timeout=10)
#r.encoding = "utf-8"
#soup = BeautifulSoup(r.text, "lxml")
webdriver.implicitly_wait(10)
webdriver.get(weblink)
print(webdriver.title)
soup = BeautifulSoup(webdriver.page_source, "lxml")
#fp = open("index.html", "w", encoding="utf8")
#fp.write(soup.prettify())
#print("寫入檔案index.html...")
#fp.close()
#webdriver.quit()
os.chdir("\\py3_prj\\pyweb\\stock_data\\"+datepath)
sfilename = "stock"+stock_sn+"d"+nowget[0:10]+".html"
fp = open(sfilename , "w", encoding="utf8")
fp.write(soup.prettify())
#print(nowget[0:10])
print("寫入檔案"+sfilename)
fp.close()
if __name__ == '__main__':
if len(sys.argv)>1:
sspath = chk_datepath()
print(sspath)
#print(type(sspath))
#load_stockweb("3231",sspath)
sn=sys.argv[1]
load_stockweb(sn,sspath)