html agility pack - Using HtmlAgilityPack in VB.Net to Get Text from a Website -


i writing program girlfriend allows open program , automatically gather quote horoscope website , display line of text in textbox.

as of have now, displays entire website in html, not want. html line need grab.

<div class="fontdef1" style="padding-right:10px;" id="textline"> "you might have desire travel, perhaps visit friend lives far away, gemini. may set wheels in motion make happen. social events take time evening, , meet interesting people. friend might need sympathetic ear. today you're sensitive others, prepared hear sad story. otherwise, day should go well.  </div> 

the code have far is.

imports system.net imports system.io imports htmlagilitypack  public class form1      private function gethtml(byval address string) string         dim rt string = ""          dim wrequest webrequest         dim wresponse webresponse          dim sr 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 system.object, byval e 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 

thank can get. have no idea go program now. it's been 3 days no progress.

learn xpath or linq pull out information html document using htmlagilitypack. console application example using xpath selector :

imports system imports system.xml imports htmlagilitypack  public module module1     public sub main()         dim link string = "http://my.horoscope.com/astrology/free-daily-horoscope-gemini.html"         'download page link htmldocument'         dim doc htmldocument = new htmlweb().load(link)         'select <div> having class attribute equals fontdef1'         dim div htmlnode = doc.documentnode.selectsinglenode("//div[@class='fontdef1']")         'if div found, print inner text'         if not div nothing             console.writeline(div.innertext.trim())         end if     end sub end module 

dotnetfiddle demo

output :

you might have desire travel, perhaps visit friend lives far away, gemini. may set wheels in motion make happen. social events take time evening, , meet interesting people. friend might need sympathetic ear. today you're sensitive others, prepared hear sad story. otherwise, day should go well.


Comments