用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - python代码库

学习

2020-02-07 作者: 云代码会员举报

[python]代码库

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


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...