Wu Yuxiong - born natural python learning Notes: python automatic page test

Python page testing functionality that can be achieved is very powerful, and can even pass 
through the program is implemented so that the vast majority of the testing process automation. This is open to many 
developers, it is definitely a rare artifact. 
hash lib New members can determine whether a file has been changed, just use the 
md5 method specified file is encoded, can be compared. 
Selenium is a very famous piece of New Zealand web automated testing, it can be 
automatically tested by the page directive. Selenium also enables web developers 
large number of repetitive operation of automated and automatically in the set time straight 
run quite powerful.
Check whether the network update wax data 
usually requires the data stored in a file or database data when crawling your site. Every website reads 
data must take up valuable network resources, in order to improve efficiency, if the site content is not updated, there is no 
reading necessary data again, as long as we read from the last saved a file or a database can a.
Determining whether the file has been updated with hashlib 
Python provides hash lib component determines whether the file can be updated, the easiest way is by m d5 side 
generates binary encoding method specified, as long as the file has been updated, it will have a different md5 code. 
Before using has hlib, you must first import ha sh lib components, and then create an object md5 method. 
For example: Create a md5 object.

 

 

Md5 update method using the created object, encrypts the specified string, wherein the string 
must be a binary type: hexdigest obtained encryption method may result in hexadecimal. The syntax examples are as follows:

 

 

In this manner somewhat cumbersome, relatively simple way is to skip creating md5 objects and update methods, directly 
to the specified character string as a binary hashlib.md5 () parameters, for example:

 

 

Md5 check with the site content is updated 
To achieve this, md5 code we need to save up first website created earlier, and then create a new 
compare md5 code, so you can judge whether the content of the website has been updated. 
The following program, we will Sina new with previous md5 code stored in the <Old md5. Tx t > file, read 
out md5 code to compare the newly created post, after the end, with the latest md5 code to overwrite the original o ld rnd5 .txt 
file. (<MD5. Py >)
Import hashlib, os, Requests 

url = " http://opendata.epa.gov.tw/ws/Data/REWXQA/?$orderby=SiteName&$skip=0&$top=1000&format=json " 

# read the page source 
html requests.get = (URL) .text.encode ( ' UTF--SIG. 8 ' )
 # determines whether to update the page 
MD5 = hashlib.md5 (HTML) .hexdigest () 

iF os.path.exists ( ' F.: \\ pythonBase CH06 pythonex \\ \\ old_md5.txt \\ ' ): 
    with Open ( ' F.: \\ pythonBase pythonex \\ \\ \\ old_md5.txt CH06 ' , ' R & lt ' ) AS F: 
        old_md5 =reached, f.read () 
    with Open ( ' F.: \\ pythonBase pythonex \\ \\ \\ old_md5.txt CH06 ' , ' W ' ) AS F: 
        f.write (MD5) 
the else : 
    with Open ( ' F.: \\ CH06 \\ \\ \\ pythonex pythonBase old_md5.txt ' , ' W ' ) AS F: 
        f.write (MD5) 

IF MD5 =! old_md5:
     Print ( ' data has been updated ... ' ) 
 the else :
     Print ( ' data not updated, read from the database ... ' )

Guess you like

Origin www.cnblogs.com/tszr/p/12024642.html