Automatic renewal of book collection based on python

surroundings

Python 2.7

IDE Pycharm 5.0.3

Firefox browser: 47.0.1

purpose

Automatically realize the screenshot of the book list of the library borrowed books, and renew all books with one click

Implementation

Selenium+PhantonJS automated script execution

Reasonable creation of headings is helpful for the generation of catalogues

Enter it directly once #and press spaceit to generate a level 1 heading.
After entering it twice #and pressing spaceit, a level 2 heading will be generated.
By analogy, we support level 6 headings. It helps to TOCgenerate a perfect table of contents after using the grammar.

Implementation plan

Use the Firefox browser to simulate login. This is a cool one. You can watch the browser running by yourself, and you can't stop happily. . .

Call PhantomJS.exe without showing the operation of the browser, and run it directly in the cmd window (the cmd window will appear after being packaged into an exe with pyinstaller)

Scheme realization process

Using Selenium+Firefox method:

First come the final product animation:
Insert picture description here

Then comes the program code—the main module (the called module can also be executed separately)

#-*- coding: utf-8 -*-
from selenium import webdriver
import time
#shift-tab多行缩进(左)
print 'please wait...system loading...'
#reload(sys)
PostUrl = "http://lib.hrbeu.edu.cn/#"
driver=webdriver.Firefox()#用浏览器实现访问
#driver = webdriver.PhantomJS(executable_path="phantomjs.exe")#没用浏览器
driver.get(PostUrl)
elem_user = driver.find_element_by_name('number')
elem_psw = driver.find_element_by_name('passwd')
#选择我的图书馆,点击后才能看到输入账号密码
click_first = driver.find_element_by_xpath("//ul[@id='imgmenu']/li[4]")
click_first.click()
elem_user.send_keys('S315080092')
elem_psw.send_keys('xxxxxxxxx')
#点击登录
click_second = driver.find_element_by_name('submit')
click_second.click()
print 'log in...'
time.sleep(1)
#定位新页面元素,将handle重定位即可
driver.switch_to_window(driver.window_handles[1])#定位弹出的第一个页面,也就是当前页面
#sreach_window = driver.current_window_handle  #此行代码用来定位当前页面#不可行
driver.find_element_by_xpath("/html/body/div[4]/div/div/ul/li[3]/a").click()
driver.save_screenshot('image_booklist_firefox.jpg')
print 'turning to the mylib...'
time.sleep(1)#搜索结果页面停留片刻
#driver.switch_to_window(driver.window_handles[1])
#没有跳出新窗口就是在同一页面的!
for i in range(2,30):#这里限定是29本书,一般我们都不会借那么多书的
    try:
        #driver.find_element_by_xpath("/html/body/div[4]/div/div[2]/table/tbody/%s/td[8]/div/input"%('tr[%s]'%i)).click()#下面的比较好理解
        driver.find_element_by_xpath("/html/body/div[4]/div/div[2]/table/tbody/tr[%s]/td[8]/div/input"%i).click()
        print 'renewing...the %d\'th book renewed '%(i-1)
    except:
        print '%d books have been renewed !'%(i-2)
        a=i-2
        time.sleep(4)
        driver.save_screenshot('image_done_firefox.jpg')
        print 'the picture is saving...'
        print 'done!'
        break
time.sleep(1)
driver.close()
driver.quit()

Call the main execution function of the above module (in fact, it is just to encapsulate the above module, encapsulated into a gui interface, to prepare for subsequent packaging)

 -*- coding: utf-8 -*-
from Tkinter import *
import tkMessageBox#执行gui窗
import time
def check_renew():
    print 'checking and renewing...'
    tkMessageBox.showinfo('提示','即将开启装逼模式,请确认已安装Firefox浏览器')
    #time.sleep(4)
    import Selenium_PhantomJS_lib_firefox
    tkMessageBox.showinfo('提示','已执行成功!\n(截图已保存于程序目录)')
