Function request(url, Optional requestHeader = "", Optional method = "GET", Optional postData = "", Optional CharSet = "")
On Error GoTo goErr
Set winhttp = CreateObject("WinHttp.WinHttpRequest.5.1")
winhttp.Open method, url, False
If TypeName(requestHeader) = "Variant()" Then
For Each Header In requestHeader
idx = InStr(Header, ":")
If idx > 0 Then
Key = Trim(Left(Header, idx - 1))
Value = Trim(Mid(Header, idx + 1))
If Key <> "" And Value <> "" Then winhttp.SetRequestHeader Key, Value
End If
Next
End If
winhttp.settimeouts 10000, 10000, 10000, 10000 '타임아웃 10초
winhttp.Send postData
request = winhttp.responsetext
Exit Function
goErr:
request = Err.Description
End Function
Function getJson(text)
Set getJson = JsonConverter.ParseJson(text)
End Function
Function getDocument(text)
Set getDocument = CreateObject("htmlfile")
getDocument.body.innerHtml = text
End Function
Function getCookie(url)
Set winhttp = CreateObject("WinHttp.WinHttpRequest.5.1")
winhttp.Open "GET", url, False
winhttp.Send
headers = winhttp.getAllresponseheaders
getCookie = getBetween(headers, "Set-Cookie: ", ";")
End Function
Function getBetween(text, strA, strB) As String
On Error GoTo goErr
aLen = Len(strA)
bLen = Len(strB)
aPos = InStr(text, strA)
bPos = InStr(aPos + aLen, text, strB)
If aPos = 0 Or bPos = 0 Then Exit Function
cPos = aPos + aLen
cLen = bPos - cPos
getBetween = Mid(text, cPos, cLen)
Exit Function
goErr:
End Function
Function encode(str)
Set htmlfile = CreateObject("htmlfile")
htmlfile.parentWindow.execScript "function encode(s) {return encodeURIComponent(s)}", "jscript"
encode = htmlfile.parentWindow.encode(str)
End Function
Function decode(str)
Set htmlfile = CreateObject("htmlfile")
htmlfile.parentWindow.execScript "function decode(s) {return decodeURIComponent(s)}", "jscript"
decode = htmlfile.parentWindow.decode(str)
End Function
Function downloadFile(url, localPath) As String
On Error GoTo goErr
Set winhttp = CreateObject("WinHttp.WinHttpRequest.5.1")
winhttp.Open "GET", url, False
winhttp.Send
Set objStream = CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1
objStream.Write winhttp.responseBody
objStream.SaveToFile localPath, 2
objStream.Close
Exit Function
goErr:
downloadFile = Err.Description
End Function
Function changeFileName(txt)
s = txt
s = Replace(s, "\", "_")
s = Replace(s, "/", "_")
s = Replace(s, ":", "_")
s = Replace(s, "*", "_")
s = Replace(s, "?", "_")
s = Replace(s, """", "_")
s = Replace(s, "<", "_")
s = Replace(s, ">", "_")
s = Replace(s, "|", "_")
changeFileName = s
End Function
Function openFolder(path)
On Error GoTo goErr
Set objShell = CreateObject("shell.application")
objShell.Open path
Exit Function
goErr:
End Function
'크롤링_crawling' 카테고리의 다른 글
크롤링 Json (0) | 2023.04.18 |
---|---|
winhttp 크롤링 vba 기본 code (0) | 2023.04.03 |
selenium 엔카 크롤링 (bot 감지 실행 안됨) (0) | 2023.04.01 |
selenium 셀레니움 기본 크롬오픈 최소화 (0) | 2023.04.01 |
셀레니움 selenium 오류 -2147217392(80041010) 런타임 자동화 오류 (0) | 2023.03.31 |