# coding=utf-8 |
# 刷学习强国 |
from PyQt5 import QtCore, QtGui, QtWidgets |
from xuexi_ui import Ui_Form |
from selenium import webdriver |
import time |
import requests |
from PIL import Image |
import matplotlib.pyplot as plt |
import threading |
HOME_PAGE = 'https://www.xuexi.cn/' # 学习强国官方url |
LOGIN_LINK = 'https://pc.xuexi.cn/points/login.html' # 登录url |
VIDEO_LINK = 'https://www.xuexi.cn/lgdata/1novbsbi47k.json' # 视频数据包 |
ARTICLES_LINK = 'https://www.xuexi.cn/lgdata/1ajhkle8l72.json' # 新闻数据包 |
SCORES_LINK = 'https://pc.xuexi.cn/points/my-points.html' # 分数url |
loglist = [] |
def log_open(): |
with open ( 'xuexilog.txt' , 'r' ) as f: # 读取记录 |
templist = f.readlines() |
for line in templist: |
temp = line.strip( '\n' ) |
loglist.append(temp) |
def login(driver): |
"""模拟登录""" |
driver.get(LOGIN_LINK) # 打开网页 |
driver.execute_script( "var q=document.documentElement.scrollTop=800" ) |
driver.execute_script( "var q=document.documentElement.scrollLeft=200" ) |
# cookies=driver.get_cookies() |
time.sleep( 5 ) |
try : |
driver.get_screenshot_as_file( 'login_pic.png' ) |
img = Image. open ( 'login_pic.png' ) |
plt.figure(figsize = ( 12 , 6 ), dpi = 240 ) |
plt.axis( 'off' ) |
plt.imshow(img) |
plt.show() |
except BaseException as msg: |
print (msg) |
now_url = driver.current_url |
while now_url ! = "https://pc.xuexi.cn/points/my-study.html" : |
time.sleep( 2 ) |
now_url = driver.current_url |
print ( "模拟登录完毕\n" ) |
def watch_news(url_t, wait_time, driver): |
web_data = requests.get(url_t).json() |
time.sleep( 1 ) |
url = [urls[ 'url' ] for urls in web_data] |
select_list = [item for item in url if item not in loglist] |
for i in range ( 6 ): |
js = 'window.open("%s");' % select_list[i] |
driver.execute_script(js) |
with open ( 'xuexilog.txt' , 'a+' ) as f: |
s = select_list[i] + '\n' |
f.write(s) |
time.sleep( 1 ) |
handles = driver.window_handles |
for handle in handles: # 切换窗口(切换到搜狗) |
if handle ! = driver.current_window_handle: |
driver.switch_to_window(handle) |
js = "var q=document.documentElement.scrollTop=100000" |
driver.execute_script(js) |
time.sleep(wait_time) |
driver.close() |
driver.switch_to_window(handles[ 0 ]) |
time.sleep( 1 ) |
def start(): |
opt = webdriver.ChromeOptions() # 创建浏览器 |
opt.set_headless() # 无窗口模式 |
opt.add_argument( |
'user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36' ) |
opt.add_argument( '--no-sandbox' ) |
opt.add_argument( 'disable-infobars' ) |
opt.add_experimental_option( "excludeSwitches" , [ "ignore-certificate-errors" ]) |
driver = webdriver.Chrome(options = opt) # 创建浏览器对象 |
driver.implicitly_wait( 30 ) |
driver.maximize_window() # 最大化窗口 |
log_open() |
login(driver) |
watch_news(ARTICLES_LINK, 130 , driver) |
watch_news(VIDEO_LINK, 220 , driver) |
driver.quit() |
class MainWindow(QtWidgets.QMainWindow, Ui_Form): |
def __init__( self , parent = None ): |
super (MainWindow, self ).__init__(parent) |
self .setupUi( self ) |
self .pushButton.clicked.connect( self .PBclick) |
def PBclick( self ): |
self .pushButton.setEnabled( False ) |
self .pushButton.setText( "请耐心等待扫码窗口" ) |
t1 = threading.Thread(target = start) |
t1.start() |
初级程序员
by: 野望 发表于:2020-09-20 16:43:52 顶(0) | 踩(0) 回复
没有那个模块
回复评论