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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| import os import json
class CookieLogin: def __init__(self,f_path): """ 对象初始化 :param url: 首页地址 :param f_path: Cookies文件保存路径 """ self.f_path = f_path
def save_cookies(self, data, encoding="utf-8"): """ Cookies保存方法 :param data: 所保存数据 :param encoding: 文件编码,默认utf-8 """ with open(self.f_path, "w", encoding=encoding) as f_w: json.dump(data, f_w) print("save done!")
def load_cookies(self, encoding="utf-8"): """ Cookies读取方法 :param encoding: 文件编码,默认utf-8 """ if os.path.isfile(self.f_path): with open(self.f_path, "r", encoding=encoding) as f_r: user_cookies = json.load(f_r) return user_cookies
from selenium import webdriver from selenium.webdriver.edge.service import Service from selenium.webdriver.edge.options import Options from selenium.webdriver.common.by import By from time import sleep
prefs = { 'profile.default_content_setting_values': { 'notifications': 2 }, 'credentials_enable_service': False, 'profile.password_manager_enabled': False }
edge_options = Options() edge_options.binary_location = "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge" driver_path = "/Users/yanghongli/Desktop/edgedriver_mac64_m1/msedgedriver" service = Service(driver_path) driver = webdriver.Edge(service=service, options=edge_options)
driver.maximize_window() driver.implicitly_wait(5)
url = "https://weibo.com/" driver.get(url=url)
sleep(4) driver.delete_all_cookies()
login = CookieLogin("cookie.json") cookies = login.load_cookies() try: for cookie in cookies: cookie_dict = { 'domain': '.weibo.com', 'name': cookie.get('name'), 'value': cookie.get('value'), "expires": '', 'path': '/', 'httpOnly': False, 'HostOnly': False, 'Secure': False } print(cookie_dict) driver.add_cookie(cookie_dict) except Exception as e: print(e)
sleep(5)
driver.refresh()
sleep(5)
profile = driver.find_element( By.XPATH, '//img[@alt="profile"]') profile.click() sleep(2)
more_buttons = driver.find_elements( By.XPATH, '//i[@class="woo-font woo-font--angleDown morepop_action_bk3Fq"]' )
print(f"找到 {len(more_buttons)} 条微博")
first_weibo = driver.find_element( By.XPATH, '//i[@class="woo-font woo-font--angleDown morepop_action_bk3Fq"]') first_weibo.click() sleep(2)
only_me = driver.find_element( By.XPATH, '//div[normalize-space()="删除"]') only_me.click() sleep(2)
confirm_btn = driver.find_element( By.XPATH, '//button[@class="woo-button-main woo-button-flat woo-button-primary woo-button-m woo-button-round woo-dialog-btn"]') confirm_btn.click() sleep(2)
driver.refresh()
sleep(5) driver.close() driver.quit()
|