如何使用excelvba获取HTML网页的特定属性
这是前一篇文章的延续。在这篇新文章中,我试图捕获以下列表下HTML代码中以下元素的内容: 发布日期预期结果:2018年8月18日上午4:19 预期结果:美国 地址地区预期结果:俄克拉荷马市 HTML文本如下所示:如何使用excelvba获取HTML网页的特定属性,excel,vba,web-scraping,Excel,Vba,Web Scraping,这是前一篇文章的延续。在这篇新文章中,我试图捕获以下列表下HTML代码中以下元素的内容: 发布日期预期结果:2018年8月18日上午4:19 预期结果:美国 地址地区预期结果:俄克拉荷马市 HTML文本如下所示: <div class="container-fluid"> <div itemscope itemtype="http://schema.org/JobPosting"> <div class="row content">
<div class="container-fluid">
<div itemscope itemtype="http://schema.org/JobPosting">
<div class="row content">
<div class="col-sm-3 sidenav well job_detail_lhs">
<div class="card">
<div class="card-body">
<strong><a href="/?cmp=jd&from=search-more">< Search 32182 More Oil Jobs </a></strong>
<meta itemprop="datePosted" content="Aug. 18, 2018, 4:19 a.m." />
<meta itemprop="industry" content="Oil & Gas" />
<span itemprop="jobLocation" itemscope itemtype="http://schema.org/Place">
<span itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">
<h4><strong>Country:</strong></h4>
<p itemprop="addressCountry">United States</p>
<h4><strong>Location:</strong></h4>
<meta itemprop="addressRegion" content="Oklahoma City" />
<p itemprop="addressLocality">Oklahoma City</p>
</span>
</span>
<h4><strong>Posted:</strong></h4>
<p>22 Days Ago</p>
<div>
我尝试了几个getElementsByTagName的串联组合,但没有得到预期的结果
提前感谢您的帮助 我使用了xmlhttp请求来加快执行时间。非常简洁,易于处理。我在下面的脚本中使用了相同的方法来定位元素。看看这个:
Sub GetInfo()
Const url$ = "https://www.oneoiljobsearch.com/jobs/senior-reservoir-engineer-oklahoma-city-united-states-4/?cmp=js&from=job-search-form-7"
Dim Http As New XMLHTTP60, Html As New HTMLDocument
With Http
.Open "GET", url, False
.send
Html.body.innerHTML = .responseText
End With
Range("A1") = Html.querySelector("meta[itemprop='datePosted']").getAttribute("content")
Range("A1").Offset(, 1) = Html.querySelector("p[itemprop='addressCountry']").innerText
Range("A1").Offset(, 2) = Html.querySelector("meta[itemprop='addressRegion']").getAttribute("content")
End Sub
要添加到库中的引用:
Microsoft XML, v6.0
Microsoft HTML Object Library
我已经使用xmlhttp请求来加快执行时间。非常简洁,易于处理。我在下面的脚本中使用了相同的方法来定位元素。看看这个:
Sub GetInfo()
Const url$ = "https://www.oneoiljobsearch.com/jobs/senior-reservoir-engineer-oklahoma-city-united-states-4/?cmp=js&from=job-search-form-7"
Dim Http As New XMLHTTP60, Html As New HTMLDocument
With Http
.Open "GET", url, False
.send
Html.body.innerHTML = .responseText
End With
Range("A1") = Html.querySelector("meta[itemprop='datePosted']").getAttribute("content")
Range("A1").Offset(, 1) = Html.querySelector("p[itemprop='addressCountry']").innerText
Range("A1").Offset(, 2) = Html.querySelector("meta[itemprop='addressRegion']").getAttribute("content")
End Sub
要添加到库中的引用:
Microsoft XML, v6.0
Microsoft HTML Object Library
使用CSS选择器和IE
Option Explicit
Public Sub GetInfo()
Dim ie As New InternetExplorer
With ie
.Visible = True
.navigate "https://www.oneoiljobsearch.com/jobs/senior-reservoir-engineer-oklahoma-city-united-states-4/?cmp=js&from=job-search-form-7"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
Debug.Print .querySelector("[itemprop=datePosted]").Content
Debug.Print .querySelector("[itemprop=addressCountry]").innerText
Debug.Print .querySelector("[itemprop=addressRegion]").Content
End With
Stop '<=delete me after
'other stuff
.Quit
End With
End Sub
WinHTTP也是如此
Option Explicit
Public Sub GetInfo()
Dim html As New HTMLDocument
With CreateObject("WinHttp.WinHttpRequest.5.1")
.Open "GET", "https://www.oneoiljobsearch.com/jobs/senior-reservoir-engineer-oklahoma-city-united-states-4/?cmp=js&from=job-search-form-7", False
.send
html.body.innerHTML = .ResponseText
With html
Debug.Print .querySelector("[itemprop=datePosted]").Content
Debug.Print .querySelector("[itemprop=addressCountry]").innerText
Debug.Print .querySelector("[itemprop=addressRegion]").Content
End With
Stop '<=delete me after
'other stuff
End With
End Sub
使用CSS选择器和IE
Option Explicit
Public Sub GetInfo()
Dim ie As New InternetExplorer
With ie
.Visible = True
.navigate "https://www.oneoiljobsearch.com/jobs/senior-reservoir-engineer-oklahoma-city-united-states-4/?cmp=js&from=job-search-form-7"
While .Busy Or .readyState < 4: DoEvents: Wend
With .document
Debug.Print .querySelector("[itemprop=datePosted]").Content
Debug.Print .querySelector("[itemprop=addressCountry]").innerText
Debug.Print .querySelector("[itemprop=addressRegion]").Content
End With
Stop '<=delete me after
'other stuff
.Quit
End With
End Sub
WinHTTP也是如此
Option Explicit
Public Sub GetInfo()
Dim html As New HTMLDocument
With CreateObject("WinHttp.WinHttpRequest.5.1")
.Open "GET", "https://www.oneoiljobsearch.com/jobs/senior-reservoir-engineer-oklahoma-city-united-states-4/?cmp=js&from=job-search-form-7", False
.send
html.body.innerHTML = .ResponseText
With html
Debug.Print .querySelector("[itemprop=datePosted]").Content
Debug.Print .querySelector("[itemprop=addressCountry]").innerText
Debug.Print .querySelector("[itemprop=addressRegion]").Content
End With
Stop '<=delete me after
'other stuff
End With
End Sub
您的帖子中没有指向网页的链接。请包括它以获得任何特定的解决方案。链接已修复!谢谢您的帖子中没有指向网页的链接。请包括它以获得任何特定的解决方案。链接已修复!谢谢你可能想得到这个日期2018年8月18日凌晨4:19。我已经在脚本中解决了这个问题。像往常一样使用很好的选择器。我现在发现API是石油和天然气公司的一个公司/角色,而不是应用程序编程接口的有用搜索词。太好了,我看到这种方式要快得多!你能推荐一个关于xmlhttp的教程吗?我相信我会经常使用它!谢谢你的支持!QHarr API是美国石油学会的缩写,他们创建了一种测量石油重力的方法,通常称为API重力,简称API。祝大家一周愉快!对于后面部分中关于xmlhttp请求的教程@Pegaso.You可能希望获得这个日期2018年8月18日凌晨4:19。我已经在脚本中修复了这个问题。像往常一样使用好选择器。我现在发现API是石油和天然气公司的一个公司/角色,而不是应用程序编程接口的有用搜索词。太好了,我看到这种方式要快得多!你能推荐一个关于xmlhttp的教程吗?我相信我会经常使用它!谢谢你的支持!QHarr API是美国石油学会的缩写,他们创建了一种测量石油重力的方法,通常称为API重力,简称API。祝大家一周愉快!关于xmlhttp请求的教程,在后面的@Pegaso部分,我在Debug.Print的第一行得到了一个错误'91'。我已经搜索了很多如何使用XMLHHTP获取元数据的方法,但是没有luckI在Debug.Print的第一行出现错误“91”。我已经搜索了很多关于如何使用XMLHHTP获取元数据的信息,但是没有找到