Click here to Skip to main content
15,902,447 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I try to get text from a website more specific, by the Tag Name
<h2>

everything i tried failed so far.

What I have tried:

VB
Dim webrewq As Net.HttpWebRequest = Net.WebRequest.Create(url)
webrewq.Timeout = 60000
webrewq.Method = "GET"
Dim hwresponse As Net.HttpWebResponse = webrewq.GetResponse()
If hwresponse.StatusCode = Net.HttpStatusCode.OK Then
    Dim responseStream As IO.StreamReader = New IO.StreamReader(hwresponse.GetResponseStream())
    Label1.Text = responseStream.ReadLine()
    responseStream.Close()
End If
Posted
Updated 3-Oct-20 4:34am
Comments
Richard MacCutchan 3-Oct-20 10:30am    
So what happens?
[no name] 3-Oct-20 10:32am    
Right now this code just gives me the whole page source but i only need the inner text of that tag
Richard MacCutchan 3-Oct-20 10:44am    
Of course it does, because that is what the GET operation returns. If you want to extract a specific part then you have to add code to search through the returned text.

1 solution

Instead of doing that, have a look at the HTMLAgility pack[^] - it's available via NuGet in VS - if you are doing anything with web scraping it makes your life a whole load easier:
C#
HtmlWeb web = new HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = web.Load(url);
string title = doc.DocumentNode.SelectSingleNode("//title").InnerText;
HtmlNodeCollection h2Nodes = doc.DocumentNode.SelectNodes("//h2]");
HtmlNodeCollection divNodes = doc.DocumentNode.SelectNodes("//div[@class='pre-action-link']);
Or if you must use VB:
VB
Dim web As HtmlWeb = New HtmlWeb()
Dim doc As HtmlAgilityPack.HtmlDocument = web.Load(url)
Dim title As String = doc.DocumentNode.SelectSingleNode("//title").InnerText
Dim h2Nodes As HtmlNodeCollection = doc.DocumentNode.SelectNodes("//h2]")
Dim divNodes As HtmlNodeCollection = doc.DocumentNode.SelectNodes("//div[@class='pre-action-link']);")
Have a look at the documentation - it can really make your life eaiser!
 
Share this answer
 
Comments
[no name] 3-Oct-20 10:36am    
Thank you very much! I will look into it.
OriginalGriff 3-Oct-20 10:40am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900