0

我有一个让我困惑的问题!我想从网页中抓取一个值。我找到了带有 chrome 和检查器的 xpath "//*[@id='GlobalTab0Elm']/div[2]/div[1]/div[2]/table/tbody/tr[7]/td[2]/div"

当我在 chrome (扩展)的 webscraper 中使用上面的 xpath 时,它工作正常。但问题是这不适用于我的简单程序,您可以看到:

     Dim Handler As HtmlAgilityPack.HtmlWeb.PreRequestHandler = Function(request As HttpWebRequest)
                                                                       request.Headers(HttpRequestHeader.AcceptEncoding) = "gzip, deflate"
                                                                       request.AutomaticDecompression = DecompressionMethods.Deflate Or DecompressionMethods.GZip
                                                                       request.CookieContainer = New System.Net.CookieContainer()
                                                                       Return True
                                                                   End Function
        Dim webClient As HtmlWeb = New HtmlWeb()
        webClient.PreRequest = Handler


        webClient.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5"
        webClient.UseCookies = True

        Dim htmlDoc = webClient.Load("http://www.tsetmc.com/Loader.aspx?ParTree=15")
        htmlDoc.OptionReadEncoding = False
        Dim S As String
        S = "//*[@id='GlobalTab0Elm']/div[2]/div[1]/div[2]/table/tbody/tr[7]/td[2]/div"
        Dim node = htmlDoc.DocumentNode.SelectSingleNode(S)
    TextBox1.Text = (node.InnerText)

我的问题是为什么这个 xpath 可以在另一个刮刀上工作,但不能在这些代码上工作?我必须做什么?错误发生在这一行:

TextBox1.Text = (node.InnerText)

错误文本是

Object reference not set to an instance of an object.

当我使用这个 xpath

"//*[@id='company_text']/text()[2]"

它找到了正确的 xpath 值。

我的问题是它不适用于这个 xpath。

"//*[@id='GlobalTab0Elm']/div[2]/div[1]/div[2]/table/tbody/tr[7]/td[2]/div"
4

1 回答 1

0

我找到了解决方案

 For Each li As HtmlNode In htmlDoc.DocumentNode.SelectNodes("//tbody")


            ArzeshNode = li.SelectSingleNode("//*[contains(@class,'table1')]/tbody/tr[7]/td[2]/div")   

        Next
msgbox( SplitValue((ArzeshNode.InnerText)) )
于 2020-04-25T11:17:44.163 回答