-
Notifications
You must be signed in to change notification settings - Fork 0
/
datacrypto.py
61 lines (43 loc) · 1.84 KB
/
datacrypto.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
import csv
import random
import time
import pyfiglet
import colored
from datetime import datetime
from bs4 import BeautifulSoup
import requests
def get_crypto(coin):
url = f"https://www.google.com/search?q={coin}+price&sxsrf=AOaemvJxkJI2mosb39pvFv2zVbHFlHp1Rw%3A1642759600754&ei=sIXqYae8LaGH9u8Pu6ONmAI&ved=0ahUKEwin1NCzzML1AhWhg_0HHbtRAyMQ4dUDCA4&uact=5&oq=bitcoin+price&gs_lcp=Cgdnd3Mtd2l6EAMyBAgjECcyBAgAEEMyCggAEIAEEIcCEBQyBQgAEIAEMgUIABCABDIFCAAQgAQyBQgAEIAEMgUIABCABDIFCAAQgAQyBQgAEIAEOgcIIxCwAxAnOgcIABBHELADOgcIABCwAxBDSgQIQRgASgQIRhgAUI0DWNIGYJUJaAFwAngAgAHHA4gBmQmSAQkwLjIuMC4xLjGYAQCgAQHIAQrAAQE&sclient=gws-wiz"
HTML = requests.get(url)
soup = BeautifulSoup(HTML.text, 'html.parser')
text = soup.find('div', attrs={'class':'BNeawe iBp4i AP7Wnd'}).find('div', attrs={'class':'BNeawe iBp4i AP7Wnd'}).text
return text
field_names = ["Date", "price"]
with open("data.csv", 'w') as csv_file:
csv_writer = csv.DictWriter(csv_file, fieldnames=field_names)
csv_writer.writeheader()
while True:
e = datetime.now()
crypt = 'bitcoin'
price = get_crypto(crypt)
if ',' in price:
price = price.replace(',', '')
with open("data.csv", 'a') as csv_file:
csv_writer = csv.DictWriter(csv_file, fieldnames=field_names)
inter = int(e.minute)
if inter < 10:
x_value = f'{e.month}{e.day}{e.hour}0{e.minute}'
else:
x_value = f'{e.month}{e.day}{e.hour}{e.minute}'
info = {
"Date": x_value,
"price": round(int(float(price[0:12])), 7)
}
csv_writer.writerow(info)
print(x_value, round(int(float(price[0:12])), 7))
# x_value += 1
price = get_crypto(crypt)
# print(f'price : {price[0:17]} IRR')
# print(e.second, price[0:14])
time.sleep(60)
# return e.second, price[0:14]