-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.py
More file actions
136 lines (125 loc) · 3.83 KB
/
scraper.py
File metadata and controls
136 lines (125 loc) · 3.83 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import requests
import json
from bs4 import BeautifulSoup
from newsapi import NewsApiClient
import os
import requests
from iexfinance.stocks import Stock
from iexfinance.stocks import get_historical_data
from datetime import datetime
import csv
# obtains the training data and writes it to a file
def obtainTrainingData(code, year, month, day):
date = str(year) + '-' + str(month) + '-' + str(day)
query = getQuery(code)
if not isWeekday(datetime(year, month, day)):
print('weekend')
return
url = makeURL(date, query, 100)
data = getData(url)
if data == None:
return
count = 0
path = '../'+ 'TrainingData' + '/' + query + 'TrainingData' + '_' + date +'/'
for i in data:
tmpDict = dict(i)
fullName = path + query + str(count) + '.txt'
os.makedirs(os.path.dirname(fullName), exist_ok=True)
file = open(fullName, 'w')
try:
page = requests.get(tmpDict['url'])
except:
print('page failed')
if page.status_code == 200:
soup = BeautifulSoup(page.content, "lxml")
all_tags = soup.find_all('p')
for i in all_tags:
try:
file.write(i.get_text())
except Exception as inst:
print('darned emojis')
else:
print("unfortunate failure, page failure")
file.close()
count+=1
fullName = path + query + '.csv'
path = '../' + 'TrainingData' + '/'
appendStock(fullName, path, code, year, month, day, query)
def getData(url):
try:
response = requests.get(url)
data = response.json()
data = data['articles']
return data
except:
print('unfortunate failure')
return
#Adds stock to the csv
def appendStock(fullName, path, code, year, month, day, query):
os.makedirs(os.path.dirname(fullName), exist_ok=True)
with open(fullName, 'w', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=',')
try:
writer.writerow([query] + [getStock(code, year, month, day)])
except:
print('no stock data')
fullName = path + query + '.csv'
os.makedirs(os.path.dirname(fullName), exist_ok=True)
with open(fullName, 'w', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=',')
st = str(year) + '-' + str(month) + '-' + str(day)
try:
writer.writerow([st] + [getStock(code, year, month, day)])
except:
print('no stock data')
#makes the url
def makeURL(query, date, pageSize):
return ('https://newsapi.org/v2/everything?'
'q=' + query + '&'
'from=' + date + '&'
'sortBy=popularity&'
'pageSize=' + str(pageSize) + '&'
'apiKey=13bd628fa8b548738d3b113d9442574e&'
'language=en')
#gets query from the stock code
def getQuery(code):
return get_symbol(code).split(',')[0]
#checks if the day is a weekday, if not don't do anything
def isWeekday(today):
if today.weekday() >= 5:
return False
else:
return True
#never used, don't question, gets symbol from company name
def get_symbol(symbol):
url = "http://d.yimg.com/autoc.finance.yahoo.com/autoc?query={}®ion=1&lang=en".format(symbol)
result = requests.get(url).json()
for x in result['ResultSet']['Result']:
if x['symbol'] == symbol:
return x['name']
#gets stock difference between opening and closing that day
def getStock(code, year, month, day):
start = datetime(year, month, day)
stock = get_historical_data(code, start, start, token='pk_3fc4f2751a6746f3b1cdc30763095572')
if day >= 10:
dict = stock[str(year) + '-' + str(month) + '-' + str(day)]
else:
dict = stock[str(year) + '-' + str(month) + '-' + '0' + str(day)]
return dict['close'] - dict['open']
#Get data for stocks in given range
def stockDayrange(code, year, month, start, end):
for i in range(start, end+1):
obtainTrainingData(code, year, month, i)
#Main Function
if __name__ == '__main__':
print('Type Code')
code = str(input())
print('Type Month')
month = int(input())
print('Type start')
startDay = int(input())
print('Type end')
endDay = int(input())
print('Type year')
year = int(input())
stockDayrange(code, year, month, startDay, endDay)