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
| from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys import time import re import csv from datetime import datetime import mysql.connector
def extract_numbers(text): numbers = re.findall(r'\d+\.\d+|\d+', text) return ''.join(numbers)
def extract_info():
chrome_options = Options() chrome_options.add_argument("headless") chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--disable-gpu') chrome_options.add_argument('--disable-dev-shm-usage') chrome_options.add_argument("--window-size=1920x1080")
driver = webdriver.Chrome(options=chrome_options)
driver.get('https://xxxxx/login')
username_field = driver.find_element(By.XPATH, '//input[@type="tel" and @placeholder="请输入手机号"]') password_field = driver.find_element(By.XPATH, '//input[@type="password" and @placeholder="请输入密码"]') username_field.send_keys('xxxxxx') password_field.send_keys('xxxxxxx') password_field.send_keys(Keys.RETURN)
login_button = driver.find_element(By.XPATH, '//button[contains(@class, "van-button") and .//span[text()="登录"]]') login_button.click()
time.sleep(2)
my_button = driver.find_element(By.XPATH, '//div[contains(@class, "van-tabbar-item") and .//span[text()="我的"]]') my_button.click()
time.sleep(2)
recharge_button = driver.find_element(By.XPATH, '//div[contains(@class, "tabcoloritem") and .//p[text()="缴费充值"]]') recharge_button.click()
time.sleep(2) top_up_button = driver.find_element(By.XPATH, '//div[contains(@class, "tabTitle2Grey") and text()="充值"]') top_up_button.click()
time.sleep(5)
room_number = driver.find_element(By.XPATH, '//div[contains(@style, "font-size: 14px") and contains(@style, "font-weight: 600")]').text remaining_water = driver.find_element(By.XPATH, '//p[contains(@class, "red") and contains(text(), "吨")]').text remaining_electricity = driver.find_element(By.XPATH, '//p[contains(@class, "red") and contains(text(), "度")]').text
room_number = extract_numbers(room_number) remaining_water = extract_numbers(remaining_water) remaining_electricity = extract_numbers(remaining_electricity)
driver.quit() current_date = datetime.now().strftime('%Y-%m-%d %H:%M') info = [int(room_number), float(remaining_water), float(remaining_electricity), current_date]
return info
def append_to_csv(info, csv_file_path): with open(csv_file_path, mode='a', newline='') as file: writer = csv.writer(file) writer.writerow(info)
def insert_into_mysql(info, db_config): connection = mysql.connector.connect(**db_config) cursor = connection.cursor()
insert_query = "INSERT INTO utility_usage (room_number, remaining_water, remaining_electricity, date) VALUES (%s, %s, %s, %s)"
cursor.execute(insert_query, info)
connection.commit()
cursor.close() connection.close()
if __name__ == "__main__": csv_file_path = 'output.csv'
db_config = { 'host': 'xxx.xxx.xxx.xxx', 'user': 'xxxx', 'password': 'xxxx', 'database': 'xxxxx' }
info = extract_info()
append_to_csv(info, csv_file_path)
insert_into_mysql(info, db_config)
print(info)
|