2016-09-24 5 views
1

장치를 나열하고 해당 장치 옆에 가격을 책정하고 싶습니다.특정 셀에서 Excel 데이터 가져 오기

내 목표는 매주 다른 사이트를 확인하고 추세에 주목하는 것입니다.

이것은 취미 프로젝트입니다. 이미이 작업을 수행하는 사이트가 있습니다. 예를 들어

:

Device | URL Site 1 | Site 1 | URL Site 2 | Site 2 
Device a | http://... | €40,00 | http://... | €45,00 
Device b | http://... | €28,00 | http://... | €30,50 

수동이 많은 작업 (매주 확인)를, 그래서 Excel에서 매크로 도움이 될 생각했다. 문제는 하나의 셀에 데이터를 넣고 테이블 만 인식하는 것입니다. 솔루션 : 소스 코드보기, 가격 읽기, 특정 셀로의 가격 수출.

나는 이것이 Excel 내에서 모두 가능하다고 생각하지만, 가격이나 다른 주어진 데이터를 읽는 방법을 알아 내고 하나의 특정 셀에 넣는 방법을 알아낼 수는 없습니다. 소스 코드에서 좌표를 지정할 수 있습니까? 아니면보다 효과적인 사고 방식이 있습니까?

미리 감사드립니다. 모든 팁과 해결책을 환영합니다!

+0

실제로 코드를 제공하지 않으므로 실제로는 맞지 않습니다. 각 웹 사이트에는 자체 구조가 있으므로 웹 사이트에서 데이터를 가져 오는 방법을 제안하려면 최소한 URL을 알아야합니다. 일반적으로 Excel의 쿼리 가능, IE 자동화 또는 XHR 응답 분석을 통해 웹 사이트에서 데이터를 가져올 수 있습니다. 그런 다음 데이터를 적절한 형식 (예 : 배열)으로 변환하여 워크 시트에 놓아야합니다. – omegastripes

+0

예를 들어 _ [이 사이트] (http://www.mediamarkt.de/de/product/_bosch-wtw-85230-2004975.html) _에서 소스 코드의 78 번째 줄 가격이 있습니다. 도와 주셔서 감사합니다 :) –

+0

당신이 시작 : [이 링크를 클릭하십시오] (http://stackoverflow.com/questions/1820345/perform-http-post-from-within-excel-and-parse-results) – jamheadart

답변

0

먼저 웹 사이트가 어떻게 작동하는지 알아야합니다. page의 경우 다음을 수행했는지 묻습니다.

  • 열린 Chrome에서 http://www.mediamarkt.de 페이지.
  • 검색 창에 입력 된 BOSCH WTW 85230이 제안 목록에 나타납니다.
  • F12 개발자 도구를 열고 네트워크 탭을 클릭하십시오.

general

:

search box

일반 정보를 검사 요청을 클릭 함 : 나는 입력 될 때마다
  • , 새로운 요청 (노란색 영역 참조) 등장

    GET 메소드와 url-encoded prod를 포함한 일부 매개 변수를 사용하는 것을 볼 수 있습니다. uct 이름.

    • 서버에서 반환하는 데이터 조사에 대한 응답 탭을 클릭했습니다 다음과 같이

    response

    당신은 그것을 볼 수는 일반 JSON입니다, 전체 내용은 다음과 같습니다

    {"suggestions":[{"attributes":{"energyefficiencyclass":"A++","modelnumber":"2004975","availabilityindicator":"10","customerrating":"0.00000","ImageUrl":"http://pics.redblue.de/artikelid/DE/2004975/CHECK","collection":"shop","id":"MediaDEdece2358813","currentprice":"444.00","availabilitytext":"Lieferung in 11-12 Werktagen"},"hitCount":0,"image":"http://pics.redblue.de/artikelid/DE/2004975/CHECK","name":"BOSCH WTW 85230 Kondensationstrockner mit Warmepumpentechnologie (8 kg, A++)","priority":9775,"searchParams":"/Search.ff?query=BOSCH+WTW+85230+Kondensationstrockner+mit+W%C3%A4rmepumpentechnologie+%288+kg%2C+A+%2B+%2B+%29\u0026channel=mmdede","type":"productName"}]} 
    

    여기 "currentprice":"444.00" 가격을 찾을 수 있습니다.

    Option Explicit 
    
    Sub TestMediaMarkt() 
    
        Dim oRange As Range 
        Dim aResult() As String 
        Dim i As Long 
        Dim sURL As String 
        Dim sRespText As String 
    
        ' set source range with product names from column A 
        Set oRange = ThisWorkbook.Worksheets(1).Range("A1:A3") 
        ' create one column array the same size 
        ReDim aResult(1 To oRange.Rows.Count, 1 To 1) 
        ' loop rows one by one, make XHR for each product 
        For i = 1 To oRange.Rows.Count 
         ' build up URL 
         sURL = "http://www.mediamarkt.de/FACT-Finder/Suggest.ff?channel=mmdede&query=" & EncodeUriComponent(oRange.Cells(i, 1).Value) 
         ' retrieve HTML content 
         With CreateObject("MSXML2.XMLHTTP") 
          .Open "GET", sURL, False 
          .Send 
          sRespText = .responseText 
         End With 
         ' regular expression for price property 
         With CreateObject("VBScript.RegExp") 
          .Global = True 
          .MultiLine = True 
          .IgnoreCase = True 
          .Pattern = """currentprice""\:""([\d.]+)""" ' capture digits after 'currentprice' in submatch 
          With .Execute(sRespText) 
           If .Count = 0 Then ' no matches, something going wrong 
            aResult(i, 1) = "N/A" 
           Else ' store the price to the array from the submatch 
            aResult(i, 1) = .Item(0).Submatches(0) 
           End If 
          End With 
         End With 
        Next 
        ' output resultion array to column B 
        Output Sheets(1).Range("B1"), aResult 
    
    End Sub 
    
    Function EncodeUriComponent(strText) 
        Static objHtmlfile As Object 
        If objHtmlfile Is Nothing Then 
         Set objHtmlfile = CreateObject("htmlfile") 
         objHtmlfile.parentWindow.execScript "function encode(s) {return encodeURIComponent(s)}", "jscript" 
        End If 
        EncodeUriComponent = objHtmlfile.parentWindow.encode(strText) 
    End Function 
    
    Sub Output(oDstRng As Range, aCells As Variant) 
        With oDstRng 
         .Parent.Select 
         With .Resize(_ 
          UBound(aCells, 1) - LBound(aCells, 1) + 1, _ 
          UBound(aCells, 2) - LBound(aCells, 2) + 1 _ 
         ) 
          .NumberFormat = "@" 
          .Value = aCells 
          .Columns.AutoFit 
         End With 
        End With 
    End Sub 
    
    • 가득 워크 시트 :

    products

    • 하위를 출시하고 결과를 얻었다 : XHR을 통해 웹 사이트에서 데이터를 검색하고 응답을 구문 분석하는 방법을 단지 예입니다

    result

    을 RegExp를 사용하면 도움이되기를 바랍니다.

  • +0

    [이 답변] (http://stackoverflow.com/a/26129999/2165759)도 유용 할 수 있습니다. – omegastripes