Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 加速vba刮片的方法(POST请求)_Json_Vba_Web Scraping_Xmlhttprequest - Fatal编程技术网

Json 加速vba刮片的方法(POST请求)

Json 加速vba刮片的方法(POST请求),json,vba,web-scraping,xmlhttprequest,Json,Vba,Web Scraping,Xmlhttprequest,我使用vba和JSON解析器库制作了一个web scraper。 我的代码如下所示,运行良好 Sub Setcustoms() Dim JSON As Object Dim ws As Worksheet, results(), i As Long, s As String Dim shipvalue As String, custom As String, MyURL As String Dim BL As String, returnshipvalue As String Dim a, b

我使用vba和JSON解析器库制作了一个web scraper。 我的代码如下所示,运行良好

Sub Setcustoms()
Dim JSON As Object
Dim ws As Worksheet, results(), i As Long, s As String
Dim shipvalue As String, custom As String, MyURL As String
Dim BL As String, returnshipvalue As String
Dim a, b As Variant
Dim mytext, finaltext As String
Dim myvalue As Object
Dim country() As String
Dim year As String



country = Split("NL,DE,MY,US,VN,UA,ID,JP,CN,CL,CA,TH,PL,RU,PH", ",")
'country = Split("MY,VN", ",")
Dim port() As String


port = Split("KRKAN,KRKUV,KRTSN,KRPUS,KRYMH,KRINC,KRPTK,KRKPO,KRKCN,KRBNP,KRUSN", ",")
'port = Split("KRKAN,KRKUV", ",")


Application.ScreenUpdating = False


