ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 오토핫키] 웹 통합 크롤링 Crawling.ahk / Winhttp, xmlHTTP, URLdownloadtoFile, ReadURL() 통합 제공
    오토핫키/프로그램 배포 2022. 11. 10. 02:42

    https://apsalman.tistory.com/75


    https://www.youtube.com/channel/UC6hW-NrL9CxDQd2IzWbekYA


    https://apsalman.tistory.com/41

     

    오토핫키 연습일지 #2 ] 웹 간편 크롤링

    https://open.kakao.com/me/apsalman https://www.youtube.com/channel/UC6hW-NrL9CxDQd2IzWbekYA 연습 #2 오토핫키를 이용해서 크롤링을 하는 방법은 정말 여러가지가 있다. 그 중에서 오토핫키 내장으로 지원하는 명령어 U

    apsalman.tistory.com

     

    6월을 시작으로 WinHttp 에 관심을 가지게 되었고 그 열정은 지금까지 이어져왔었다.

     

    그 과정에

    Winhttp, URLDownloadtoFile, ReadURL() 3가지의 크롤링 방식을 한 곳에서 테스트 해볼 수 있도록 만들어진

    '웹 간편 크롤링' 이라는 프로그램을 만들었었다.

    22년 7월에 완성된 웹 통합 크롤링. 다음카페 크롤링 때문에 고민하던 순간

    물론 배포는 하지 않았다. 연습단계이기도 했고 배포할 수준은 아니었기 때문이다.

     

     

    그 사이에 필요하신 분들도 몇분 계셔서 드리긴 했다.

    시간도 꽤 지났고 이제는 배포할 수준은 되지 않을까? 해서 배포를 시작한다.

    xmlHTTP 크롤링도 추가하였다.

    ; 압살맨 티스토리 apsalman.tistory.com
    CoordMode, Mouse, Screen
    
    #NoTrayIcon
    Global wh := ComObjCreate("WinHTTP.WinHTTPRequest.5.1") ;WinHTTP 오브젝트 생성
    	  ,req := ComObjCreate("MSXML2.XMLHTTP.6.0") ;XMLHTTP 오브젝트 생성
    ;----------------------------------Gui 영역----------------------------------
    Gui, Add, Text, x10 y11 w70 h20, 사이트 주소
    Gui, Add, Edit, x80 y8.5 w285 h20 vSiteLink, 
    Gui, Add, Button, x370 y7.5 w60 h22 gSearch, 검 색
    Gui, Add, Button, x435 y7.5 w60 h22 gSearchHeader, 과 자
    
    Gui, Add, Text, x0 y32 w510 0x10,
    Gui, Add, GroupBox, x5 y40 w90 h40, 제거 옵션
    Gui, Add, CheckBox, x15 y55 w70 h20 vFilterBlank, 공백 제거
    
    Gui, Add, GroupBox, x100 y40 w305 h40, 방식 선택 (미선택은 WinHTTP)
    Gui, Add, Radio, x105 y55 w70 h20 gSetEnc vRadio1, ReadURL
    Gui, Add, Radio, x180 y55 w70 h20 vRadio2, URLdown
    Gui, Add, Radio, x255 y55 w70 h20 gSetEnc vRadio3, WinHTTP
    Gui, Add, Radio, x330 y55 w70 h20 vRadio4, xmlHTTP
    
    Gui, Add, GroupBox, x410 y40 w85 h40, 소요시간
    GUi, Add, Text, x430 y58 w60 h20 vUseTime, 
    
    Gui, Add, Edit, x5 y85 w490 h200 vResultBox, 
    
    Gui, Add, Button, x5 y290 w100 h22 gCopy, 복사
    ;~ Gui, Add, Button, x115 y290 w100 h22 gFind, 검색
    ;~ Gui, Add, Button, x225 y290 w100 h22 gChange, 바꾸기
    
    ;~ Gui, Add, Edit, x5 y320 w100 h20 vBStr
    ;~ Gui, Add, Edit, x115 y320 w100 h20 vAStr
    ;~ Gui, Add, Button, x225 y319 w60 h22 gChangeStr, 바꾸기
    
    Gui, Show, w500 h317, 웹 통합 크롤링
    
    Gui, 2:Margin, 0, 0
    Gui, 2:Add, ComboBox, x0 y0 w80 gEncod vEncoding, UTF-8||UTF-16|cp51949
    Gui, 2:+ToolWindow -Caption -Border
    ;----------------------------------Gui 영역----------------------------------
    return
    
    Search: ;검색 버튼 클릭 시 작동
    StartTime := A_TickCount
    Gui, SubMit, NoHide
    
    SearchType := Radio1?1:Radio2?2:Radio3?3:Radio4?4:4
    
    Result := Filterblank?RegExReplace(RegExReplace(RegExReplace(SearchType=1?ReadURL(SiteLink, Encoding):SearchType=2?UDF(SiteLink):SearchType=4?HTTP("req",SiteLink, Encoding):HTTP("wh",SiteLink, Encoding), "  "), "	"), "`n`n"):SearchType=1?ReadURL(SiteLink, Encoding):SearchType=2?UDF(SiteLink):SearchType=4?HTTP("req",SiteLink, Encoding):HTTP("wh",SiteLink, Encoding)
    
    GuiControl, , ResultBox, % Result
    FinishTime := A_TickCount - StartTime
    GuiControl, , UseTime, % FinishTime "ms"
    return
    
    SearchHeader:
    Gui, Submit, NoHide
    wh.Open("Get", SiteLink)
    wh.Send()
    Result := RegExReplace(Cookie(wh.GetAllResponseHeaders()), "; ", "; `n")
    GuiControl, , ResultBox, % Result
    return
    
    SetEnc:
    MouseGetPos, mx, my
    Gui, 2:Show, % "x" mx-60 " y" my+15
    return
    
    Encod:
    Gui, 2:Submit, NoHide
    Enc := Encoding
    Gui, 2:Hide
    return
    
    Copy: ;크롤링한 내용을 클립보드에 복사
    ClipBoard := Result
    ToolTip, 복사되었습니다.
    Sleep, 1000
    ToolTip
    return
    
    ;----------------------------------미 구현----------------------------------
    ;~ Find: ;크롤링한 내용 안에서 키워드 검색
    ;~ return
    
    ;~ Change: ;크롤링한 내용 안에서 키워드 변경
    ;~ if(StatusChange = "")
    ;~ {
    	;~ StatusChange := 1
    	;~ Gui, Show, w500 h345, 웹 통합 크롤링
    ;~ }
    ;~ else
    ;~ {
    	;~ StatusChange := ""
    	;~ Gui, Show, w500 h317, 웹 통합 크롤링
    ;~ }
    ;~ return
    
    ;~ ChangeStr:
    ;~ Gui, Submit, NoHide
    ;~ StringReplace, ResultChange, Result, BStr, AStr
    ;~ GuiControl, , ResultBox, %ResultChange%
    ;~ return
    ;----------------------------------미 구현----------------------------------
    
    GuiClose:
    ExitApp
    
    UDF(URL) {
    	URLDownloadToFile, % URL, Error.txt
    	FileRead, Result, Error.txt
    	FileDelete, Error.txt
    	return Result
    }
    
    HTTP(HttpType, URL, Enc) {
    	%HttpType%.Open("Get", URL)
    	%HttpType%.Send()
    	try {
    		a := %HttpType%.responseText
    	}
    	catch e {
    		body := wh.ResponseBody
    		pdata := NumGet(ComObjValue(body)+8+A_PtrSize)
    		length := body.MaxIndex() - body.MinIndex() + 1
    		a := StrGet(pdata, length, Enc)
    	}
    	return a
    }
    
    ReadURL(URL, encoding = "UTF-8") {
    	static a := "AutoHotkey/" A_AhkVersion
    	if (!DllCall("LoadLibrary", "str", "wininet") || !(h := DllCall("wininet\InternetOpen", "str", a, "uint", 1, "ptr", 0, "ptr", 0, "uint", 0, "ptr")))
    		return 0
    	c := s := 0, o := ""
    	if (f := DllCall("wininet\InternetOpenUrl", "ptr", h, "str", url, "ptr", 0, "uint", 0, "uint", 0x80003000, "ptr", 0, "ptr"))
    	{
    		while (DllCall("wininet\InternetQueryDataAvailable", "ptr", f, "uint*", s, "uint", 0, "ptr", 0) && s > 0)
    		{
    			VarSetCapacity(b, s, 0)
    			DllCall("wininet\InternetReadFile", "ptr", f, "ptr", &b, "uint", s, "uint*", r)
    			o .= StrGet(&b, r >> (encoding = "utf-16" || encoding = "cp1200"), encoding)
    		}
    		DllCall("wininet\InternetCloseHandle", "ptr", f)
    	}
    	DllCall("wininet\InternetCloseHandle", "ptr", h)
    	return o
    }
    
    Cookie(Cookies) {
    	pos := 0
    	Loop, 5 {
    		pos := RegExMatch(Cookies, "Set-Cookie: (.*?)path", cookie, pos+1) + StrLen(cookie)
    		cookieA .= cookie1
    	}
    	cookieA := SubStr(cookieA, 1, strlen(cookieA)-2)
    	return cookieA
    }

    코드 전문이다.

    ahk 다운로드 파일이다.

    웹 통합 크롤링.ahk
    0.00MB

     

    이번에 11월 9일을 기점으로 xmlHTTP 로 크롤링하는 부분도 추가되었다.

    2023년 11월 15일 Winhttp 로 크롤링할 시 발생하는 매니페스트 매핑 오류를 넘길 수 있도록 인코딩 옵션을 추가하였다.

    과자는 쿠키인데 대부분의 사이트에서는 안되는 것 같다.

     

    아무쪼록 많은 사람들이 도움을 받았으면 좋겠다.

     

    그럼 이만

    댓글