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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
import os import time import random import pyautogui from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.keys import Keys
chromedriver_path = 'E:\\kimi_analyze\\chromedriver-win64\\chromedriver.exe'
service = Service(executable_path=chromedriver_path)
options = Options() options.add_argument('lang=zh_CN.UTF-8')
options.add_argument('user-agent="Mozilla/5.0 (iPod; U; CPU iPhone OS 2_1 like Mac OS X; ja-jp) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5F137 Safari/525.20"')
options.add_argument(r'--user-data-dir=C:/Users/A/AppData/Local/Google/Chrome/User Data')
driver = webdriver.Chrome(service=service, options=options)
url = 'https://yiyan.baidu.com/chat/4544202670' driver.get(url) time.sleep(5)
folder_path = 'F:/midjourney_blend_Record/20240804/bbb' folder_paths_random = [ "E:/kimi_analyze/abb", "E:/kimi_analyze/bab", "E:/kimi_analyze/you", "E:/hanghai", "E:/midjourney", "E:/MyDrivers", "E:/pytorch3d", "E:/pythonstudy", "E:/painteruvtest1", "E:/unrealhoudiniplugin" ]
zishus = [ "1100", "1122", "955", "999", "1111", "1133", "966", "977", "988", "1199" ]
files = [f for f in os.listdir(folder_path) if f.lower().endswith(('.png', '.jpg', '.jpeg'))]
txt_file_path = 'E:/kimi_analyze/chromedriver-win64/BaiduPicRecognize2.txt'
with open(txt_file_path, 'r', encoding='utf-8') as txt_file: file_content = txt_file.read()
def check_upload_failure(): """ 检查网页上是否有上传失败的提示 """ try: failure_element = WebDriverWait(driver, 5).until( EC.presence_of_element_located((By.XPATH, "//span[text() = '上传失败']")) ) print("检测到上传失败的提示") return True except: print("未检测到上传失败的提示") return False
def handle_upload_failure(): """ 处理上传失败的图片 """ print("处理上传失败的图片") pyautogui.click(1657, 1690) time.sleep(2) print("已删除上传失败的图片")
def focus_file_name_input(): """ 将光标聚焦到文件名输入框 """ pyautogui.click(954, 984)
def set_file_dialog_path(path): """ 设置文件对话框的地址栏路径 """ pyautogui.click(904, 65) time.sleep(1) pyautogui.write(path) pyautogui.press('enter') time.sleep(2)
def copy_files_to_clipboard(file_paths): """ 复制文件到剪贴板 """ import win32clipboard from win32con import CF_UNICODETEXT file_list = '\n'.join(file_paths) win32clipboard.OpenClipboard() win32clipboard.EmptyClipboard() win32clipboard.SetClipboardData(CF_UNICODETEXT, file_list) win32clipboard.CloseClipboard()
def paste_files(): """ 粘贴剪贴板中的文件到对话框 """ pyautogui.hotkey('ctrl', 'v')
def get_latest_response(driver, image_file_name): """ 获取包含指定图片文件名的最新回复内容 """ response_elements = WebDriverWait(driver, 10).until( EC.presence_of_all_elements_located((By.XPATH, '//div[@class=\'q4y8uP0A\']//div[@id=\'answer_text_id\']')) )
for response_element in reversed(response_elements): response_text = response_element.text if image_file_name in response_text: return response_text
return None
def random_scroll(driver, min_duration=3, max_duration=5): """ 随机滚动网页条,持续时间在 min_duration 到 max_duration 秒之间。 """ end_time = time.time() + random.uniform(min_duration, max_duration) while time.time() < end_time: num_scrolls = random.randint(3, 5)
for _ in range(num_scrolls): ActionChains(driver).key_down(Keys.PAGE_UP).key_up(Keys.PAGE_UP).perform() time.sleep(random.uniform(0.5, 0.9))
for _ in range(num_scrolls): ActionChains(driver).key_down(Keys.PAGE_DOWN).key_up(Keys.PAGE_DOWN).perform() time.sleep(random.uniform(0.6, 0.8))
for file in files: file_path = os.path.join(folder_path, file) file_name = os.path.basename(file_path)
while True: try: print(f"开始上传文件: {file_name}") pyautogui.click(1625, 1778) time.sleep(2)
set_file_dialog_path(folder_path) time.sleep(1) folder_path_random = random.choice(folder_paths_random) set_file_dialog_path(folder_path_random) time.sleep(1) set_file_dialog_path(folder_path)
focus_file_name_input() time.sleep(2) copy_files_to_clipboard([file_name])
paste_files() time.sleep(2) pyautogui.press('enter') print("提交上传图片命令") time.sleep(8) if check_upload_failure(): print("检测到上传失败") time.sleep(2) handle_upload_failure() time.sleep(2) continue
zishu = random.choice(zishus) content = f"用{zishu}字中文详细描述我本次发送的图片,以“{file_name}”展示了为开头," send_content = content + file_content
dialog_input = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.XPATH, "//div[@class='yc-editor-wrapper']/div[@class='yc-editor' and @contenteditable='true']")) ) dialog_input.click() dialog_input.clear() dialog_input.send_keys(send_content) print("内容已发送到对话框") time.sleep(8) print("点击提交按钮") pyautogui.click(3113, 2002)
time.sleep(65) pyautogui.click(997, 1340)
random_integer = random.randint(66, 88) time.sleep(random_integer)
response_content = get_latest_response(driver, file_name) if response_content: with open(os.path.join(folder_path, f'{file_name}_response.txt'), 'w', encoding='utf-8') as response_file: response_file.write(response_content) else: print(f"No response found for {file_name}")
time.sleep(random_integer/12) pyautogui.click(997, 1340) time.sleep(random_integer/12)
except Exception as e: print(f"Error occurred: {e}") import traceback print(traceback.format_exc())
break
driver.quit()
|
评论