Ростелеком: Получение баланса и интернет трафика.

  • Python
Парсим трафик и баланс из личного кабинета Ростелеком с помощью библиотеки selenium.

Необходимы библиотеки:
  • selenium
  • time
  • datetime
  • re
  • lxml
  • pyodbc

import time
import pyodbc 
import datetime
import re
from time import sleep
from lxml import html, etree
from selenium import webdriver
from selenium.webdriver.common import action_chains, keys

dt = datetime.datetime.now()
dtLogs = str(dt)

number = 'номер телефона'
passw = 'пароль'
driver = webdriver.Chrome('chromedriver')

try:
	driver.get('https://lk.rt.ru/#login')
	time.sleep(3)
	
except:
	with open('logs.txt', 'at') as file:
		file.write(dtLogs + ' - Ошибка загрузки сайта: ' + number + '\n')

try:
	username = driver.find_element_by_css_selector('[cmp-name="login"]').find_element_by_tag_name('input')
	username.click()
	time.sleep(1)
	username.send_keys(number)

	password = driver.find_element_by_css_selector('[cmp-name="password"]').find_element_by_tag_name('input')
	password.click()
	time.sleep(1)
	password.send_keys(passw)

	submit = driver.find_element_by_css_selector('[cmp-element="buttonTitle"]')
	submit.click()
	time.sleep(5)
except:
	with open('logs.txt', 'at') as file:
		file.write(dtLogs + ' - Ошибка авторизации на номере: ' + number + '\n')
		
try:
	balance = driver.find_element_by_xpath('//div[@class="sum"]').find_element_by_xpath('.//span/span[1]').text
	balance = re.sub(',', '.', balance)
	balance = float(balance)
	print(balance)
	print(type(balance))
	time.sleep(1)
except:
	with open('logs.txt', 'at') as file:
		file.write(dtLogs + ' - Ошибка считывания баланса: ' + number + '\n')

try:
	driver.get('https://lk.rt.ru/#mvno?serviceId=29196085')
	time.sleep(3)
except:
	with open('logs.txt', 'at') as file:
		file.write(dtLogs + ' - Ошибка перехода в пакет "Интернет": ' + number + '\n')
		
try:
	traff_Gb = driver.find_element_by_css_selector('[cmp-element="tar_opt_TARIFF_PACKAGES_2"]').find_element_by_xpath('.//section/div/div/div/div[2]').text
	traff_Gb = re.sub('МБ', '', traff_Gb)
	traff_Gb = int(traff_Gb)/1000
	traff_Gb = float(traff_Gb)
	print(traff_Gb)
	print(type(traff_Gb))
	time.sleep(1)
except:
	with open('logs.txt', 'at') as file:
		file.write(dtLogs + ' - Ошибка считывания трафика: ' + number + '\n')

driver.quit()

try:
	cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
						  "Server=0.0.0.0;"
						  "Database=db;"
						  "Trusted_Connection=yes;"
						  "UID=table;"
						  "PWD=0000;")
						  
	cursor = cnxn.cursor()
	cursor.execute('INSERT INTO rt_balance (dt, number, traff_Gb, balance) VALUES (?, ?, ?, ?)', (dt, number, traff_Gb, balance))
	cursor.commit()
except:
	with open('logs.txt', 'at') as file:
		file.write(dtLogs + ' - Ошибка записи в БД: ' + number + '\n')
Поделиться:

Нет комментариев