2017-12-18 4 views
-1

메신저를 사용하고 있지만 그것은 단지 나에게 내가 파란색 비트가 무엇입니까HTML 테이블 긁는 TR 태그의 나라를 얻으려고 노력 VBA

Sub ipsearch() 
    Dim x As Integer 
    x = 2 

    Do Until x = 4000 

     Dim ie As New InternetExplorer  
     ie.navigate "https://whatismyipaddress.com/ip/" & Range("E" & x).Value 

     Do  
      DoEvents   
     Loop Until ie.readyState = READYSTATE_COMPLETE 

     Dim doc As HTMLDocument  
     Set doc = ie.document  
     Dim sDD As String 

     sDD = Trim(doc.getElementsByTagName("td")(, 0).innerText) 

     Range("F" & x).Value = sDD  
     x = x + 1  
    Loop 

End Sub 

enter image description here 특정 행을 긁어 어떻게 테이블의 첫 번째 행을 제공합니다 나는 얻고 노란색 내가 당신의 예에서 "영국"를 반환 것, 이것은 뭔가

답변

2

원하는 것입니다 : 일반적으로

Sub ipsearch() 
    Dim x As Long 
    x = 2 

    Do Until x = 4000 
     Dim ie As New InternetExplorer 
     ie.navigate "https://whatismyipaddress.com/ip/" & "2.99.247.66" 

     Do 
      DoEvents 

     Loop Until ie.readyState = READYSTATE_COMPLETE 
     Dim doc As HTMLDocument 
     Set doc = ie.document 
     Dim sDD As String 
     ie.Visible = True 

     sDD = Trim(doc.getElementsByTagName("tbody")(1).innerText) 
     Range("F" & x).Value = Split(sDD, vbCrLf)(5) 

     x = x + 1 

    Loop 

End Sub 

을, AF 더 나은 코드 작성을위한 아이디어.

  • 예를 들어 스크린 샷에서 IP의 숫자는 파란색 위에있는 줄에서 가져옵니다. "2.99.247.66 세부 사항"에서.
  • StackOverflow에 코드를 제공 할 때 코드에서 사용중인 추가 라이브러리에 대해 언급해야합니다. 귀하의 경우에는이 두 :

enter image description here

또는 코드가 런타임에 바인딩을 사용하고 있는지 확인하여 라이브러리를 추가 할 수 없습니다. 당신이 그것을 제출할 때,


  • 형식 코드 Why Use Integer Instead of Long? - 은 - 일반적으로 Integer 대신에, Long을 사용하는 것이 좋습니다. 조금 나아 졌어. Ctrl + K은 StackOverflow의 바로 가기입니다.
  • VBA에 Option Explicit을 사용하십시오.
  • 질문에있는 변수를 하드 코드 해보십시오. 귀하의 경우 :

"https://whatismyipaddress.com/ip/" & "2.99.247.66"

+0

감사합니다. 귀하의 버전을 사용해 보겠습니다. – yusuf

2

사진을주세요. 국가 이름을 가져와 범위 ("A1")에 배치합니다.

1. Microsoft Internet Controls 
2. Microsoft HTML Object Library 

그리고 그것은 빠른 방법은, 하나의 아래에 시도 할 :

Sub ipsearch() 
    Dim IE As New InternetExplorer, html As HTMLDocument 
    Dim post As Object 

    With IE 
     .Visible = False 
     .navigate "https://whatismyipaddress.com/ip/2.99.247.66" 
     Do Until .readyState = READYSTATE_COMPLETE: Loop 
     Set html = .document 
    End With 

    For Each post In html.getElementsByTagName("th") 
     If InStr(post.innerText, "Country:") > 0 Then [A1] = post.ParentNode.LastChild.innerText: Exit For 
    Next post 
    IE.Quit 
End Sub 

참조 라이브러리에 추가 할

: 라이브러리에 추가 할

Sub ipsearch() 
    Dim HTTP As New XMLHTTP60, html As New HTMLDocument 
    Dim post As Object 

    With HTTP 
     .Open "GET", "https://whatismyipaddress.com/ip/2.99.247.66", False 
     .send 
     html.body.innerHTML = .responseText 
    End With 

    For Each post In html.getElementsByTagName("th") 
     If InStr(post.innerText, "Country:") > 0 Then [A1] = post.ParentNode.LastChild.innerText: Exit For 
    Next post 
End Sub 

참조

1. Microsoft XML,v6.0 ''or the version you have 
2. Microsoft HTML Object Library 

출력 :

United Kingdom 
관련 문제