HtmlAgilityPack - Get only the first match - vb.net

Im new to HtmlAgilityPack and i cant figure out how to stop the search after the first match has been found.
Dim site As HtmlAgilityPack.HtmlWeb = New HtmlWeb()
Dim document As HtmlAgilityPack.HtmlDocument = site.Load("website")
For Each table As HtmlNode In document.DocumentNode.SelectNodes("//td[#class='forum_thread_post']//a[#href]")
ListBox1.Items.Add(table.InnerText)
Next
The problem is that the website contains many td[#class='forum_thread_post' nodes and i only need the first one. I experimented with SelectSingleNode too, but i couldnt even get that to work but im thinking that is the way to do it? If getting the single match to a textbox is better/easier i would want that.
Here is a picture: http://oi42.tinypic.com/25fmwr5.jpg
I want to get the a title or a alt from the picture

What was wrong with SelectSingleNode?
Dim table = document.DocumentNode.SelectSingleNode("//td[#class='forum_thread_post']//a[#href]")
ListBox1.Items.Add(table.InnerText)
If the InnerText could be empty:
Dim tables = document.DocumentNode.SelectNodes("//td[#class='forum_thread_post']//a[#href]")
Dim firstNotEmpty = tables.FirstOrDefault(Function(t) Not String.IsNullOrWhiteSpace(t.InnerText))

Related

List(Of...) WebBrowser - How to declare Web Browsers based on number of strings in an array

I am trying to grab images from a number of similar URLs (The urls do not end in an image format like .jpg). The number of URLs can vary, and is determined by counting the number of strings in an array of strings.
The best method I have found for downloading images from a website is taken from here:
https://www.codeguru.com/columns/vb/how-to-use-visual-studio-2012-to-download-images-from-websites.htm
This was working perfectly, however now I need to be able to declare more web browsers, depending on how many urls have to be visited to grab images for downloading. The following is what I would like to be able to do:
Dim photonumbers() As String = {"hdshshaga","sjshaghah","akajaha"}
Dim WebBrowsers As New List(Of WebBrowser)
Dim x As Integer
For x = 0 To photonumbers.Count - 1 Step 1
WebBrowsers.Add("wb"&x.ToString) 'I understand I am trying to add a string to a list of webbrowsers, but is there a way to declare a new webbrowser with this name?
Next
The reason I want to declare a new webbrowser for each url is that I cannot get the code from the above link to work as a single function solution. Instead you have to download the URL onto the web browser using one subroutine (eg.Triggered by a Button click), and then save all images from that webbrowser with the next subroutine. If anyone is able to advise me on how to change this code to a single function so that it can be called for each string, that would be even better.
Dim photonumbers() As String = {"hdshshaga", "sjshaghah", "akajaha"}
Dim WebBrowsers As New List(Of WebBrowser)
For Each photonumber As String In photonumbers
WebBrowsers.Add(New WebBrowser() With {.Name = "wb" & photonumber})
Next

How to extract URLs from HTML source in vb.net

My question is: I have a program that fetches the whole source code of a specified URL. The source code will be saved in a variable.
Part of the source code looks like this:
"thumbnail_src":"https:\/\/scontent-fra3-1.blablabla.com\/t51.2885-15\/s640x640\/sh0.08\/e35\/1234567_984778981596410_1107218704_n.jpg","is_video":false,
The code is has quite a bunch of those URLs. I want my code to look for the part "thumbnail_src":" as a marker for beginning the extraction process and stop the extraction at ","is_video":
This should be obviously done in a loop until all URLs are being extracted and saved into a listing variable.
How can I achieve that?
I am trying to get that Regexp into my sourcecode. The one that codexer wrote, which is correct but I am getting eerrors in visual basic net.
Dim regex As Regex = New Regex("thumbnail_src""": """(.*)""","""is_video")
Dim match As Match = regex.Match(sourceString)
If match.Success Then
Console.WriteLine(match.Value)
End If
I tried it this way..and also that way:
Dim regex As Regex = New Regex("thumbnail_src":"(.*)","is_video")
Something is wrong the way I am entering the regex code.
Here is the correct one I need to implement:
https://regex101.com/r/hK0xH8/4
thumbnail_src":"(.*)","is_video
In light of your recent edit I am going to redo this answer.
Since it looks like everything is coming in on one line of text here is how I would handle it.
Dim LargetxtLine as String = TheVeryLargylineofText
Dim CommaSplit as String() = LargetxtLine.split(","c)
Dim URLList as New List(of String)
Dim RG as New Regex("\"":\""(.*)\""")
For Each str as String in CommaSplit
If str.contains("thumbnail_src") Then
URLList.Add(RG.Match(str).value)
End If
Next
This will break up the long line of text into managable chunks and then it uses regex to add it to a list of URL's (URLList)
From there you can do just about anything with a list(of String).
There is another way of doing it without splitting on the ,'s
if you use this Regex
"thumbnail_src\"":\""(.*?)\"",\""is_video"
Adding the "?" in there turns it into a greedy statement meaning that it will stop at the first occurance.
After that you could create a URLList like this
DIM RG as New Regex("thumbnail_src\"":\""(.*?)\"",\""is_video")
Dim URLList as MatchCollection = RG.Matches(reallybigString)
It's really personal preference

Retrieving data in VB.NET

Very new to VB.NET so please go easy :) Im currently working on an old application and im wondering if you could help me retrieve the data more efficiently. The below code works fine but everytime a new application variable is added it will need to be amended e.g dim groupthree, dim groupfour etc etc
Dim GroupOne As New Organisation(Application("testVar1"))
Dim GroupTwo As New Organisation(Application("testVar2"))
ddlGroups.AddDropDownListItem(GroupOne.Title, GroupOne.OrganisationID)
ddlGroups.AddDropDownListItem(GroupTwo.Title, GroupTwo.OrganisationID)
GroupOne = Nothing
GroupTwo = Nothing
Basically im wondering if someone could suggest a better way to retrieve the variable data (which are in a DB table). I was thinking about using a list but so far i have had no luck trying to implement it
Many thanks
Paul
I was thinking something like the follwoing if the application variable were calle dthe same in the db but although there are two instances it only shows one in the dropdown list
Dim myList As New List(Of Organisation)
Dim Group As New Organisation(Application("testVar1"))
myList.Add(Group)
For Each Item In myList
ddlGroup.AddDropDownListItem(Group.Title, Group.OrganisationID)
Group = Nothing
Next
Cheers
Paul

Get information from a HTML page

I'm novice and I'm trying to understand how to get information from a webpage, I've already read about HtmlAgilityPack and I'm using it, but after 2 days trying to understand how I can do this, here am I asking for help.
Ok, the thing is: I want to read some informations from a page and write it in a label text.
The page I'll use as example is: http://www.tibia.com/community/?subtopic=characters&name=Huur
I want to show in different labels the level, the vocation and the guild informations...
But, all I got is this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim myWeb As HtmlWeb = New HtmlWeb
Dim myDoc = myWeb.Load("http://www.tibia.com/community/?subtopic=characters&name=" & TextBox1.Text.Trim())
Dim myRoot As HtmlNode = myDoc.DocumentNode
Dim myElements As List(Of HtmlElement) = New List(Of HtmlElement)
Dim MainContentArea As HtmlNode
myWeb.Load("http://www.tibia.com/community/?subtopic=characters&name=" & TextBox1.Text.Trim())
MainContentArea = myDoc.GetElementbyId("characters")
TextBox2.Text = MainContentArea.InnerHtml
End Sub
As you guys can see, I found a way to read all the character informations, but I don't know how to find the thing that I want that is: level, vocation and guild informations and show it in differents labels text.
Can you guys help me please? :}
(In the code I'm using Textbox2.Text to show the page content cause it shows alot of things and I've got errors when trying to show the content in a label text.)
Sorry for the bad english guys.
First I would suggest looking at xpath if you aren't familiar with it. Secondly, you will need to figure out your html structure. You can use Firefox and go to what you are looking for and right-click on inspect element. It will lay out the structure of the document and give you information you can use for the xpath.
For instance if you want to get the level you can use "/html/body//div[#class='BoxContent'/table/body/tr[td='Level:']/td" to get the element that contains the level indicator and then move to the HtmlNode.NextSibling to get the next element whose text contains the value of the level you are looking for.
I hope that is enough to get you started.

Linq Query to compare if collection contains list of string

Im fairly new to Linq and am trying to write a search results query.
We use a tagging system for our help desk and Im trying to return all cases that contain tags or children of TAGS selected from a drop down list.
I am using a path comparison to find any children tags which works well when there is only 1 tag selected. This is my code for when multiples tags are selected but it is not working. Can you point me in the right direction?
Single Tag Selection (working)
Dim tagpath = uxTags.SelectedItem.Text
lnqCases = From i In lnqCases Where i.HelpDeskTagItems.Any(Function(x) x.Path.StartsWith(tagpath))
Multi Tag Selection (not working)
Dim tagpaths As New List(Of String)
For Each i In TagList.SelectedItems
tagpaths.Add(i.ToString)
Next
lnqCases = From i In lnqCases Where i.HelpDeskTagItems.Any(Function(x) x.Path.StartsWith(tagpaths.Any))
Reverse the logic, check for the Path within the list
lnqCases = From i In lnqCases Where i.HelpDeskTagItems.Any(Function(x) tagpaths.Contains(x.Path))
Or to continue the StartsWith logic
lnqCases = From i In lnqCases Where i.HelpDeskTagItems.Any(Function(x) Test.Exists(Function(m) m.StartsWithPath(x)))