-
Notifications
You must be signed in to change notification settings - Fork 0
/
myblog.py
157 lines (134 loc) · 5.28 KB
/
myblog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/python
# -*-coding:utf-8 -*-
import time
import json
import pickle
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
# from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import platform
import os
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
import logging
import traceback
import datetime
import time
from kernel import interface_db
# 获取发表内容
def query_sleep_content():
return interface_db.DailyGetUpEvent()
# 初始化浏览器 打开微博登录页面
def init_browser(chromedriver_path: str):
# 采用谷歌浏览器
chrome_options = Options()
chrome_options.add_argument('--no-sandbox') # 参数是让Chrome在root权限下跑
chrome_options.add_argument('--disable-gpu')
# chrome_opt.add_argument('start-maximized')
chrome_options.add_argument('disable-infobars')
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--window-size=1920x1480')
# chrome_options.add_argument('blink-settings=imagesEnabled=false') # 无图模式
sys = platform.system()
print(sys)
if sys == "Windows":
print("sys=OS is Windows!!!")
elif sys == "Linux":
print("sys=OS is centos!!!")
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
# headless将在无头模式下启动Chrome浏览上下文
# chrome_options.binary_location = chromedriver_path Chrome has crashed. 不能这样写
# 创建Chrome浏览器对象
service_path = Service(chromedriver_path) # 将文件路径作为参数传入Service对象
driver = webdriver.Chrome(service=service_path, options=chrome_options)
return driver
# # 检测cookies的有效性
# def check_cookies():
# # 读取本地cookies
# cookies = read_cookies()
# s = requests.Session()
# for cookie in cookies:
# s.cookies.set(cookie['name'], cookie['value'])
# response = s.get("https://weibo.com")
# response.encoding = response.apparent_encoding
# html_t = response.text
# # 检测页面是否包含微博用户名
# if '用户7720733258' in html_t:
# return True
# else:
# return False
def gen_url_Cookies(driver, cook_path: str, url: str):
is_gen_cook = False
if not os.path.exists(cook_path):
print("cook_path not exists,please login")
is_gen_cook = True # 过期
if not is_gen_cook:
logging.debug(r"cool is is right")
return # 没有过期
# 用法:https://selenium-python.readthedocs.io/getting-started.html
driver.get(url)
time.sleep(30) # 留时间进行扫码
# 在Python中,Pickle模块就用来实现数据序列化和反序列化。
print("login succe")
cookies = driver.get_cookies()
# 该方法实现的是将序列化后的对象obj以二进制形式写入文件file中,进行保存
# https://zhuanlan.zhihu.com/p/271674011
pickle.dump(cookies, open(cook_path, "wb"))
jsCookies = json.dumps(cookies) # 转换成字符串保存
# with open(r"/root/bin/cookies.txt", 'w') as f:
# f.write(jsCookies)
print("dump cookies succed")
def loginWithCookies(browser, cookpath, url):
browser.get(url)
cookies = pickle.load(open(cookpath, "rb"))
print(cookies)
for cookie in cookies:
if 'expiry' in cookie:
cookie['expiry'] = int(cookie['expiry'])
browser.add_cookie(cookie)
time.sleep(2)
browser.refresh()
print("loginWithCookies")
"""
kill 进程 防止 命令执行失败
"""
def KillChromebeta():
os.system("ps -ef | grep google-chrome | grep -v grep | awk '{print $2}' | xargs kill")
os.system("ps -ef | grep chrome-beta | grep -v grep | awk '{print $2}' | xargs kill")
"""
博客发表接口
class="comment-area"
"reply-btn send-btn-b clearTpaErr
"""
def InterfaceSendToBlog(browser, post_url, bodyMsg, submit, content):
print(r"push {post_url} begin")
# load
browser.get(post_url)
time.sleep(3)
# 填写内容
weitoutiao_content = WebDriverWait(browser, 10).until(EC.presence_of_element_located(
(By.CSS_SELECTOR, ".PostContainer_webpcBox_2GefL")))
time.sleep(1)
weitoutiao_content.send_keys(content)
time.sleep(1)
# https://blog.csdn.net/weixin_44065501/article/details/89314538
weitoutiao_content.send_keys(Keys.ENTER)
time.sleep(2)
# class ="byte-btn byte-btn-primary byte-btn-size-default byte-btn-shape-square publish-content" type="button" > < span > 发布 < / span > < / button >
weitoutiao_send_btn = browser.find_element(By.CSS_SELECTOR,
".Button_webpcButton_2jdHV.Button_primary_3fC65.Button_small_1FrvR")
time.sleep(2)
if weitoutiao_send_btn is None:
print("submit is miss")
# 模拟鼠标点击动作
# https://juejin.cn/post/7119756252850159647
# weitoutiao_send_btn.send_keys(Keys.SPACE)
weitoutiao_send_btn.click()
time.sleep(1)
print(r"push {post_url} ok")
logging.info(r"push {post_url} ok")