用python贴吧自动回帖_python基于selenium实现贴吧自动发帖
說明:本文僅供學習參考;請勿用作其他用途
1.功能
已經實現:實現貼吧自動發帖功能(包括標題,內容,上傳圖片),標題和內容加入了簡單的隨機字符串,每發一次會隨機等待一段時間,關注人數少于50000的不發
缺陷:自動登錄需要關閉手機驗證,且第一次驗證碼需要手動拖拽,有想法的同學可以買第三方來做驗證碼自動識別;發帖間隔不宜過快,否則每次發帖都會要求輸入驗證碼
2.開始編碼
以下部分直接上代碼,未做較好的封裝,有興趣的朋友自己優化...
# -*- coding: utf-8 -*-
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
import logging
import sys
import random
import uuid
from pywinauto import Application
from pywinauto.keyboard import send_keys
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s', filemode='w',
filename="log.txt")
BROWSER_URL = r'D:\program files (x86)\360chrome\Chrome\Application\360chrome.exe'
from pywinauto.application import Application
class WinAuto:
def __init__(self, class_name, title_re):
# 連接到指定應用程序,此處為連接到指定窗口
self.app = Application().connect(class_name=class_name, title_re=title_re)
# 定位窗口方法
def get_window(self, window_object, class_name="", title_re=""):
return window_object.window(class_name=class_name, title_re=title_re)
# 向編輯框輸入指定信息
def file_input(self, file_path):
# 定位到標題名為“打開”對話框
window = self.get_window(self.app, "#32770", "打開")
# 定位到編輯框
window = self.get_window(window, class_name="Edit")
# 向編輯框中輸入信息
window.TypeKeys(file_path)
# 點擊【打開】按鈕
def open_button_click(self):
# 定位到標題名為“打開”對話框
window = self.get_window(self.app, "#32770", "打開")
# 定位到【打開】按鈕
button = self.get_window(window, class_name="Button", title_re="打開")
# 點擊【打開】按鈕
button.click()
def get_titile():
title_list = [
"雙十一最強攻略,保證有驚喜,進來了解一下吧",
"雙十一擼貨套路,來了解一下",
"雙11無敵神車已經發車,來了解一下把",
"雙11最強攻略,保證你不后悔",
"雙十一最強攻略,走過路過不要錯過",
]
t = random.choice(title_list)
return t + str(uuid.uuid4()).split('-')[-1]
def get_content():
content = """testest{uuid}atetset{uuid}testest{uuid}test""".format(uuid=str(uuid.uuid4()).split('-')[-1])
return content
def get_datas():
with open('tieba.txt', 'r', encoding='utf-8', errors='ignore') as fp:
schools = fp.readlines()
title = get_titile()
content = get_content()
return title, content, schools
def get_sleep_time(num):
if num % 5 == 0:
t = random.choice(range(180, 300))
else:
t = random.choice(range(35, 125))
logging.info("sleep {}".format(t))
return t
def start_chrome():
chrome_options = Options()
chrome_options.binary_location = BROWSER_URL
# chrome_options.add_argument("–incognito")
driver = webdriver.Chrome(options=chrome_options)
driver.implicitly_wait(20)
if login_tieba(driver):
title, content, schools = get_datas()
num = 0
for name in schools:
url = u"https://tieba.baidu.com/f?ie=utf-8&kw=%s&fr=search" % name
logging.info("start request {}, url:{}".format(name, url))
try:
driver.get(url)
except Exception as e:
logging.error("request {} error".format(name))
continue
followers = driver.find_element_by_class_name("card_menNum").text
logging.info("followers is :{}".format(followers))
followers = followers.replace(',', "")
if int(followers) < 50000:
logging.info("Less than 50000 followers")
continue
try:
driver.find_element_by_xpath("/html/body/ul/li[2]/a").click()
sleep(1)
driver.find_element_by_xpath('//*[@id="tb_rich_poster"]/div[3]/div[1]/div[2]/input').send_keys(title)
# driver.quit()
sleep(1)
driver.find_element_by_id("ueditor_replace").send_keys(content)
sleep(1)
driver.find_element_by_class_name('edui-btn-image').click()
sleep(1)
driver.find_element_by_class_name('pic_upload_container').click()
sleep(1)
driver.find_element_by_class_name('next_step').click()
sleep(1)
# 定位打開窗口
window = WinAuto("#32770", "打開")
sleep(1)
if num % 2 == 0:
window.file_input(r'F:\py_home\selenium_auto\2.jpg')
else:
window.file_input(r'F:\py_home\selenium_auto\3.jpg')
sleep(1)
window.open_button_click()
sleep(8)
driver.find_element_by_link_text('插入圖片').click()
sleep(5)
driver.find_element_by_xpath('//*[@id="tb_rich_poster"]/div[3]/div[5]/div/button[1]').click()
except Exception as e:
logging.error("post article failed,: {}".format(e))
import traceback
traceback.print_exc()
continue
num += 1
t = get_sleep_time(num)
sleep(t)
driver.quit()
def login_tieba(driver):
tieba_url = 'https://tieba.baidu.com/'
driver.get(tieba_url)
driver.find_element_by_xpath('//*[@id="com_userbar"]/ul/li[4]/div/a').click()
sleep(2)
try:
driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_11__footerULoginBtn"]').click()
driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_11__userName"]').send_keys('用戶名')
driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_11__password"]').send_keys('密碼')
driver.find_element_by_xpath('//*[@id="TANGRAM__PSP_11__submitWrapper"]').click()
logging.info("login success")
except Exception:
logging.error("login failed")
sys.exit(1)
logging.info("wait for 10s")
sleep(10)
return True
if __name__ == '__main__':
start_chrome()
總結
以上是生活随笔為你收集整理的用python贴吧自动回帖_python基于selenium实现贴吧自动发帖的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 线段树区间gcd
- 下一篇: 电脑打开html不显示图片,网页不显示图