#主框架部分
root = Tk()
root.title('图书馆查询续约)
label=Label(root,text='   图书馆一键查询与续约Firefox版本 (✪ω✪)  ')
button_check=Button(root,text='查询书单并续期━Σ(゚Д゚|||)━开启Firefox有形装逼模式 ',background='green',command=check_renew)
label.pack()
button_check.pack()
root.mainloop()

The effect is shown in the figure:

Insert picture description here

Encountered problems and solutions

Selenium throws
NoSuchElementException: Message: Unable to locate element for the new page element cannot be located

Errors, making it impossible to perform click operations on the new interface.

Solution: I wrote a blog specifically, please see

Solve the problem of Selenium popping up a new page that cannot locate element (Unable to locate element)

The packaged version cannot be run, and an error as shown in Errno 10054 is thrown.
Insert picture description here
Solution: No solution is found, exe file is not available, program execution is available

Repeat the click operation on the unknown number of books, the code redundancy
solution: because each element of the click renew button is different, you can see the law through observation, and then you know to modify there, but if you just modify it, ten books The book has ten similar code strings, which are not pythontic. Therefore, the format string is used to carry in the for loop, which is convenient and beautiful!

I still can’t locate the element after using the solution in 1. There
may be an error in the way to find the element. My current method of use is to use the xpath method to find it, such as this

driver.find_element_by_xpath("/html/body/div[4]/div/div/ul/li[3]/a")

Although it looks a bit long, the elements are quite easy to find, and the positioning is very accurate. If you use a
driver.find_element_by_xpath("//ul[@id='imgmenu']/li[4]"), I still can’t It's well controlled, the possibility of making mistakes is a little bit high, so you have to try more next time.

Next, realize the idea of ​​the second scheme:
call PhantomJS.exe, without showing the operation of the browser, run directly in the cmd window (the cmd window will be packaged into an exe with pyinstaller)

Code

#-*- coding: utf-8 -*-
from selenium import webdriver
import time
import sys
from PIL import Image
#shift-tab多行缩进(左)
print 'please wait...system loading...'
reload(sys)
PostUrl = "http://lib.hrbeu.edu.cn/#"
driver = webdriver.PhantomJS(executable_path="phantomjs.exe")#没用浏览器
driver.get(PostUrl)
elem_user = driver.find_element_by_name('number')
elem_psw = driver.find_element_by_name('passwd')
#选择我的图书馆,点击后才能看到输入账号密码
click_first = driver.find_element_by_xpath("//ul[@id='imgmenu']/li[4]")
click_first.click()
elem_user.send_keys('S315080092')
elem_psw.send_keys('xxxxxxxx')
#点击登录
click_second = driver.find_element_by_name('submit')
click_second.click()
print 'log in...'
time.sleep(1)
#定位新页面元素,将handle重定位即可
driver.switch_to_window(driver.window_handles[1])#定位弹出的第一个页面,也就是当前页面
driver.find_element_by_xpath("/html/body/div[4]/div/div/ul/li[3]/a").click()
driver.save_screenshot('image_booklist.jpg')
print 'turning to the mylib...'
time.sleep(1)#搜索结果页面停留片刻
#driver.switch_to_window(driver.window_handles[1])
#没有跳出新窗口就是在同一页面的!
for i in range(2,30):#这里限定是29本书,一般我们都不会借那么多书的
    try:
        driver.find_element_by_xpath("/html/body/div[4]/div/div[2]/table/tbody/%s/td[8]/div/input"%('tr[%s]'%i)).click()
        print 'renewing...the %d\'th book renewed '%(i-1)
    except:
        print '%d books have been renewed !'%(i-2)
        a=i-2
        time.sleep(4)
        driver.save_screenshot('image_done.jpg')
        print 'the picture is opening...please wait...'
        break
time.sleep(2)
driver.close()
driver.quit()
def show_img():
    im_check=Image.open('image_booklist.jpg')
    im_check.show()
    im_done =Image.open('image_done.jpg')
    im_done.show()

Then the program entry

*# -*- coding: utf-8 -*-*
from Tkinter import *
import tkMessageBox
def check_renew():
    print 'checking and renewing...'
    tkMessageBox.showinfo('提示','执行速度取决于网速和电脑,能等着就按"确定"\n(请允许phantomjs.exe访问网络)\nBTW 你现在按啥都不好使,程序照样执行(*゜Д゜)σ凸')
    from Selenium_PhantomJS_lib import show_img
    show_img()#show一下预约前和预约后截图,好确认
    tkMessageBox.showinfo('提示','已执行成功!\n(若没有弹出图片则请自行打开程序目录)')
#主框架部分
root = Tk()
root.title('图书馆查询续约)--by 哈士奇说喵')
label=Label(root,text='   图书馆一键查询与续约cmd版本 (✪ω✪)  ')
button_check=Button(root,text='查询书单并续期━Σ(゚Д゚|||)━开启cmd无形装逼模式 ',background='green',command=check_renew)
label.pack()
button_check.pack()
root.mainloop()

Encountered problems and solutions

Cannot find the executable file, phantomjs.exe
solution: add phantomjs.exe to the working path, the most convenient way is, where your project is, just add it directly to the project folder.

The screenshot of the picture is not displayed, or it prompts "This application cannot be activated when UAC is disabled".
Solution: Whether the picture is displayed, you can see if the show method is called. If it is called, there is no problem in testing on your own computer. I encountered UAC problems when testing other computers, just enable it directly, generally there is no problem, if you don’t want to trouble start, then go directly to the working folder to open it manually, the screenshot has been saved in the local working path of.

Original link: https://www.write-bug.com/article/2448.html
(slightly modified, infringement contact deleted)
Original author homepage: https://www.write-bug.com/member/50.html

Guess you like

Origin blog.csdn.net/chenglilich/article/details/106856703