i = 2
For Each a In country
    For Each b In port
        With CreateObject("MSXML2.XMLHTTP")
            .Open "POST", "https://www.bandtrass.or.kr/customs/total.do", False
            .setRequestHeader "Accept", "application/json, text/javascript, */*; q=0.01"
            .setRequestHeader "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36"
            .setRequestHeader "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"
            .setRequestHeader "Accept-Encoding", "gzip, deflate, br"
            .send "SELECT_DIV1=PORT_DIV&GODS_TYPE=H&ECONO_TYPE=undefined&PORT_TYPE=B&LOCATION_TYPE=undefined&FILTER1_GODS_UNIT=&SELECT_DIV2=NATN_DIV&FILTER2_GODS_UNIT=&SELECT_DIV3=GODS_DIV&FILTER3_GODS_UNIT=10&POP_TABLE=&COL_NAME=&EXCEL_LOG=&MENU_CODE=CUS00301_POP&EXCEL_SUBJECT=&SelectCd3=4401310000" + _
            "&SelectCd1=" & b & "&SelectCd2=" & a
            
            mytext = Right(.responseText, Len(.responseText) - 44)
            finaltext = Replace(Left(mytext, Len(mytext) - 2), "\", "")
            Set JSON = JsonConverter.ParseJson(finaltext)
            For Each myvalue In JSON
                If Len(myvalue("BASE_DATE")) = 5 Then
                    year = myvalue("BASE_DATE")
                Else
                    If myvalue("IM_WGHT") <> "" Then
                        Cells(i, 2).Value = DateSerial(CInt(Left(year, 4)), CInt(Left(LTrim(myvalue("BASE_DATE")), 2)) + 1, 0)
                        Cells(i, 3).Value = a
                        Cells(i, 4).Value = b
                        Cells(i, 5).Value = myvalue("IM_WGHT") / 1000
                        If myvalue("IM_WGHT") <> 0 Then
                            Cells(i, 6).Value = myvalue("IM_AMT") * 1000 / myvalue("IM_WGHT")
                            i = i + 1
                        Else
                        End If
                    Else
                    End If
                End If
            Next
        End With
    Next
Next
Application.ScreenUpdating = True
Sub-Setcustoms()
Dim JSON作为对象
将ws设置为工作表,结果(),i设置为长,s设置为字符串
Dim shipvalue作为字符串,custom作为字符串,MyURL作为字符串
Dim BL作为字符串,returnshipvalue作为字符串
尺寸a、b作为变型
Dim mytext,finaltext作为字符串
将myvalue设置为对象
Dim country()作为字符串
朦胧的一年
国家=分割(“NL、DE、MY、US、VN、UA、ID、JP、CN、CL、CA、TH、PL、RU、PH”、“,”)
'country=Split('MY,VN',','))
将端口()设置为字符串
端口=拆分(“KRKAN、KRKUV、KRTSN、KRPUS、KRYMH、KRINC、KRPTK、KRKPO、KRKCN、KRBNP、KRUSN”、“,”)
'端口=拆分(“KRKAN,KRKUV”,“,”)
Application.ScreenUpdating=False
i=2
每一个国家
对于端口中的每个b
使用CreateObject(“MSXML2.XMLHTTP”)
.打开“POST”https://www.bandtrass.or.kr/customs/total.do”“错
.setRequestHeader“接受”,“应用程序/json,文本/javascript,*/*;q=0.01”
.setRequestHeader“用户代理”、“Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,类似Gecko)Chrome/87.0.4280.66 Safari/537.36”
.setRequestHeader“内容类型”,“应用程序/x-www-form-urlencoded;字符集=UTF-8”
.setRequestHeader“接受编码”、“gzip、deflate、br”
.send“SELECT\u DIV1=PORT\u DIV&goods\u TYPE=H&ECONO\u TYPE=undefined&PORT\u TYPE=B&LOCATION\u TYPE=undefined&FILTER1\u goods\u UNIT=&SELECT\u DIV2=NATN\u DIV&FILTER2\u goods\u UNIT=&SELECT\u DIV3=goods\u goods\u UNIT=10&POP\u TABLE=&COL\u NAME=&EXCEL\u LOG=&MENU-CODE=CUS00301\u-POP&EXCEL\EXCEL主题=&cd3=1310000”+_
“&SelectCd1=“&b&”&SelectCd2=“&a
mytext=右(.responseText,Len(.responseText)-44)
finaltext=Replace(左(mytext,左(mytext)-2),“\”,“”)
设置JSON=JsonConverter.ParseJson(finaltext)
对于JSON中的每个myvalue
如果Len(myvalue(“基准日期”)=5,则
年份=我的价值(“基准日”)
其他的
如果myvalue(“IM_WGHT”)“那么
单元格(i,2).Value=DateSerial(CInt(左(年,4)),CInt(左(LTrim(myvalue(“基准日期”),2))+1,0)
单元格(i,3)。值=a
单元格(i,4)。值=b
单元格(i,5).Value=myvalue(“IM_WGHT”)/1000
如果myvalue(“IM_WGHT”)为0,则
单元格(i,6)。Value=myvalue(“IM_AMT”)*1000/myvalue(“IM_WGHT”)
i=i+1
其他的
如果结束
其他的
如果结束
如果结束
下一个
以
下一个
下一个
Application.ScreenUpdating=True
端接头

我唯一的问题是,刮板大约需要10分钟才能完成。 我真的很想加快这个过程,因为我将每月更新数据。 另一个可行的选择是,我可以勉强度过最近几个月,但在这种情况下,我必须重写整个代码

有没有什么可能的方法来加速这个过程?
谢谢。

如果没有对代码进行精确测量,我想大部分时间都花在Post通话中。可以肯定的是,只需输入日志,测量Post调用所需的时间。如何解决这个问题,也许可以进行几个并行的Post调用?@glick你能在VBA中做到吗?@MatteoNNZ yes应该是可能的,例如@MatteoNNZ和@QHarr这似乎是C函数的工作。VBA调用C#函数并同步等待它,C#函数将异步收集结果,准备就绪后将返回VBA结果列表。这将避免黑客攻击(从不信任的黑客攻击),并允许更好的调试和维护。我说的是C#,但我想它可以是VBA可以调用的任何东西。