您的位置:首页 > 编程语言 > Qt开发

qtp 实战 应用 如何使用 QTP 11.5 UFT 获取淘宝商品价格与数量

2015-03-12 14:52 706 查看
由于需要抓起淘宝/天猫 搜索结果的120个宝贝的平均价格,使用qtp来实现。





考虑到可以使用html
dom 来获取价格和数量元素的属性值。借助firebug查看价格和数量的属性:





再对textcontent 进行字符串处理就可以获取价格了,同理可以获取销售数量的值:

代码如下:

On error resume next

'

''Browser("天猫tmall.com-尚天猫,就购了").Page("天猫tmall.com-尚天猫,就购了").WebEdit("q").Set"鱼嘴鞋" @@ hightlightid_;_Browser("天猫tmall.com-尚天猫,就购了").Page("天猫tmall.com-尚天猫,就购了").WebEdit("q")_;_scriptinfofile_;_ZIP::ssf8.xml_;_

''Browser("天猫tmall.com-尚天猫,就购了").Page("天猫tmall.com-尚天猫,就购了").WebButton("搜索").Click@@ hightlightid_;_Browser("天猫tmall.com-尚天猫,就购了").Page("天猫tmall.com-尚天猫,就购了").WebButton("搜索")_;_scriptinfofile_;_ZIP::ssf9.xml_;_

'Browser("天猫tmall.com-尚天猫,就购了").Page("鱼嘴鞋-时尚女鞋-天猫Tmall.com-尚天猫,就购了").Link("人气").Click@@ hightlightid_;_Browser("天猫tmall.com-尚天猫,就购了").Page("鱼嘴鞋-时尚女鞋-天猫Tmall.com-尚天猫,就购了").Link("人气")_;_scriptinfofile_;_ZIP::ssf10.xml_;_

'Browser("天猫tmall.com-尚天猫,就购了").Page("鱼嘴鞋-时尚女鞋-天猫Tmall.com-尚天猫,就购了_2").Link("下一页>>").Click@@ hightlightid_;_Browser("天猫tmall.com-尚天猫,就购了").Page("鱼嘴鞋-时尚女鞋-天猫Tmall.com-尚天猫,就购了2").Link("下一页>>")_;_scriptinfofile_;_ZIP::ssf11.xml_;_

iteration = environment.value("TestIteration")

n = cint(iteration)

If n=7 Then

reporter.ReportEventmicWarning, "stop","Please stop"

End If

If n = 1 Then



Dim Conn,str,sql,sum,adoRecordsset

Set outsheet = Datatable.AddSheet ("Output")'add
a sheet to inport the datawhich import fromthe DataBase

Set Conn = createObject ("ADODB.Connection")

Const ConnectionString = "DSN=@@@@;DATABASE=@@@@@@database;PWD=@@@@@;PORT=3306;SERVER=@@@@@@.com;UID=@@@@@@"

Conn.Open ConnectionString

If Conn.State <> 0 Then

reporter.ReportEvent micPass, "link state","ConnectSuccess!!"

Else

reporter.ReportEvent micFail, "link state","Connect Failed!!"

End If

sql = "SELECT keyword,tmall_price,date from @@@@@tablewhere keyword in ('女裤','女裤 长裤','女裤裙','哥弟女裤','哈伦女裤','真丝女裤','雪纺女裤')"

Set adoRecordsset = Conn.execute(sql)

Dim fld

For each fld in adoRecordsset.Fields

datatable.GetSheet("Output").AddParameterfld.name,""

Next

Dim i

i = 0

While not adoRecordsset.EOF

For each fld in adoRecordsset.fields

datatable.value(1,"Output") = adoRecordsset("keyword").value

'atatable.value(2,"Output")= adoRecordsset("taobao_price").value

datatable.value(2,"Output") = adoRecordsset("tmall_price").value

datatable.value(3,"Output") = adoRecordsset("date").value

Next

adoRecordsset.MoveNext

i = i + 1



Datatable.SetCurrentRowi + 1

Wend

Set adoRecordsset = nothing

Conn.Close

Set Conn = nothing

End If

n = cint(iteration)

k = 3*n - 2

j = 3*n - 1

m = 3*n

Datatable.SetCurrentRow n

