| | |
| | | from selenium.webdriver.support.wait import WebDriverWait |
| | | from selenium.webdriver.support import expected_conditions as EC |
| | | |
| | | import setting |
| | | |
| | | |
| | | class CommentManager: |
| | | |
| | |
| | | comment_template_str = "" |
| | | # 将中文替换为正则表达式支持的unicode编码 |
| | | comment_template_str = comment_template_str.encode('unicode_escape').decode("utf-8") |
| | | self.comment_templates = [(x.split("#")[0], x.split("#")[1]) if x.find("#")>=0 for x in comment_template_str.split("\n")] |
| | | self.comment_templates = [(x.split("#")[0], x.split("#")[1]) for x in comment_template_str.split("\n") if |
| | | x.find("#") >= 0] |
| | | self.driver = None |
| | | |
| | | def __init(self): |
| | |
| | | element = wait.until(EC.visibility_of_element_located((By.ID, "side-bar"))) |
| | | threading.Thread(target=lambda: self.click_like(self.driver), daemon=True).start() |
| | | |
| | | def __process_like(self,comment_content, comment_element): |
| | | def __process_like(self, comment_content, comment_element): |
| | | """ |
| | | 处理单条评论的赞 |
| | | :param comment_content: |
| | |
| | | like.click() |
| | | self.driver.implicitly_wait(2) |
| | | |
| | | def __process_reply(self, comment_content, comment_element): |
| | | def __process_reply(self, comment_content, comment_element): |
| | | """ |
| | | 处理单条评论的回复 |
| | | :param comment_content: |
| | | :param comment_element: |
| | | :return: |
| | | """ |
| | | |
| | | def get_reply_comment(nick_name, content): |
| | | if not setting.is_reply_comment(): |
| | | return None |
| | | # 内容是否符合标准 |
| | | for t in self.comment_templates: |
| | | if re.match(t[0], content): |
| | | # 满足内容 |
| | | retrun t[1].replace("[昵称]",nick_name) |
| | | return t[1].replace("[昵称]", nick_name) |
| | | |
| | | comment_pattern = re.compile(r'[\u4e00-\u9fa5]+') |
| | | comment_content = re.sub("<img.*?>", "", comment_content) |
| | |
| | | print("还没回复。。。。") |
| | | |
| | | comment_actions = comment_element.find_element(By.CLASS_NAME, "action-list").find_elements(By.CLASS_NAME, |
| | | "action-item") |
| | | "action-item") |
| | | # 点击评论 |
| | | comment_actions[1].click() |
| | | wait = WebDriverWait(self.driver, 5) # 最多等待10秒 |
| | | element = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "comment-create-content"))) |
| | | |
| | | replay_content = get_reply_comment(, comment_element) |
| | | # TODO 昵称 |
| | | replay_content = get_reply_comment("昵称", comment_element) |
| | | if not replay_content: |
| | | # 不需要评论 |
| | | return |
| | | |
| | | self.driver.find_element(By.CLASS_NAME, |
| | | "comment-create-content").find_element(By.TAG_NAME, |
| | | "textarea").send_keys(replay_content) |
| | | "comment-create-content").find_element(By.TAG_NAME, |
| | | "textarea").send_keys(replay_content) |
| | | self.driver.implicitly_wait(1) |
| | | self.driver.find_element(By.CLASS_NAME, |
| | | "comment-create-content").find_element(By.XPATH, "div[3]/div[2]").click() |
| | | "comment-create-content").find_element(By.XPATH, "div[3]/div[2]").click() |
| | | time.sleep(2) |
| | | |
| | | |
| | | def __click_like_all(self, driver, start_index=0): |
| | | scroll_list = driver.find_element(By.CLASS_NAME, "feed-comment__wrp") |
| | |
| | | |
| | | if __name__ == "__main__": |
| | | # CommentManager().start_process_comment() |
| | | s ="你好" |
| | | s = "你好" |
| | | print(s.encode('unicode_escape').decode("utf-8")) |
| | | |
| | | # if __name__ == "__main__": |