我想从一个URL页面中删除一个数据,我正在使用Excel VBA

w9apscun  于 2023-03-24  发布在  其他
关注(0)|答案(2)|浏览(114)

1)我要删除此URL中出现的电话号码https://www.idealista.com/inmueble/96599356/电话是607176984。到目前为止,我已经能够从该URL获得我需要的其他数据。
2)使用Chrome,我得到了URL的代码(与本例相关的行)

<div class="ide-box-contact module-contact-gray contact-data-container ">
<h2 class="txt-big txt-bold mb-small">
Anunciante
</h2>
<div id="contact-phones-container" class="hidden-contact-phones no-form">
<a role="button" aria-label="contact phone" class="icon-phone hidden-contact-phones_link" href="">
<span class="hidden-contact-phones_text">Ver teléfono</span>
<span class="animation_circle">
<span class="circle"></span>
<span class="circle"></span>
<span class="circle"></span>
</span>
</a>
</div>
<div class="professional-name">
<div class="name">
Particular
</div>
<span class="particular">
<span class="icon-block"></span>

3)我用Excel VBA编写了此代码

EnSub detalles_de_cada_trastero()
ActiveSheet.Select

hoja_actual = ActiveSheet.Name
   
Application.ScreenUpdating = False
    
Dim XMLPage As New MSXML2.XMLHTTP60
Dim htmldoc As New MSHTML.HTMLDocument
Dim htmlim As MSHTML.IHTMLElement
Dim htmlims As MSHTML.IHTMLElementCollection
Dim url As String
    
url = "https://www.idealista.com/inmueble/96599356/"

XML页面。打开“Get”,url_corregida,False
setRequestHeader“Content-Type”,“text/xml”On Error Resume Next XMLPage.send
htmldoc.body.innerHTML = XMLPage.responseText

# First: I try with this code. It doesn't work
        
       Set htmlims = htmldoc.getElementsByClassName("hidden-contact-phones_text")

        For Each htmlim In htmlims
            Sheets("hoja_actual").Cells(Fila, 12).Value = htmlim.innerText        
        texto = htmlim.innerText   ' what I get is  "Ver teléfono"        
        Next htmlim
====================================================
   
#  'Second: I try with this code
        Set htmlims = htmldoc.getElementsByClassName("icon-phone hidden-contact-phones_formatted-phone _mobilePhone")

        For Each htmlim In htmlims '*************************The control doesn't enter in this For-Next.   It jumps it              
        texto = htmlim.getAttribute("href")        
        'Sheets("hoja_actual").Cells(Fila, 12).Value = htmlim.innerText        
        Next htmlim
        
        '===============================================================
# 'third: I try this
        
        texto = htmldoc.getElementsByClassName("icon-phone hidden-contact-phones_formatted-phone _mobilePhone")  'Gives "nothing"
        
        texto = htmldoc.getElementsByClassName("icon-phone hidden-contact-phones_formatted-phone _mobilePhone").Item
        texto = htmldoc.getElementsByClassName("icon-phone hidden-contact-phones_formatted-phone _mobilePhone").toString 
  
        
        '===============================================================
#       'Fourth: I try this
        
        texto = htmldoc.getElementById("contact-phones-container")
 
        Set htmlims = htmldoc.getElementById("contact-phones-container")    'Error!
      
        For Each htmlim In htmlims            
           texto_1 = htmlim.Children(0).innerText  'sale profesonal'
            texto = htmlim.getAttribute("href")  
            texto_2 = htmlim.Children(1).innerText         
        Next


Any help will be wellcome
9njqaruj

9njqaruj1#

我的经验是可怕的这种方法..成像300产品的价格和可用性,它做了大约2.5小时。正如我建议几年前..最强大的方式来做到这一点是通过python。
但这里有一个例子

Sub ExtractPhoneNumber()
Dim url As String
Dim html As Object
Dim phoneElement As Object
Dim phoneNumber As String

' Get the HTML content of the page
url = "https://example.com"
Set html = CreateObject("MSXML2.XMLHTTP")
html.Open "GET", url, False
html.send

' Parse the HTML to find the phone number
Set phoneElement = html.responseXML.querySelector(".icon-phone")
If Not phoneElement Is Nothing Then
    phoneNumber = phoneElement.getAttribute("href")
    ' Extract the phone number from the href attribute
    phoneNumber = Mid(phoneNumber, InStr(phoneNumber, "tel:") + 4)
End If

' Print the phone number to the immediate window
Debug.Print phoneNumber

末端子组件

cngwdvgl

cngwdvgl2#

谢谢,但我得到的是一个错误,我解释如下

Sub ExtractPhoneNumber()
Dim url As String
Dim html As Object
Dim phoneElement As Object
Dim phoneNumber As String

' Get the HTML content of the page
'url = "https://example.com"
url = "https://www.idealista.com/inmueble/96599356/"

Set html = CreateObject("MSXML2.XMLHTTP")
html.Open "GET", url, False
html.send

' Parse the HTML to find the phone number
Set phoneElement = html.responseXML.querySelector(".icon-phone") **'<== Error**, 
ejecuting this line

If Not phoneElement Is Nothing Then
    phoneNumber = phoneElement.getAttribute("href")
   ' Extract the phone number from the href attribute
    phoneNumber = Mid(phoneNumber, InStr(phoneNumber, "tel:") + 4)
End If

' Print the phone number to the immediate window
Debug.Print phoneNumber

末端子组件

相关问题