Private Function get_Num(a)

Dim b

b = Left(a,len(a)-1)

c = right(b,len(b)-4)

'Printc

get_Num = c

End Function

Browser("天猫tmall.com-尚天猫,就购了").Page("天猫tmall.com-尚天猫,就购了").WebEdit("q").Set DataTable("keyword", "Output")

inputKW = Browser("天猫tmall.com-尚天猫,就购了").Page("天猫tmall.com-尚天猫,就购了").WebEdit("q").GetROProperty("value")

Browser("天猫tmall.com-尚天猫,就购了").Page("天猫tmall.com-尚天猫,就购了").WebButton("搜索").Click

Browser("天猫tmall.com-尚天猫,就购了").Page("鱼嘴鞋-时尚女鞋-天猫Tmall.com-尚天猫,就购了").Link("人气").Click

'iteration =environment("TestIteration")

k = 3*n-2

j = 3*n-1

m = 3*n

Datatable.GetSheet("Action1").AddParameter "Price","0"

Datatable.GetSheet("Action1").AddParameter "SaleNum","0"

Datatable.GetSheet("Action1").AddParameter "Total","0"

'SetoDoc2=Browser("天猫tmall.com-尚天猫,就购了").Page("假发-假发/美容工具-天猫Tmall.com-尚天猫,就购了").object.getElementsByTagName_r("em")

'

''msgboxoDoc2.length

'

'For x = 0 TooDoc2.length -1 Step 1

'

'

' text=oDoc2(x).getattribute("title")

' Iftext <>"" Then

' z=int(x/2+1)

' Datatable.Value("Price","Action1")=text

' Datatable.SetCurrentRowz

'

' EndIf

'' 'msgboxprice

''printprice

'Next

Set oDoc = Browser("天猫tmall.com-尚天猫,就购了").Page("鱼嘴鞋-时尚女鞋-天猫Tmall.com-尚天猫,就购了").object.getElementsByClassName("productStatus")

Set oDoc2 = Browser("天猫tmall.com-尚天猫,就购了").Page("鱼嘴鞋-时尚女鞋-天猫Tmall.com-尚天猫,就购了").object.getElementsByClassName("productPrice")

fstNum = oDoc.length

'msgboxoDoc.lengthget

For i = 0 To oDoc.length-1 Step 1

Datatable.SetCurrentRow i+1

set p = oDoc(i).firstElementChild

pp = p.textContent



pNum = get_num(pp) 'getthe data of the sale numbers







text = oDoc2(i).textContent

price = split(text,"¥")

rop = price(1)

pop = left(rop,len(rop)-2) 'get
the data of theprice



If instr(pNum,"万") Then

pNumsub = left(pNum,len(pNum)-1)

pNum = pNumsub*10000

End If



sumMoney = pop*pNum





Datatable.Value(k,"Action1") = pop

Datatable.Value(j,"Action1") = pNum

Datatable.Value(m,"Action1") = sumMoney

Next

Browser("天猫tmall.com-尚天猫,就购了").Page("鱼嘴鞋-时尚女鞋-天猫Tmall.com-尚天猫,就购了").Link("xpath:=/html/body/div[1]/div/div[3]/div/div[4]/p/a").Click 'clickthe
link of “下一页” go to the next page

'Browser("天猫tmall.com-尚天猫,就购了").Page("假发-假发/美容工具-天猫Tmall.com-尚天猫,就购了").Link("下一页>>").Click

'

'Browser("天猫tmall.com-尚天猫,就购了").Page("高跟凉鞋 女-时尚女鞋-天猫Tmall.com-尚天猫,就").Link("人气").Click

Set oDoc3 = Browser("天猫tmall.com-尚天猫,就购了").Page("鱼嘴鞋-时尚女鞋-天猫Tmall.com-尚天猫,就购了").object.getElementsByClassName("productStatus")

Set oDoc4 = Browser("天猫tmall.com-尚天猫,就购了").Page("鱼嘴鞋-时尚女鞋-天猫Tmall.com-尚天猫,就购了").object.getElementsByClassName("productPrice")

'msgboxoDoc.lengthget

sndNum = oDoc3.length

proNumber = fstNum + sndNum

For i = 0 To oDoc3.length-1 Step 1

