百度知道自动答题

mac2025-11-15  3

用 selenium 自动在百度知道对没有最佳答案的题目进行答题,再也不用担心没有题目不去回答了

from selenium import webdriver import time url = 'https://zhidao.baidu.com/list?cid=110' brower = webdriver.Chrome() brower.get(url) brower.delete_all_cookies() # 手动登录 time.sleep(30) # brower.refresh() # 所有的问题列表 title_links = brower.find_elements_by_class_name('title-link') for i in title_links: # 列表页 brower.switch_to.window(brower.window_handles[0]) # 问题的链接 href = i.get_attribute('href') brower.execute_script("window.open(\"{}\");".format(href)) time.sleep(5) # 问题页 brower.switch_to.window(brower.window_handles[1]) try: brower.find_elements_by_id('ueditor_0') title = brower.find_element_by_class_name('ask-title').text title_url = "https://zhidao.baidu.com/search?word=" + title brower.execute_script('window.open("{}");'.format(title_url)) time.sleep(5) # 搜索答案页 brower.switch_to.window(brower.window_handles[2]) answer_list = brower.find_elements_by_class_name('dt,mb-4,line') for j in answer_list: href = j.find_element_by_tag_name('a').get_attribute('href') brower.execute_script('window.open("{}");'.format(href)) # 答案详情页 brower.switch_to.window(brower.window_handles[3]) try: text = brower.find_element_by_class_name('best-text,mb-10').text except: text = '' finally: # 关闭详情页 brower.close() if text: # 关闭答案列表 brower.switch_to.window(brower.window_handles[2]) brower.close() # 写答案 brower.switch_to.window(brower.window_handles[1]) brower.switch_to.frame('ueditor_0') brower.find_element_by_xpath('/html/body').click() brower.find_element_by_xpath('/html/body').send_keys(text) brower.switch_to.default_content() brower.find_element_by_xpath('//*[@id="answer-editor"]/div[2]/a').click() time.sleep(5) brower.switch_to.window(brower.window_handles[1]) brower.close() break except Exception as e: all_handles = brower.window_handles for i, v in enumerate(all_handles): if i != 0: brower.switch_to.window(v) brower.close() brower.switch_to.window(brower.window_handles[0]) print(e)

之前可以先保存 cookies,然后在添加入

from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By import time import json brower = webdriver.Chrome() brower.get("https://www.baidu.com") # 登录的按钮 brower.find_element_by_xpath('//*[@id="u1"]/a[7]').click() # 知道出现登录的界面 conditon_1 = EC.visibility_of_element_located((By.XPATH,'//*[@id="TANGRAM__PSP_10__footerULoginBtn"]')) WebDriverWait(brower, timeout=2).until(conditon_1) # 用户名登录的选项 brower.find_element_by_xpath('//*[@id="TANGRAM__PSP_10__footerULoginBtn"]').click() # 等到直到出现登录界面 account = brower.find_element_by_xpath('//*[@id="TANGRAM__PSP_10__userName"]') passwd = brower.find_element_by_xpath('//*[@id="TANGRAM__PSP_10__password"]') # 知道出现了输入密码 conditon_2 = EC.visibility_of_element_located((By.XPATH, '//*[@id="TANGRAM__PSP_10__password')) # WebDriverWait(brower, timeout=5).until(conditon_2) # 输入账号密码 account.send_keys("username") passwd.send_keys("password") brower.find_element_by_xpath('//*[@id="TANGRAM__PSP_10__submit"]').click() # 手工登录吧 time.sleep(10) try: brower.find_element_by_xpath('//*[@id="TANGRAM__28__button_send_mobile"]').click() code = input("Code: ") brower.find_element_by_xpath('//*[@id="TANGRAM__28__input_label_vcode"]').send_keys(str(code)) brower.find_element_by_xpath('//*[@id="TANGRAM__28__button_submit"]').click() except Exception as e: print(e) all_cookies = brower.get_cookies() with open("./cookies.txt", 'w') as file: file.write(json.dumps(all_cookies)) time.sleep(2)
最新回复(0)