机能

早已同时实现:同时实现贴吧全自动回帖机能(主要包括副标题,文本,上载相片),副标题和文本重新加入了单纯的乱数数组,每发一场会乱数等候一两年,高度关注数目多于50000的充耳不闻。

瑕疵:全自动登入须要停用智能手机校正,且第二次接收者须要全自动拖曳,有设想的老师能买服务器端来做接收者全自动识别;回帖间距切忌迟滞,不然每天回帖单厢明确要求输出接收者。

早已开始标识符

下列部份间接上标识符,未做良好的PCB,有兴趣的好友他们强化~

-*- coding: utf-8 -*-

fromselenium.webdriver.chrome.optionsimportOptions

fromseleniumimportwebdriver

fromselenium.webdriver.common.keysimportKeys

fromtimeimportsleep

importlogging

importsys

importrandom

importuuid

frompywinautoimportApplication

frompywinauto.keyboardimportsend_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 =rD:\program files

(x86)\360chrome\Chrome\Application\360chrome.exe

frompywinauto.applicationimportApplication

classWinAuto:

def__init__(self, class_name, title_re):

连接到指定应用程序,此处为连接到指定窗口

self.app = Application().connect(class_name=class_name,

title_re=title_re)

定位窗口方法

defget_window(self, window_object, class_name="", title_re=""):

returnwindow_object.window(class_name=class_name,

title_re=title_re)

向编辑框输出指定信息

deffile_input(self, file_path):

定位到副标题名为打开对话框

window = self.get_window(self.app,"32770","打开")

定位到编辑框

window = self.get_window(window, class_name="Edit")

向编辑框中输出信息

window.TypeKeys(file_path)

点击【打开】按钮

defopen_button_click(self):

定位到副标题名为打开对话框

window = self.get_window(self.app,"32770","打开")

定位到【打开】按钮

button = self.get_window(window, class_name="Button", title_re="打开")

点击【打开】按钮

button.click()

defget_titile():

title_list = [

"双十一最强攻略,保证有惊喜,进来了解一下吧",

"双十一撸货套路,来了解一下",

"双11无敌神车早已发车,来了解一下",

"双11最强攻略,保证你不后悔",

"双十一最强攻略,走过路过不要错过",

]

t = random.choice(title_list)

returnt + str(uuid.uuid4()).split(-)[-1]

defget_content():

content =

"""testest{uuid}atetset{uuid}testest{uuid}test""".format(uuid=str(uuid.uuid4()).split(-)[-1])

returncontentdefget_datas():

withopen(tieba.txt,r, encoding=utf-8, errors=ignore)asfp:

schools = fp.readlines()

title = get_titile()

content = get_content()

returntitle, content, schools

defget_sleep_time(num):

ifnum % 5 == 0:

t = random.choice(range(180, 300))

else:

t = random.choice(range(35, 125))

logging.info("sleep {}".format(t))

returnt

defstart_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)

iflogin_tieba(driver):

title, content, schools = get_datas()

num = 0

fornameinschools:

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)

exceptExceptionase:

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(,,"")

ifint(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)

ifnum % 2 == 0:

window.file_input(rF:\py_home\selenium_auto\2.jpg)

else:

window.file_input(rF:\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()exceptExceptionase:

logging.error("post article failed,: {}".format(e))

importtraceback

traceback.print_exc()

continue

num += 1

t = get_sleep_time(num)

sleep(t)

driver.quit()

deflogin_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")

exceptException:

logging.error("login failed")

sys.exit(1)

logging.info("wait for 10s")

sleep(10)

returnTrue

if__name__ ==__main__:

start_chrome()