Skip to content

Instantly share code, notes, and snippets.

@anton-petrov
Created March 30, 2020 05:47
Show Gist options
  • Save anton-petrov/78553014824dc637ce55659c3fe725fb to your computer and use it in GitHub Desktop.
Save anton-petrov/78553014824dc637ce55659c3fe725fb to your computer and use it in GitHub Desktop.
import requests
from bs4 import BeautifulSoup
import time
from time import gmtime, strftime
import emoji
import smtplib
from email.mime.text import MIMEText
from email.header import Header
print('Exchange Rates v1.0')
print(emoji.emojize('Сделано с :blue_heart:\n\n'))
class CurrencyRate:
__login: str = '[email protected]'
__password = ''
DOLLAR_RUB = 'https://www.google.com/search?sxsrf=ALeKk00z4iQbIPw7txjcpJasYv7dK_NZCA%3A1585477287016&ei=p3aAXtFL3YCTvg__57L4DQ&q=%D0%BA%D1%83%D1%80%D1%81+%D0%B4%D0%BE%D0%BB%D0%BB%D0%B0%D1%80%D0%B0&oq=%D0%BA%D1%83%D1%80%D1%81+%D0%B4%D0%BE%D0%BB%D0%BB%D0%B0%D1%80%D0%B0&gs_lcp=CgZwc3ktYWIQDDIECAAQRzIECAAQRzIECAAQRzIECAAQRzIECAAQRzIECAAQRzIECAAQRzIECAAQR1AAWABgrzdoAHAEeACAAQCIAQCSAQCYAQCqAQdnd3Mtd2l6&sclient=psy-ab&ved=0ahUKEwiRu_eTu7_oAhVdwMQBHf-zDN8Q4dUDCAs'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'}
current_currency_rate: float = 0
currency_deviation: float = 1.0
mail_to: str = ''
def __init__(self, mail_to: str = ''):
self.current_currency_rate = self.get_currency_rate()
self.mail_to = mail_to
def __now(self):
return strftime("%Y-%m-%d %H:%M:%S", gmtime())
def get_currency_rate(self):
full_page = requests.get(self.DOLLAR_RUB, headers=self.headers)
soup = BeautifulSoup(full_page.content, 'html.parser')
convert = soup.findAll("span", {"class": "DFlfde", "class": "SwHCTb", "data-precision": "2"})
return float(convert[0].text.replace(',', '.'))
def check_currency(self):
currency = self.get_currency_rate()
if currency >= self.current_currency_rate + self.currency_deviation:
print(emoji.emojize("Курс доллара сильно вырос :thums_up:"))
elif currency <= self.current_currency_rate - self.currency_deviation:
print(emoji.emojize("Курс доллара сильно упал :-1:"))
currency_rate_message = f"{self.__now()} курс: 1 доллар = {currency} руб."
print(currency_rate_message, f'\n\nОтправляем на почту {self.mail_to}')
self.__send_mail(currency_rate_message)
# time.sleep(3)
# self.check_currency()
def __send_mail(self, text: str):
server = smtplib.SMTP('smtp.gmail.com', 587, timeout=10)
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.ehlo()
server.login(self.__login, self.__password)
message = MIMEText(text, 'plain', 'utf-8')
message['Subject'] = Header(f'Курс валют $ --> {self.__now()}', 'utf-8')
message['From'] = self.__login
message['To'] = self.mail_to
print(self.mail_to)
server.sendmail(message['From'], message['To'], message.as_string())
server.quit()
currency = CurrencyRate('[email protected]')
currency.check_currency()
@sasha9905
Copy link

Help me. Это код:
import time
import requests
from bs4 import BeautifulSoup

class Currency:

BTC = 'https://www.google.ru/search?q=%D0%BA%D1%83%D1%80%D1%81+%D0%B1%D0%B8%D1%82%D0%BA%D0%BE%D0%B8%D0%BD%D0%B0&newwindow=1&sxsrf=ALeKk01IAJrreXlAGktOcxfyXCPGOK8SfQ%3A1622663762133&source=hp&ei=UuK3YJ_uBL6K9u8P4MuHsAQ&iflsig=AINFCbYAAAAAYLfwYlrljveuFKuy7gW8btRhQKgWKToc&oq=%D0%BA%D1%83%D1%80%D1%81+%D0%B1%D0%B8%D1%82%D0%BA%D0%BE%D0%B8%D0%BD%D0%B0&gs_lcp=Cgdnd3Mtd2l6EAMyDQgAELEDEIMBEEYQggIyCAgAELEDEIMBMggIABCxAxCDATIICAAQsQMQgwEyCAgAELEDEIMBMgUIABCxAzIICAAQsQMQgwEyAggAMgUIABDJAzICCAA6CwguELEDEIMBEJMCOgsIABCxAxDHARCjAjoCCC46BQguELEDOggIABDHARCjAjoICAAQChABECo6BggAEAoQAToOCAAQsQMQgwEQxwEQowI6DAgAELEDEIMBEAoQKjoKCAAQsQMQgwEQCjoHCAAQsQMQCjoECAAQClDXJliOOmCuO2gGcAB4AoABT4gB6weSAQIxNpgBAKABAaoBB2d3cy13aXqwAQA&sclient=gws-wiz&ved=0ahUKEwif34PJ3fnwAhU-hf0HHeDlAUYQ4dUDCAc&uact=5'
headers = {'user-agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36', 'accept' : '*/*'}

money: float = 0

def init(self):
self.money = self.get_currency_price()

def get_currency_price(self):
seconds = time.time()
local_time = time.ctime(seconds)
r = requests.get(self.BTC, headers=self.headers)
soup = BeautifulSoup(r.content, 'html.parser')
convert = soup.find_all('span', class_="DFlfde SwHCTb")
return float(convert[0].text.replace(',', '.'))

def check_currency(self):
currency = self.get_currency_price()
print(type(currency))
print('\nСейчас курс: 1 BTC = ' + convert[0].text)
print("Местное время:", local_time)
time.sleep(3)
self.check_currency()
currency = Currency()
currency.check_currency()

Это ошибка:
Traceback (most recent call last):
File "F:/Python/Ошибки.py", line 31, in
currency.check_currency()
AttributeError: 'Currency' object has no attribute 'check_currency'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment