python自動化腳本

第一個python自動化腳本-測試頁面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 urlparse

import re


#讀取Excel

filepath=get.getFilePath("\\AutoScript\\resources\\testpageid")+'.xls' #根據地址獲取Excel, 表名:testpageid.xls

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

sheetname=work_book.sheet_names()[0]        #獲取Excel的第一張工作簿

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

nrows=sheet.nrows


#獲取表中數據

for i in range(1,nrows):

    date=sheet.row_values(i)   #按行獲取表中數據

    url=date[0]                #獲取行中的第一列

    if url!=None:

        pattern=re.compile(r'pid=(\d+)')     #截取url參數 正則表達式 表示以pid開頭的數字

        real_pageid=pattern.findall(url)

        pattern2=re.compile(r'locale=(.+?)&')  #正則規則,查找 locale= 開頭,& 結尾的,返回字符串中間內容

        locale=pattern2.findall(url)            #查找滿足規則的字符串,返回的是滿足條件的list

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

        if response.status_code== 200:   #請求成功

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

            print(pageid)

            print(real_pageid[0])

        if int(pageid) ==int (real_pageid[0]):               #需要轉換成int型,real_pageid[0]表示list的第一個下標的值

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

        else:

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


 


導入模塊介紹:

requests:強大的可以滿足web需求的HTTP庫


PyQuery:  解析HTML內容,獲得需要的內容


xlrd:    實現excel文件內容讀取


xlwt:     實現excel文件的寫入


xlutils3:   拷貝已有excel進行修改,生成新的excel表格


re:       正則模塊


excel詳細操作:https://blog.csdn.net/dreambitbybit/article/details/72353768


正則匹配相關操作:https://blog.csdn.net/three_co/article/details/78494977


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


(1):讀取Excel:


(2):寫入Excel


(3):請求url


(4):解析html


(5):正則表達式匹配


2.引用另一文件夾的py文件


 


from tool import get

出現錯誤:


ModuleNotFoundError: No module named 'tool'


 


修改成:from AutoScript.tool import get


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章