我正在尝试从电子商务时尚网站获取一些数据。当我在网站上打开一个类别时,我最终可以看到48个项目和一个加载更多按钮。当我点击该按钮时,我会看到接下来的48个项目。反手发生的是,当我点击加载更多按钮时,会发送XHR发布请求并以JSON格式返回响应。我想自动执行此搜索并在Excel工作表中捕获响应数据。我是编程新手,不熟悉高级脚本语言,所以我正在开发VBA。我的请求已提交但未收到回复。我的类别页面为http://www.myntra.com/_,请求发送的链接为http://www.myntra.com/searchws/search/styleids2。 这是我的代码:
Sub post()
Dim objHTTP As Object
Dim result As String
Set objIE = CreateObject("InternetExplorer.Application")
objIE.navigate "about:blank"
objIE.Visible = True
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
URL = "http://www.myntra.com/searchws/search/styleids2"
objHTTP.Open "POST", URL, False
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
objHTTP.send ("query=(full_text_myntra:(_)AND(statusid:1008))&start=0&rows=500&facet=true&facetField=[]")
result = objHTTP.responsetext
objIE.document.Write result
Set objHTTP = Nothing
End Sub
答案 0 :(得分:1)
尝试使用Postman运行查询时出现了大量415错误,看起来API期待JSON而不是form-urlencoded,并且需要将参数包装在数组中以便我检查在你的代码中:
"[{""query"":""(full_text_myntra:(_)AND(statusid:1008))"",""start"":0,""rows"":500,""facet"":true,""facetField"":[]}]"
此外,我建议使用像Excel-REST这样的东西(我为此案例中所做的那样),以帮助创建请求和处理JSON:
Dim MyntraClient As New RestClient
MyntraClient.BaseUrl = "http://www.myntra.com/"
' With inline JSON
Dim json As String
json = "[{""query"":""(full_text_myntra:(_)AND(statusid:1008))"",""start"":0,""rows"":500,""facet"":true,""facetField"":[]}]"
Dim Response As RestResponse
Set Response = MyntraClient.PostJSON("searchws/search/styleids2", json)
' It's no fun creating json string by hand, instead create it via Dictionary/Collection/Array
Dim SearchParameters As New Dictionary
SearchParameters.Add "query", "(full_text_myntra:(_)AND(statusid:1008))"
SearchParameters.Add "start", 0
SearchParameters.Add "rows", 500
SearchParameters.Add "facet", True
SearchParameters.Add "facetField", Array()
Set Response = MyntraClient.PostJSON("searchws/search/styleids2", Array(SearchParameters))
' Check status, received content, or do something with the data directly
Debug.Print Response.StatusCode
Debug.Print Response.Content
Debug.Print Response.Data("response1")("totalProductsCount")