Datatable.SetCurrentRow i+61

set p = oDoc3(i).firstElementChild

pp = p.textContent



pNum = get_num(pp)



text = oDoc4(i).textContent

price = split(text,"¥")

rop = price(1)

pop = left(rop,len(rop)-2)



If instr(pNum,"万") Then

pNumsub = left(pNum,len(pNum)-1)

pNum = pNumsub*10000

End If



sum = pop*pNum



Datatable.Value(k,"Action1") = pop

Datatable.Value(j,"Action1") = pNum

Datatable.Value(m,"Action1") = sum

Next

TotalNum = 0

TotalMoney = 0

For i = 1 To proNumber Step 1

rowNum = Datatable.GetSheet("Action1").GetParameter(j).ValueByRow (i)

rowMoney = Datatable.GetSheet("Action1").GetParameter(m).ValueByRow (i)



TotalNum = TotalNum + rowNum

TotalMoney = TotalMoney + rowMoney



Next

Datatable.SetCurrentRow 122+2*n-1

Datatable.Value (1,"Action1") = inputKW

Datatable.Value (2,"Action1") = TotalNum

Datatable.Value (3,"Action1") = TotalMoney

avgPrice = TotalMoney/TotalNum

Datatable.SetCurrentRow 122+ 2*n

Datatable.Value (2,"Action1") = "TmallAvgPrice"

Datatable.Value (3,"Action1") = avgPrice

'下面的代码为淘宝页面商品的价格与数理的抓取

'

'Browser("天猫tmall.com-尚天猫,就购了").Page("假发-假发/美容工具-天猫Tmall.com-尚天猫,就购了_2").Link(">").Click@@ hightlightid_;_Browser("天猫tmall.com-尚天猫,就购了").Page("假发-假发/美容工具-天猫Tmall.com-尚天猫,就购了2").Link(">")_;_script infofile_;_ZIP::ssf4.xml_;_



' Browser("淘宝网 -淘!我喜欢").Page("淘宝网 - 淘!我喜欢").WebEdit("q").Set DataTable("keyWord",dtGlobalSheet)

'

' inputword=Browser("淘宝网- 淘!我喜欢").Page("淘宝网 -淘!我喜欢").WebEdit("q").GetROProperty("value")

' ' msgboxinputword

'Browser("淘宝网 -淘!我喜欢").Page("淘宝网 - 淘!我喜欢").WebButton("").Click

'

'iteration=environment.value("TestIteration")

' n=cint(iteration)

' k=3*n-2

' j=3*n-1

' m=3*n

'' KeyWord=DataTable.GetSheet("Global").GetParameter("keyWord").ValueByRow(n)

'' msgboxKeyWord

'Browser("淘宝网 -淘!我喜欢").Page("鱼嘴鞋_淘宝搜索").Link("人气").Click

'Browser("淘宝网 -淘!我喜欢").Sync

'' wait5

'SetDoc=Browser("淘宝网 -淘!我喜欢").Page("鱼嘴鞋_淘宝搜索").Object.getElementsByClassName("colprice")

'SetDoc2=Browser("淘宝网 -淘!我喜欢").Page("鱼嘴鞋_淘宝搜索").Object.getElementsByClassName("col enddealing")

''msgboxDoc2.length

''setprice=Doc.getElementsByClassName("col price")

''numberofprice=price.length

''msgboxnumberofprice

''Setp1=Doc.getElementsByClassName("col price")(0)

'

'datatable.getsheet("Action1").AddParameter"Price","0"

'datatable.getsheet("Action1").AddParameter"SaleNum","0"

'datatable.getsheet("Action1").AddParameterinputword,"0"

' For i=0 toDoc.length -1

'

' price=Doc(i).textContent

' num=Doc2(i).textContent

' 'print price

'

' a=mid(price,2)

' b=mid(num,1,len(num)-3)

'

' sum=a*b

'

' DataTable.GetSheet("Action1").SetCurrentRow i+1

'

' DataTable.Value(k, "Action1")=a

' DataTable.Value(j, "Action1")=b

' DataTable.Value(m, "Action1")=sum

'' ah=DataTable.GetSheet("Action1").GetParameter("Price").Value

'

''

'

'

'

'Next

'

