【发布时间】:2021-04-27 18:11:23
【问题描述】:
这里是新手!
我编写了一段代码,试图获取出现在 html 页面“head”元素中的文本:1.3253/1.3325 XRP/USD - GateHub Markets
代码不会产生任何错误,它只是输出:total count = 1,但不是我要提取的文本。
请指教如何调整它??
这是我要抓取的页面的 HTML:
<html lang="en" ng-app="landing" class="ng-scope">
<head><style type="text/css">@charset "UTF-8";[ng\:cloak],[ng-cloak],[data-
ng-cloak],[x-ng-cloak],.ng-cloak,.x-ng-cloak,.ng-hide:not(.ng-hide-animate)
{display:none !important;}ng\:form{display:block;}.ng-animate-
shim{visibility:hidden;}.ng-anchor{position:absolute;}</style>
<!--
_____ _ _ _ _
/ ____| | | | | | | | |
| | __ __ _| |_ ___| |__| |_ _| |__
| | |_ |/ _` | __/ _ \ __ | | | | '_ \
| |__| | (_| | || __/ | | | |_| | |_) |
\_____|\__,_|\__\___|_| |_|\__,_|_.__/
GateHub is a platform for the Internet of Value.
-->
<meta charset="utf-8">
<base href="/">
<title ng-bind="pageTitle" class="ng-binding">1.3253/1.3325 XRP/USD -
GateHub Markets</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
这是尝试提取部件内文本的代码
Option Explicit
Private cd As Selenium.ChromeDriver
Sub New1()
Dim FindBy As New Selenium.By
Dim Results As Selenium.WebElements
Dim Result As Selenium.WebElement
Set cd = New Selenium.ChromeDriver
cd.Start
cd.get "https://gatehub.net/markets/XRP/USD+rhub8VRN55s94qWKDv6jmDy1pUykJzF3wq"
If Not cd.IsElementPresent(FindBy.Tag("title"), 3000) Then
cd.Quit
MsgBox ("Not found!")
Exit Sub
End If
Set Results = cd.FindElementsByTag("title")
Debug.Print "Total found =" & Results.Count
Dim n As Long
For n = 1 To Results.Count
Debug.Print Results(n).Text
Next
cd.Quit
End Sub
【问题讨论】:
-
其他语言有
driver.title,但我对VB不熟悉...你试过吗?在您的情况下,可能是cd.Title。它应该将页面的标题作为字符串返回。 -
另外,请阅读为什么a screenshot of code is a bad idea。粘贴代码并正确格式化它。这包括 HTML。
-
这次我同意你@JeffC :)
-
这里出现什么错误,对于什么代码行?此外,可能是该元素的
value属性而不是文本。 -
谢谢!我试过 cd.title 但它不适用于 VBA 目的。为截图道歉:无论如何,我也会在下面复制我试图提取的页面部分。 @Prophet:我尝试使用 .value 而不是 .text,得到相同的输出:Total found = 1,但没有我想要提取的价格值。