您的位置: 网站首页> SEO工具> 当前文章

selenium贴吧关注(多账号多吧)

老董-我爱我家房产SEO2020-11-13165围观,133赞

  一批百度账号,每个账号关注一批贴吧,然后慢慢养号。手动登录关注贴吧劳心劳力影响心情,必须借助脚本。selenium百度贴吧多账号多吧关注

  功能:多账号多吧,每个账号关注多个贴吧,默认15-20秒关注一次。

  1、需要准备tieba_urls.txt 一行一个贴吧地址(https://tieba.baidu.com/f?kw=百度&ie=utf-8)

  2、准备cookie_zh.txt一行一个账号cookie

  3、结果会生成一个tieba_success_guanzhu.txt 来记录关注成功的账号和吧。

# -*- coding: utf-8 -*-
"""
功能:多账号多吧,每个账号关注多个贴吧
默认15-20秒关注一次
准备tieba_urls.txt 一行一个贴吧地址
准备cookie_zh.txt一行一个cookie
tieba_success_guanzhu.txt 记录关注成功的账号和吧
"""

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
import random
import gc



def get_driver(chromedriver_path,chrome_path,ua):
    ua = ua
    option = Options()
    option.binary_location = chrome_path
    option.add_argument("user-agent=" + ua)
    option.add_argument("--no-sandbox")
    option.add_argument("--disable-dev-shm-usage")
    option.add_argument("--disable-gpu")
    option.add_argument("--disable-features=NetworkService")
    option.add_argument("--disable-features=VizDisplayCompositor")
    # option.add_argument('headless')
    option.add_argument('log-level=3')  # 屏蔽日志
    option.add_argument('--ignore-certificate-errors-spki-list')  # 屏蔽ssl error
    option.add_argument('-ignore -ssl-errors')  # 屏蔽ssl error
    option.add_experimental_option("excludeSwitches", ["enable-automation"])
    option.add_experimental_option('useAutomationExtension', False)
    No_Image_loading = {"profile.managed_default_content_settings.images": 1}
    option.add_experimental_option("prefs", No_Image_loading)
    # 屏蔽webdriver特征
    option.add_argument("--disable-blink-features")
    option.add_argument("--disable-blink-features=AutomationControlled")
    driver = webdriver.Chrome(options=option, chrome_options=option,executable_path=chromedriver_path)
    return driver


# 获得所有账号cookie
def get_cookie(filepath):
    cookie_list = []
    cookie_list = [line.strip() for line in open(filepath,'r',encoding='utf-8')]
    return cookie_list


# 字符串cookie转为字典
def to_dict(cookie_str):
    cookie = {}
    lists = cookie_str.split(';')
    for i in lists:
        j = i.strip()
        j = j.split('=')
        cookie[j[0]] = j[1]
    return cookie


# 自动登录
def auto_login(cookie_dict):
    num = user_name = 0
    teiba_index = 'https://tieba.baidu.com/'
    driver.get(teiba_index)
    driver.delete_all_cookies()
    for k, v in cookie_dict.items():
        driver.add_cookie({'name': k, 'value': v})
    try:
        driver.get(teiba_index)  # 带cookie访问
        # 右侧导航加载
        navs = WebDriverWait(driver, 30).until(
            EC.visibility_of_element_located((By.ID, "com_userbar"))
        )
        li_list = driver.find_elements_by_css_selector('#com_userbar > ul >li')
        li_classnames = [li.get_attribute('class') for li in li_list]
        if 'u_username' in li_classnames:
            num = 1
            user = WebDriverWait(driver, 30).until(
                EC.visibility_of_element_located(
                    (By.CSS_SELECTOR, "#j_u_username > div.u_menu_item.u_menu_username > a > span"))
            )
            user_name = user.text
    except Exception as e:
        print('登陆过程异常',e)
    else:
        pass
    finally:
        return num,user_name


# 点击关注按钮一次,如果已关注点一下不会取消关注
def guanzhu(url):
    driver.get(url)
    js = 'window.scrollBy(0,{0})'.format('document.body.scrollHeight * {0}'.format(random.random()/10))
    driver.execute_script(js)
    # 等待加载
    guanzhu = WebDriverWait(driver, 30).until(
        EC.visibility_of_element_located((By.ID, "j_head_focus_btn"))
    )
    guanzhu_click_js = 'document.getElementById("j_head_focus_btn").click()'
    driver.execute_script(guanzhu_click_js)
    time.sleep(random.randrange(sleep_min,sleep_max))
    return 1


def main(cookie_list):
    for cookie_str in cookie_list:
        cookie_dict = to_dict(cookie_str)
        try:
            num,user_name = auto_login(cookie_dict)
            if num == 1:
                print(user_name,'自动登录成功')
                for tieba_url in tieba_urls:
                    guanzhu_num = guanzhu(tieba_url)
                    if guanzhu_num == 1:
                        print(user_name,tieba_url, '关注成功')
                        f_ok.write(cookie_str + '
')
                    time.sleep(random.randrange(sleep_min, sleep_max))
                f_ok.flush()
        except Exception as e:
            print('检查问题',e)
            gc.collect()
        finally:
            time.sleep(random.randrange(sleep_min, sleep_max))


if __name__ == "__main__":
    # 记录关注成功
    f_ok = open('tieba_success_guanzhu.txt', 'w', encoding='utf-8')
    chromedriver_path = 'D:/python3/install/chromedriver.exe'
    chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe'
    ua = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36'
    # 全局变量
    driver = get_driver(chromedriver_path, chrome_path, ua)
    # cookie账号路径
    cookie_path = './cookie_zh.txt'
    # 要关注的吧
    tieba_urls = [i.strip() for i in open('tieba_urls.txt', 'r', encoding='utf-8')]
    # cookie账号路径
    cookie_path = './cookie_zh.txt'
    cookie_list = get_cookie(cookie_path)
    # 登陆间隔时间最小and最大
    sleep_min, sleep_max = 15, 20
    main(cookie_list)
    f_ok.close()
    driver.quit()

很赞哦!

python编程网提示:转载请注明来源www.python66.com。
有宝贵意见可添加站长微信(底部),获取技术资料请到公众号(底部)。同行交流请加群 python学习会

文章评论

    selenium贴吧关注(多账号多吧)文章写得不错,值得赞赏

站点信息

  • 网站程序:Laravel
  • 客服微信:a772483200