'

'

'

'Browser("淘宝网 -淘!我喜欢").Page("鱼嘴鞋_淘宝搜索#J_relative").Link("下一页下一页").Click

'

'Browser("淘宝网 -淘!我喜欢").Sync

'

'SetDoc3=Browser("淘宝网 -淘!我喜欢").Page("鱼嘴鞋_淘宝搜索#J_relative").Object.getElementsByClassName("colprice")

'SetDoc4=Browser("淘宝网 -淘!我喜欢").Page("鱼嘴鞋_淘宝搜索#J_relative").Object.getElementsByClassName("colend dealing")

''msgboxDoc2.length

''setprice=Doc.getElementsByClassName("col price")

''numberofprice=price.length

''msgboxnumberofprice

''Setp1=Doc.getElementsByClassName("col price")(0)

'

''datatable.getsheet("Action1").AddParameter"Price2","0"

''datatable.getsheet("Action1").AddParameter"SaleNum2","0"

' For i=0 toDoc3.length -1

' DataTable.GetSheet("Action1").SetCurrentRow i+49

' price=Doc3(i).textContent

' num=Doc4(i).textContent

' 'print price

' a=mid(price,2)

'

'

' b=mid(num,1,len(num)-3)

'

'

' sum=a*b

'

' DataTable.Value(k, "Action1")=a

' DataTable.Value(j, "Action1")=b

' DataTable.Value(m, "Action1")=sum

'' ah=DataTable.GetSheet("Action1").GetParameter("Price").Value

'

''

'

'

'

'Next

'

'

''Browser("淘宝网 -淘!我喜欢").Page("鱼嘴鞋_淘宝搜索#J_relative").Link("下一页下一页").Click

'

'

'Browser("淘宝网 -淘!我喜欢").Page("鱼嘴鞋_淘宝搜索_4").Link("下一页下一页").Click

'Browser("淘宝网 -淘!我喜欢").Sync

'SetDoc5=Browser("淘宝网 -淘!我喜欢").Page("鱼嘴鞋_淘宝搜索#J_relative").Object.getElementsByClassName("colprice")

'SetDoc6=Browser("淘宝网 -淘!我喜欢").Page("鱼嘴鞋_淘宝搜索#J_relative").Object.getElementsByClassName("colend dealing")

''msgboxDoc2.length

''setprice=Doc.getElementsByClassName("col price")

''numberofprice=price.length

''msgboxnumberofprice

''Setp1=Doc.getElementsByClassName("col price")(0)

'

''datatable.getsheet("Action1").AddParameter"Price2","0"

''datatable.getsheet("Action1").AddParameter"SaleNum2","0"

' For i=0 toDoc5.length -17

' DataTable.GetSheet("Action1").SetCurrentRow i+93

' price=Doc5(i).textContent

' num=Doc6(i).textContent

' 'print price

' a=mid(price,2)

'

'

' b=mid(num,1,len(num)-3)

' sum=a*b

'

' DataTable.Value(k, "Action1")=a

' DataTable.Value(j, "Action1")=b

' DataTable.Value(m, "Action1")=sum

'' ah=DataTable.GetSheet("Action1").GetParameter("Price").Value

'next

'

'allSum=0

'allNum=0

'For r = 1 To 120Step 1

'

'sum_money=datatable.GetSheet("Action1").GetParameter(m).ValueByRow (r)

'sum_num=datatable.GetSheet("Action1").GetParameter(j).ValueByRow (r)

'allNum=allNum+sum_num

'allSum=allSum+sum_money

'

'avg_price=allSum/allNum

'

'datatable.GetSheet("Action1").SetCurrentRow123

'datatable.Value(m,"Action1")=allSum

'datatable.Value(j,"Action1")=allNum

'datatable.Value(k,"Action1")=inputword

'

'

'datatable.GetSheet("Action1").Setcurrentrow124

'datatable.Value(m,"Action1")=avg_price

'

'Next

'

'If n mod 7=0Then

' browser("淘宝网- 淘!我喜欢").CloseAllTabs

' SystemUtil.CloseProcessByName"iexplore.exe"

'

'EndIf

'

运行的结果:













转载:http://blog.sina.com.cn/s/blog_6c1a34300102v12s.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: