| | |
| | | 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): |
| | |
| | | :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) |
| | |
| | | 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.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") |