Html 如何获取父类之外的元素
我试图从网上提取一些数据。但是,并非我需要的所有信息都在父类中。我可以在父类中获取信息 问题-如果数据在父类之外,是否有方法获取数据?或者有没有一种方法可以设置下面的代码来提取,而不使用父类 我正在使用IE,因为它允许我搜索网站。我尝试了几种代码变体,但是,额外的信息不是我试图从中提取的父类 我的名字后,地点和社会媒体的联系。位置位于课堂外网页的顶部 我尝试将以下内容用于父类Html 如何获取父类之外的元素,html,excel,vba,web-scraping,screen-scraping,Html,Excel,Vba,Web Scraping,Screen Scraping,我试图从网上提取一些数据。但是,并非我需要的所有信息都在父类中。我可以在父类中获取信息 问题-如果数据在父类之外,是否有方法获取数据?或者有没有一种方法可以设置下面的代码来提取,而不使用父类 我正在使用IE,因为它允许我搜索网站。我尝试了几种代码变体,但是,额外的信息不是我试图从中提取的父类 我的名字后,地点和社会媒体的联系。位置位于课堂外网页的顶部 我尝试将以下内容用于父类shop home,因为所有其他类都属于它,但它不起作用。我从未尝试过获取不在父类中的数据,因此,无法100%确定如何
shop home
,因为所有其他类都属于它,但它不起作用。我从未尝试过获取不在父类中的数据,因此,无法100%确定如何获取。SIM帮助处理此元素.ParentNode.ParentNode.getElementsByClassName
,因为产品url位于父项之前。我一直在尝试将其用于父级之外的所有其他数据,但是我无法使其工作。如果有人能解释一下.ParentNode.ParentNode.
正在做什么,我不完全理解,这将有助于我的理解,我可能可以自己解决其余的问题
下面的代码适用于前两个成功的项目,所有项目的代码布局都是相同的,除了它是asIf element.getElementsByClassName(“此处的类”)(0)
。我尝试过使用ID
标记Span
等等If元素.getElementsByClassName(“这里的类”)(0).getelementsByTagName(“Span”)(0)
结果-红色日期错误或丢失,因为它不在上述父类中
H列中的装运与父列中的装运一样正常,如果没有装运信息,则会在单元格中插入连字符。C、D、E的项不在我使用的父类中
商店会员
-
幸运梅工作室
所有者
除了阅读html和html文档方法/css选择器外,不知道该说些什么,以便您了解需要应用的模式。剩下的只是练习和学习,这是最快和更稳健的方法
CSS:
Option Explicit
Public Sub GetInfo()
Dim ie As SHDocVw.InternetExplorer
Set ie = New SHDocVw.InternetExplorer
With ie
.Visible = True
.Navigate2 "https://www.etsy.com/uk/shop/LuckyPlumStudio"
While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend
With .document
Debug.Print .querySelector(".shop-location span").innerText 'location
Dim i As Long, socialMedias As Object
Set socialMedias = .querySelectorAll("#about .text-decoration-none")
For i = 0 To socialMedias.Length - 1 'media links
Debug.Print socialMedias.Item(i).href
Next
Debug.Print .querySelector("[data-region='member-name']").innerText 'company name
End With
.Quit
End With
End Sub
位置:.shop Location span
是一个span
子元素,父元素具有类shop Location
社交媒体链接:#about.text decoration none
具有一个类名为text decoration none
的子节点,其父节点id为about
名称:[data region='member-Name']
元素,其数据区域
属性具有值成员名称
了解css选择器和后代组合器
练习css选择器
了解html
VBA:
Option Explicit
Public Sub GetInfo()
Dim ie As SHDocVw.InternetExplorer
Set ie = New SHDocVw.InternetExplorer
With ie
.Visible = True
.Navigate2 "https://www.etsy.com/uk/shop/LuckyPlumStudio"
While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend
With .document
Debug.Print .querySelector(".shop-location span").innerText 'location
Dim i As Long, socialMedias As Object
Set socialMedias = .querySelectorAll("#about .text-decoration-none")
For i = 0 To socialMedias.Length - 1 'media links
Debug.Print socialMedias.Item(i).href
Next
Debug.Print .querySelector("[data-region='member-name']").innerText 'company name
End With
.Quit
End With
End Sub
选项显式
公共子GetInfo()
Dim ie作为SHDocVw.InternetExplorer
设置ie=New SHDocVw.InternetExplorer
与ie
.Visible=True
.导航2“https://www.etsy.com/uk/shop/LuckyPlumStudio"
当.Busy或.readyState时readyState\u完成:DoEvents:Wend
随附.文件
Debug.Print.querySelector(“.shop location span”).innerText的位置
把我当长,把社交媒体当对象
Set socialMedias=.querySelectorAll(#about.text装饰无)
对于i=0到socialMedias.Length-1'媒体链接
Debug.Print socialMedias.Item(i).href
下一个
Debug.Print.querySelector(“[data region='member-name']”)。innerText'公司名称
以
退出
以
端接头
不太理想的选择方法:
Option Explicit
Public Sub GetInfo()
Dim ie As SHDocVw.InternetExplorer
Set ie = New SHDocVw.InternetExplorer
With ie
.Visible = True
.Navigate2 "https://www.etsy.com/uk/shop/LuckyPlumStudio"
While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend
With .document
Debug.Print .getElementsByClassName("shop-location wt-display-flex-xs")(0).getElementsByTagName("span")(0).innerText 'location
Dim i As Object, socialMedias As Object
Set socialMedias = .getElementById("about").getElementsByClassName("text-decoration-none clearfix")
For Each i In socialMedias 'media links
Debug.Print i.href
Next
Debug.Print .getElementById("about").getElementsByClassName("flag")(0).getElementsByTagName("h6")(0).innerText 'company name
End With
.Quit
End With
End Sub
选项显式
公共子GetInfo()
Dim ie作为SHDocVw.InternetExplorer
设置ie=New SHDocVw.InternetExplorer
与ie
.Visible=True
.导航2“https://www.etsy.com/uk/shop/LuckyPlumStudio"
当.Busy或.readyState时readyState\u完成:DoEvents:Wend
随附.文件
Debug.Print.getElementsByClassName(“车间位置wt display flex xs”)(0.getElementsByTagName(“span”)(0.innerText”位置
Dim i作为对象,socialMedias作为对象
设置socialMedias=.getElementById(“关于”).GetElementsByClass