由于需要抓起淘宝/天猫 搜索结果的120个宝贝的平均价格,使用qtp来实现。
考虑到可以使用html
考虑到可以使用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
再对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