-
Notifications
You must be signed in to change notification settings - Fork 0
/
stock_line.py
71 lines (47 loc) · 1.56 KB
/
stock_line.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
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon May 11 21:56:35 2020
@author: Johnny Tsai
"""
import requests
import pandas as pd
from bs4 import BeautifulSoup
import time
url = 'https://tw.stock.yahoo.com/q/q'
headers = {
'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4122.7 Mobile Safari/537.36'}
stock_id = input('請輸入股號:')
df_index = ['股票代號', '時間', '成交', '買進', '賣出', '漲跌',
'張數', '昨收', '開盤', '最高', '最低']
payload = {
's': stock_id
}
response = requests.get(url, params=payload, headers=headers)
response.raise_for_status()
soup = BeautifulSoup(response.text, 'lxml')
tables = soup.find_all('table')
table = tables[2]
tds = table.find_all('td')
stock_data = []
for td in tds[:-1]:
data = td.text.strip().replace('加到投資組合', '')
stock_data.append(data)
df = pd.DataFrame(stock_data, index=df_index)
print(df)
print('-'*20)
time.sleep(3)
stock = df.loc['股票代號',:]
price = df.loc['成交',:]
change = df.loc['漲跌',:]
#---------------------------------------------------------------
def lineNotifyMessage(token, msg):
headers = {
"Authorization": "Bearer " + token,
}
payload = {'message': msg}
r = requests.post("https://notify-api.line.me/api/notify", headers = headers, params = payload)
return r.status_code
message = '\n' + stock + '即時價格:' + price + ' ; ' + change
token = '*****'
lineNotifyMessage(token, message)