2015-07-21 142 views
2

我正在爲我的女朋友編寫一個程序,允許她打開該程序,它會自動從她的星座網站收集她的報價,並顯示該行文本文本框。在VB.Net中使用HtmlAgilityPack從網站獲取文本

至於我現在的情況,它基本上顯示整個網站的HTML,這不是我想要的。這是我需要抓住的HTML線。

<div class="fontdef1" style="padding-right:10px;" id="textline"> 
"You might have the desire for travel, perhaps to visit a friend who lives far away, Gemini. You may actually set the wheels in motion to make it happen. Social events could take up your time this evening, and you could meet some interesting people. A friend might need a sympathetic ear. Today you're especially sensitive to others, so be prepared to hear a sad story. Otherwise, your day should go well. 
</div> 

我到目前爲止的代碼是。

Imports System.Net 
Imports System.IO 
Imports HtmlAgilityPack 

Public Class Form1 

    Private Function getHTML(ByVal Address As String) As String 
     Dim rt As String = "" 

     Dim wRequest As WebRequest 
     Dim wResponse As WebResponse 

     Dim SR As StreamReader 

     wRequest = WebRequest.Create(Address) 
     wResponse = wRequest.GetResponse 

     SR = New StreamReader(wResponse.GetResponseStream) 

     rt = SR.ReadToEnd 
     SR.Close() 

     Return rt 
    End Function 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
     Label2.Text = Date.Now.ToString("MM/dd/yyyy") 
     TextBox1.Text = getHTML("http://my.horoscope.com/astrology/free-daily-horoscope-gemini.html") 
    End Sub 
End Class 

謝謝你的幫助,我可以得到。我現在真的不知道該怎麼辦。已經3天沒有進展了。

回答

0

學習XPathLINQ使用HtmlAgilityPack從HTML文檔中提取某些信息。這是使用XPath選擇一個控制檯應用程序例如:

Imports System 
Imports System.Xml 
Imports HtmlAgilityPack 

Public Module Module1 
    Public Sub Main() 
     Dim link As String = "http://my.horoscope.com/astrology/free-daily-horoscope-gemini.html" 
     'download page from the link into an HtmlDocument' 
     Dim doc As HtmlDocument = New HtmlWeb().Load(link) 
     'select <div> having class attribute equals fontdef1' 
     Dim div As HtmlNode = doc.DocumentNode.SelectSingleNode("//div[@class='fontdef1']") 
     'if the div is found, print the inner text' 
     If Not div Is Nothing Then 
      Console.WriteLine(div.InnerText.Trim()) 
     End If 
    End Sub 
End Module 

Dotnetfiddle Demo

輸出:

也許你有旅遊的慾望,也許是去拜訪一位朋友誰住很遠,雙子座。您可能實際上將車輪置於運動中以使其發生。社交活動今晚可能會佔用你的時間,你可能會遇到一些有趣的人。一個朋友可能需要一個同情的耳朵。今天你對別人特別敏感,所以準備好聽一個悲傷的故事。否則,你的日子應該順利。

+0

好吧我帶你的建議,並試圖將你從控制檯轉換爲IO,以便它將輸出到我的TextBox1。 我改變了'Console.WriteLine(div.InnerText.Trim())'來 '昏暗完成的String =(div.InnerText.Trim())' 問題是現在,我不知道怎麼拉字符串「完成」到我的私人小組用於TextBox1。我曾嘗試調用Main,認爲字符串會隨之而來,但我想不是這種情況。 有什麼建議嗎?謝謝 – RockGuitarist1

+0

@ RockGuitarist1'TextBox1.Text = finish'? – har07

+0

我收到錯誤,說'finish'沒有聲明。好像我不能將它從'Public Sub Main()'移動到'Private Sub Form1_Load'。 – RockGuitarist1