python automated scripts

The first python script automation - test page pageid


# -*- coding : utf-8 -*-

__authon__ = 'test'

import requests

import xlrd

from pyquery import PyQuery as pq

from xlutils3 import copy

import os

import time

from AutoScript.tool import get

#import urlparam

import re


# Read Excel

filepath = get.getFilePath ( "\\ AutoScript \\ resources \\ testpageid") + '. xls' # Get Excel, from the address table: testpageid.xls

work_book=xlrd.open_workbook(filepath) #打开Excel

sheetname = work_book.sheet_names () [0] # get the first Excel workbook

sheet=work_book.sheet_by_name(sheetname)    #找到相应sheet

nrows=sheet.nrows


# Obtain data in the table

for i in range(1,nrows):

    date = sheet.row_values ​​(i) # acquired by the data table row

    url = date [0] # get the first column in row

    if url!=None:

        pattern = re.compile (r'pid = (\ d +) ') # url parameters taken beginning with a regular expression represented pid number

        real_pageid=pattern.findall(url)

        pattern2 = re.compile (r'locale = (. +?) & ') # regular rules, find locale = beginning & ending in the middle of the returned string content

        locale = pattern2.findall (url) # Find satisfy the rule string returns to meet the conditions of list

        response=requests.get(url) #请求url

        if response.status_code == 200: # request was successful

            pageid = pq(response.text)('input:last').attr('value')  # 获取pageID

            print(pageid)

            print(real_pageid[0])

        if int (pageid) == int (real_pageid [0]): # required to int, real_pageid [0] represents a list of the target value

            print("%s test result:【Pass】"%locale)

        else:

            print("%s test result:【Fail】" %locale)


 


Import Module Description:

requests: to meet the strong demand of web HTTP library


PyQuery: parsing HTML content, access to need


xlrd: achieve excel file content reading


xlwt: achieve writes excel files


xlutils3: excel copy has to be modified to produce a new excel spreadsheet


re: Regular module


excel Detailed Operation: https: //blog.csdn.net/dreambitbybit/article/details/72353768


Regular match-related operations: https: //blog.csdn.net/three_co/article/details/78494977


https://www.cnblogs.com/dwdw/p/9553192.html


(1): Read Excel:


(2): write Excel


(3): request url


(4): parsing html


(5): regular expression matching


2. Reference py file to another folder


 


from tool import get

An error occurred:


ModuleNotFoundError: No module named 'tool'


 


Modified to: from AutoScript.tool import get


Guess you like

Origin blog.51cto.com/14529380/2455031