Files
arlo/channel/toutiao/toutiao.py
konjacpotato 2c8426d543 import arlo
2025-11-05 21:00:19 +08:00

82 lines
3.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from DrissionPage import Chromium, ChromiumOptions
from DrissionPage.errors import ElementNotFoundError
from database.tcontentdispatch.model import TContentDispatch
from log.log_manager import log
class Toutiao:
def __init__(self, article: TContentDispatch):
self.article = article
co = ChromiumOptions().auto_port() # auto_port后需要重新登录账号
# co = ChromiumOptions()
self.browser = Chromium(addr_or_opts=co)
self.tab = self.browser.latest_tab
self.tab.get('https://mp.toutiao.com/')
def need_login(self):
self.tab.wait(5)
try:
login_dialog = self.tab.ele('.login-wrap')
log(login_dialog.html)
return True
except ElementNotFoundError:
return False
def login_with_password(self):
try:
# 1. 点击‘密码登录’按钮,切换到密码登录界面
password_login_btn = self.tab.ele('.web-login-other-login-method__list__item__icon web-login-other-login-method__list__item__icon__account_pwd')
password_login_btn.click()
# 2. 输入账号
username_input_box = self.tab.ele('.web-login-normal-input__input')
username_input_box.input('17704081680')
# 3. 输入密码
password_input_box = self.tab.ele('.web-login-button-input__input')
password_input_box.input('G*9dkvm834;.,[')
# 4. 勾选同意协议
agree_checkbox = self.tab.ele('.web-login-confirm-info__checkbox')
agree_checkbox.click()
# 5. 点击登录按钮
login_btn = self.tab.ele('.web-login-button')
login_btn.click()
except ElementNotFoundError:
log('尝试进行账号登录,但登录界面元素未找到')
def publish(self):
try:
# 1. 处理登录
if self.need_login():
self.login_with_password()
# 2. 新建文章
new_article_btn = self.tab.ele('.byte-menu-inline base_creation_tab').ele('.byte-menu-item')
new_article_btn.click()
# 3. 输入标题
title_input_box = self.tab.ele('.editor-title autofit-textarea-wrapper').ele('tag:textarea')
title_input_box.input(self.article.title)
# 4. 输入正文
content_input_area = self.tab.ele('.ProseMirror')
if self.article.ai_content:
content_input_area.input(self.article.ai_content)
else:
content_input_area.input(self.article.content)
# 5. 等待10秒文章会自动保存到草稿箱
self.tab.wait(10)
except (ElementNotFoundError, AttributeError):
log('发布文章出现异常')
finally:
# 6. 结束
self.finish()
def finish(self):
# 关闭浏览器
self.browser.quit()
if __name__ == '__main__':
article = TContentDispatch()
article.title = '今日新鲜事'
article.content = '当地时间29日阿塞拜疆总统阿利耶夫称阿克套空难原因系飞机“遭受来自地面的攻击受损”飞机在俄境内格罗兹尼附近尾部遭地面射击严重破坏且失去控制。'
toutiao = Toutiao(article)
toutiao.publish()