Press ESC to close

贴吧发送机器人

贴吧机器人

# 贴吧图文机器人
# 作者:20004@163.com
# 20220109-今天开始这个图文机器人
# 直接把水淼的内容图文生成到html然后粘贴进来就行了
# 20220114-解决掉粘贴图片的问题
# 20220114-加入日志功能
# 20220114-测试可以发送了,但是是一次执行的,用不同的账号也测试了,有的账号是可以发送,有的不能,是账号的敏感度的问题了

import json
import logging
import os
import time

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

# 日志的定义
LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
DATE_FORMAT = "%m/%d/%Y %H:%M:%S %p"
logging.basicConfig(level=logging.INFO, format=LOG_FORMAT, datefmt=DATE_FORMAT)

# 常量定义
BAIDU_USERNAME = ' '
BAIDU_PASSWORD = ' '
BIADU_TIEBA_INDEX_URL = 'https://tieba.baidu.com/index.html'
BAIDU_LOGIN_URL = 'https://tieba.baidu.com/home/creative/publish'
BAIDU_COOKIES_FILE_NAME = 'C:\info\tieba_user_cookies01.txt'
TEXT_FILE_DIR = 'C:\info\html'
WAIT_TIMER = '1'
CHROME_DRIVER_DIR = 'C:\chromedriver_win32\chromedriver.exe'

# 贴吧吧名
TIEBA_BAMING_LIST_FILE = 'C:\info\tieba_baming_list.txt'

# 读取html的素材库
# 读取目录的DY个html文件
files = os.listdir(TEXT_FILE_DIR)
file_path = os.path.join(TEXT_FILE_DIR, files[0])
logging.info('读取文件', file_path)
file_name = files[0].split('.')[0]
logging.info('文件名称', file_name)

# 登陆
driver = webdriver.Chrome(CHROME_DRIVER_DIR)

# 打开一个新的标签用于复制
# js='window.open()'
# driver.execute_script(js)
# js不用执行

driver.get(file_path)
# 默认会在DY次打开的标签页
logging.info('打开本地的html素材文件')
time.sleep(1)
# driver.find_element_by_class_name('BDE_Image').send_keys(Keys.CONTROL,'a')
# driver.find_element_by_class_name('BDE_Image').send_keys(Keys.CONTROL,'c')
# ActionChains(driver).send_keys(Keys.CONTROL,'a').perform()
# ActionChains(driver).send_keys(Keys.CONTROL,'c').perform()
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 'a')
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 'c')

# 读取Cookies登陆
driver.get(BAIDU_LOGIN_URL)
# 必须要先登录加入cookies的网址
f = open(BAIDU_COOKIES_FILE_NAME, 'r')
cookies_list = json.loads(f.read())

for cookie in cookies_list:
    driver.add_cookie(cookie)
    # print(cookie)
logging.info('导入cookies结束')

# 刷新chrome
driver.get(BIADU_TIEBA_INDEX_URL)
# driver.maximize_window()
driver.refresh()
logging.info('携带cookies登陆')
time.sleep(1)

# 读取贴吧吧名
tieba_baming_list = []
lines = open(TIEBA_BAMING_LIST_FILE, 'r', encoding='utf-8').readlines()
for line in lines:
    tieba_baming_list.append(line.split())
print(tieba_baming_list)
logging.info('读取贴吧吧名称')

# 登陆对应贴吧
URL_STRING1 = 'https://tieba.baidu.com/f?ie=utf-8&kw='
URL_STRING2 = '&fr=search'
# URL=URL_STRING1 + str(tieba_baming_list[0]) + URL_STRING2
URL = 'https://tieba.baidu.com/f?ie=utf-8&kw=%E5%9C%9F%E5%A3%A4%E5%9B%BA%E5%8C%96%E5%89%82%E5%8E%82%E5%AE%B6&fr=search'

driver.get(URL)
logging.info('现在开始打开贴吧')
time.sleep(1)

driver.find_element_by_xpath(
    '/html/body/div[2]/div/div[3]/div/div/div[1]/div/div[1]/div/div/div[3]/div[1]/div[2]/input').send_keys(file_name)
logging.info('开始写入标题')

# 写入对应的内容
# driver.find_element_by_xpath('//*[@id="tb_rich_poster"]/div[3]/div[1]/div[2]/input').send_keys()

# 直接粘贴到文本编辑器当中就好了
# 如果是带有图片的编辑器直接粘贴就可以了
# click_btn=driver.find_element_by_xpath('//*[@id="ueditor_replace"]')
# ActionChains(driver).click(click_btn).perform()
# driver.find_element_by_xpath('//*[@id="ueditor_replace"]').send_keys(Keys.CONTROL,'v')
# logging.info('粘贴内容到输入框中')

input1 = driver.find_element_by_xpath(
    '/html/body/div[2]/div/div[3]/div/div/div[1]/div/div[1]/div/div/div[3]/div[2]/div[2]/div/div[3]/div[2]/div[1]/p')
ActionChains(driver).move_to_element(input1).click().perform()
# ActionChains(driver).send_keys(Keys.CONTROL,'v').perform()
ActionChains(driver).key_down(Keys.CONTROL, input1).send_keys('v').key_up(Keys.CONTROL).perform()
logging.info('开始写入内容')

# 点击确定
time.sleep(2)
logging.info('等待...准备提交')
driver.find_element_by_xpath('//*[@id="tb_rich_poster"]/div[3]/div[5]/div/button[1]').click()
logging.info('点击提交')

# 删除文件
os.remove(file_path)

发表回复