做网站后的收获,豆瓣 wordpress 插件,网站主要功能,wordpress映射到外网1.先看结构#xff1a;
声明#xff1a;我是初学#xff0c;可能有不合理的地方。 2.Base层。
我是把原来一个kimi的自动问答的代码改过来。
分析#xff1a;其实我是新手#xff0c;因为我用的浏览器是固定的#xff0c;也没有打算和别人用。所以浏览器层面年的全部写…1.先看结构
声明我是初学可能有不合理的地方。 2.Base层。
我是把原来一个kimi的自动问答的代码改过来。
分析其实我是新手因为我用的浏览器是固定的也没有打算和别人用。所以浏览器层面年的全部写死。
其他功能用到什么添什么。一步步完善。
后期我想这个这基层一直用下去。所以会一步步完善的。
base_page.py的内容如下 import timefrom selenium import webdriver
from selenium.webdriver.edge.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import pyperclip
import os
class BasePage:def __init__(self):edge_user_data_dir rC:\Users\Administrator\AppData\Local\Microsoft\Edge\User Data\Defaultedge_options Options()edge_options.use_chromium Trueedge_options.add_argument(--disable-extensions) # 禁用浏览器扩展edge_options.add_argument(--disable-gpu) # 禁用GPU硬件加速# edge_options.add_argument(--headless) # 禁用GPU硬件加速edge_options.add_argument(f--user-data-dir{edge_user_data_dir})self.driver webdriver.Edge(optionsedge_options)self.driver.maximize_window()self.wait WebDriverWait(self.driver, 300)# self.driver driver# self.driver.implicitly_wait(10)def keep_browser_open(self):保持浏览器窗口打开等待用户操作.input(按回车键退出程序并关闭浏览器...)def js_condition(self,driver):自定义等待条件函数检查JavaScript返回值return driver.execute_script(return document.readyState) completedef open_url(self,url):self.driver.get(url)self.wait.until(self.js_condition)print(页面加载完成)return Truedef find_element(self, loc):try:return self.wait.until(EC.element_to_be_clickable(loc))except Exception as e:print(f元素未找到{loc})return Falsedef find_elements(self, loc):elements self.wait.until(EC.presence_of_all_elements_located(loc))if not elements: # 可选检查是否找到元素如果没有打印提示信息print(f没有找到匹配的元素{loc})return elementsdef click_element(self,loc):element self.find_element(loc)element.click()# self.driver.execute_script(arguments[0].click();, element)def set_text(self,loc,text):elementself.find_element(loc)element.send_keys(text)def full_path(self,filename):current_dir os.getcwd()# 确保文件名加上.txt扩展名if not filename.endswith(.txt):filename .txtfull_path os.path.join(current_dir, filename)return full_pathdef clipboard_content_to_file(self,filename):full_path self.full_path(filename)clipboard_text pyperclip.paste()with open(full_path, w, encodingutf-8) as file:file.write(clipboard_text)print(f剪贴板内容已成功写入到文件: {filename})def read_line_from_file(self,filename):full_path self.full_path(filename)numeric_line None # 初始化为None表示尚未找到符合条件的行try:with open(full_path, r, encodingutf-8) as file:for line in file:# 检查行是否以数字开头if line.strip().startswith(tuple(0123456789)):numeric_line line.rstrip(\n) # 找到第一行后移除行尾的换行符并赋值break # 终止循环except FileNotFoundError:print(f文件 {filename} 未找到。)except Exception as e:print(f读取文件时发生错误: {e})print(读取当前行,numeric_line)return numeric_linedef del_line_from_file(self, filename):full_path self.full_path(filename)numeric_line None # 初始化为None表示尚未找到符合条件的行lines_to_write_back [] # 用于存储除了被删除行外的所有行try:with open(full_path, r, encodingutf-8) as file:found False # 标记是否已找到并处理符合条件的行for line in file:if not found and line.strip().startswith(tuple(0123456789)):numeric_line line.rstrip(\n) # 找到第一行后移除行尾的换行符并赋值found True # 设置标志表示已找到并处理了符合条件的行else:lines_to_write_back.append(line) # 其他行保留准备写回文件if found: # 只有在确实找到并处理了符合条件的行后才重写文件with open(full_path, w, encodingutf-8) as file:file.writelines(lines_to_write_back)except FileNotFoundError:print(f文件 {filename} 未找到。)except Exception as e:print(f读取或修改文件时发生错误: {e})print(删除当前行, numeric_line)return numeric_linedef append_content_to_file(self,filename, content):full_path self.full_path(filename)try:with open(full_path, a, encodingutf-8) as file:file.write(content \n) # 内容后添加换行符以便于区分多条内容print(f内容已成功追加到文件: {filename})except Exception as e:print(f写入文件时发生错误: {e})
测试用的代码都通过了。
url https://kimi.moonshot.cn/
case BasePage()
case.open_url(url)
#lowImage___hU90c
# img_locBy.CLASS_NAME, login____RTRY
# img_locBy.CLASS_NAME, lowImage___hU90c
#
# if case.click_element(img_loc):
# print(ok)
edit_loc By.XPATH,//div[data-slate-nodeelement]
text你好吗
case.send_text(edit_loc,text)
send_locBy.ID, send-button
case.click_element(send_loc)
case.keep_browser_open()
3.page层
分析因为我计划用于kimi或讯飞或其他所以在规划时。计划用主域名当成关键字。而每一部分不再分成独立模块如登录主页等。如何有跳转的话后期根据情况写在base层。
import time
from class_learn.base.base_page import BasePage
from selenium.webdriver.common.by import By
import pyperclip
import osclass KimiPage(BasePage):def __init__(self):super().__init__() # 假设BasePage也有初始化driver的逻辑则需要调用super().__init__()def web_ready(self,url,ok_loc,nok_loc):if self.open_url(url):if self.find_element(ok_loc):print(发现头像登录成功)else:self.click_element(nok_loc)def new_page(self,new_loc):self.click_element(new_loc)time.sleep(2)def get_questions(self, op_loc, op_loc1, edit_loc, keywords, send_loc, copy_loc):self.click_element(op_loc)time.sleep(1)self.click_element(op_loc1)time.sleep(1)self.set_text(edit_loc, keywords)self.click_element(send_loc)time.sleep(2)self.click_element(copy_loc)def set_text_and_send(self,edit_loc,text,send_loc):self.click_element(edit_loc)time.sleep(1)self.set_text(edit_loc,text)self.click_element(send_loc)def click_copy_and_save(self,copy_loc):passcase KimiPage()
url https://kimi.moonshot.cn/
nok_locBy.CLASS_NAME, login____RTRY #未登录
ok_locBy.CLASS_NAME, lowImage___hU90c #已登录
case.web_ready(url,ok_loc,nok_loc)new_locBy.XPATH,//div[data-testidmsh-sidebar-new]
case.new_page(new_loc)op_locBy.CSS_SELECTOR,.icon___zTPKp svg
op_loc1By.CSS_SELECTOR,.itemContainer___eYZxh .content___EPfWU
op_loc2By.CSS_SELECTOR,div:nth-child(2) .itemContainer___eYZxh .content___EPfWU
edit_locBy.XPATH,//div[data-slate-nodeelement]
keywords泌尿系统
send_locBy.ID, send-button
copy_locBy.XPATH, //span[contains(.,复制)]
# case.get_questions(op_loc,op_loc1,edit_loc,keywords,send_loc,copy_loc)
# case.clipboard_content_to_file(keywords)
filekeywords_ques
for i in range(100):if i%100:case.new_page(new_loc)time.sleep(2)askcase.read_line_from_file(keywords)case.get_questions(op_loc,op_loc2,edit_loc,ask,send_loc,copy_loc)case.del_line_from_file(keywords)case.append_content_to_file(file, f第{i}章 {ask})case.append_content_to_file(file,pyperclip.paste())
case.keep_browser_open()
基本完成了可以自动生成100个问题自动回答自动追加到文本中自动删除已经回答过的问题。方便系统错误后接着进行每10个问题自动开始一个新的页面。
感觉比原来好用多了。清晰了看来代码要不停的写才可以。