forked from makma/scriptable-stocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
33 lines (28 loc) · 979 Bytes
/
main.py
File metadata and controls
33 lines (28 loc) · 979 Bytes
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
import requests
from bs4 import BeautifulSoup
import os
import json
import datetime
loosers_url = 'https://finance.yahoo.com/losers/'
loosers_response = requests.get(loosers_url)
loosers_page = BeautifulSoup(loosers_response.text, 'html.parser')
loosers_table = loosers_page.find('tbody')
looser_rows = loosers_table.find_all('tr')
result_json_content = {}
result_json_content['timestamp'] = datetime.datetime.now().strftime('%c')
result_json_content['loosers'] = []
for looser_row in looser_rows:
cells = looser_row.find_all('td')
ticker = cells[0].find('a').string
name = cells[1].text
change = cells[4].find('span').string
result_json_content['loosers'].append({
'ticker': ticker,
'name': name,
'change': change
})
loosers_json_filename = 'docs/result.json'
if os.path.exists(loosers_json_filename):
os.remove(loosers_json_filename)
with open(loosers_json_filename, 'a') as loosers_json_file:
json.dump(result_json_content, loosers_json_file)