python123上的彩蛋码快速获取

把课程学习完之后,看到python123上说有彩蛋抽奖,每个彩蛋码位于一个章节pdf中:

如果要找出45个彩蛋码的话,需要下载这45个pdf文档,并且逐个打开文档来找彩蛋码,再逐个在python123上输入并电击确认。

如何能够快速的找出彩蛋码?本着学以致用的态度, 采用python来部分自动化实现. 

(在此首先感谢python课程团队和慕课网,同时也感谢python123上提供的练习和彩蛋)

思路是:

首先:把pdf文档合并到一个pdf文档中(pyPDF2合并)

然后:使用pdf中的查找来把所有的彩蛋码找出来(这一步使用福昕阅读器中的查找PY0,一下子就可以查找全部出来)

最后:逐个在网站上提交彩蛋码(打开网站后,模拟copy,paste,点击提交)

1. 文档合并代码实现的话:

import os

from PyPDF2 import PdfFileReader, PdfFileMerger

fileDir=".\\pdffiles"

if (os.path.exists(fileDir)):

    print("open pdf folder <{}>".format(fileDir).center(50,'#'))

else:

    print("input folder not exist <{}>".format(fileDir).center(50,'#'))

    sys.exit()

 

allFiles=os.listdir(fileDir)

 

pdfWriter = PdfFileWriter()
outputPages = 0
for fileName in allFiles:
    #ignore other type files
    if (not fileName.endswith(".pdf")) and (not fileName.endswith(".PDF")):
        continue
    #add file and bookmark to file
    srcFile = fileDir + "\\" + fileName
    print(srcFile)
    pdfReader = PdfFileReader(open(srcFile, "rb"))
    pageCount = pdfReader.getNumPages()
    for i in range(pageCount):
        pdfWriter.addPage(pdfReader.getPage(i))
    pdfWriter.addBookmark(title=fileName, pagenum = outputPages + 1)
    outputPages += pageCount
       
outFile="mergeOutput.pdf"
outPdf=open(outFile, "wb")
pdfWriter.write(outPdf)
outPdf.close()
print("merge output file".center(50,'-'))
print(outFile)

合并后的pdf文档:有需要的可以通过这个网址下载:https://download.csdn.net/download/chunyexiyu/10820580

 

2. 打开网站彩蛋码输入的位置, 通过程序把彩蛋码自动提交到网站:

import win32api

import win32con

import win32gui

from ctypes import *

import time

VK_CODE = {'ctrl':0x11, 'a':0x41, 'c':0x43,'v':0x56,}

class POINT(Structure):

    _fields_ = [("x", c_ulong),("y", c_ulong)]

def get_mouse_point():

    po = POINT()

    windll.user32.GetCursorPos(byref(po))

    return int(po.x), int(po.y)

def mouse_click(x=None,y=None):

    if not x is None and not y is None:

        mouse_move(x,y)

        time.sleep(0.05)

    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)

    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)

def mouse_move(x,y):

    windll.user32.SetCursorPos(x, y)

 

import pyperclip

def key_paste(str=""):

    pyperclip.copy(str)

    win32api.keybd_event(VK_CODE["ctrl"],0,0,0)

    win32api.keybd_event(VK_CODE["a"],0,0,0)

    time.sleep(0.1)

    win32api.keybd_event(VK_CODE["ctrl"],0,0,0)

    win32api.keybd_event(VK_CODE["v"],0,0,0)

    time.sleep(0.1)

 

submitwords=["朝秦暮楚",

             "纵横",

             "游说"]

 

#find position

#    while True:
#        time.sleep(0.1)
#        print(get_mouse_point())
 

if __name__ == "__main__":

    for word in submitwords:

        mouse_click(308,89)  #这个根据分辨率的不同,文字输入框的位置会不同

        key_paste(word)

        mouse_click(710,89)

        time.sleep(3)

 

个人随笔 (Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu)

 

 

你可能感兴趣的:(Python,Python)