• python自动回答百度知道


    同目录新建 img 文件夹,程序会在后台运行,登录二维码会在img里面,打开扫码登录,登录成功后终端回车即可开始答题

    必备:

    pip install selenium

    google浏览器,对应版本的插件

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    import json, time
    
    url = 'https://zhidao.baidu.com/list?cid=110'
    path = r'./chromedriver.exe'
    
    # 创建一个参数对象,用来控制chrome以无界面模式打开
    chrome_options = Options()
    #chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')
    
    
    driver = webdriver.Chrome(executable_path=path, chrome_options=chrome_options)
    driver.set_window_size(1920, 1080)  # 参数数字为像素点
    driver.implicitly_wait(20)
    driver.get(url)
    time.sleep(1)
    
    # 登陆
    driver.find_element_by_xpath('//*[@id="userbar-login"]').click()
    time.sleep(5)
    driver.save_screenshot('./img/code.png')
    
    
    a = input('准备扫码登陆,登陆好后 回车 即可开始答题。')
    print('*******************开始答题*******************')
    kkk = 0
    
    while (1):
        all_handles = driver.window_handles
        driver.switch_to.window(driver.window_handles[0])
        driver.refresh()
        kkk = 0
    
        title_link = driver.find_elements_by_class_name('title-link')
        for i in title_link:
            try:
                all_handles = driver.window_handles
                for ii, v in enumerate(all_handles):
                    if ii != 0:
                        driver.switch_to.window(v)
                        driver.close()
                kkk = kkk + 1
            except:
                pass
            if kkk == 10:
                break
            driver.switch_to.window(driver.window_handles[0])
            href = i.get_attribute('href')
            driver.execute_script('window.open("%s");' % (href))
            time.sleep(5)
            driver.switch_to.window(driver.window_handles[1])
            try:
                driver.find_element_by_id('ueditor_0')
                title = driver.find_element_by_class_name('ask-title ').text
                title_url = 'https://zhidao.baidu.com/search?&word=' + title
                js = 'window.open("%s");' % (title_url)
                driver.execute_script(js)
                time.sleep(5)
                driver.switch_to.window(driver.window_handles[2])
                answer_list = driver.find_elements_by_class_name('dt,mb-4,line')
                for k in answer_list:
                    href = k.find_element_by_tag_name('a').get_attribute('href')
                    driver.execute_script('window.open("%s");' % (href))
                    time.sleep(5)
                    driver.switch_to.window(driver.window_handles[3])
                    try:
                        text = driver.find_element_by_class_name('best-text,mb-10').text
                        text = '您好,非常荣幸能在此回答您的问题。以下是我对此问题的部分见解,若有错误,欢迎指出。' + text + '非常感谢您的耐心观看,如有帮助请采纳,祝生活愉快!谢谢!'
                    except:
                        text = ''
                    finally:
                        driver.close()
                    if text:
                        driver.switch_to.window(driver.window_handles[2])
                        driver.close()
                        driver.switch_to.window(driver.window_handles[1])
                        driver.switch_to.frame('ueditor_0')
                        driver.find_element_by_xpath('/html/body').click()
                        driver.find_element_by_xpath('/html/body').send_keys(text)
                        driver.switch_to.default_content()
                        driver.find_element_by_xpath('//*[@id="answer-editor"]/div[2]/a').click()
                        print("成功回答一个问题")
                        time.sleep(5)
                        driver.switch_to.window(driver.window_handles[1])
                        driver.close()
                        break
    
            except Exception as err:
                all_handles = driver.window_handles
                for i, v in enumerate(all_handles):
                    if i != 0:
                        driver.switch_to.window(v)
                        driver.close()
                driver.switch_to.window(driver.window_handles[0])
                # print(err)
  • 相关阅读:
    HDU 1160 dp中的路径问题
    zzuli 1907: 小火山的宝藏收益
    POJ 3414 dfs广搜直接应用
    http://acm.zzuli.edu.cn/zzuliacm/problem.php?cid=1158&pid=5 二分函数的间接应用
    LightOJ 1067 组合数取模
    九段美到极致的句子
    质数和分解
    codevs 1080 线段树练习
    codevs 2806 红与黑
    codevs 2152 滑雪
  • 原文地址:https://www.cnblogs.com/zwnsyw/p/13743209.html
Copyright © 2020-2023  润新知