I'm currently making a C# console application with HtmlAgilityPack where I am trying to get a parameter value of a link that is on a webpage. So basically I have a webpage, on that page there are a bunch of links. And one of the links has a parameter called "&pagenumber=[some number]
". What I am trying to get is the value after &pagenumber=
and save that to an int variable.
Steps:
Go to website (http://forum.tibia.com/forum/?action=board&boardid=25&threadage=-1)
Look for the text "Last Page" in a url at the bottom of the page:
<a href="http://forum.tibia.com/forum/?action=board&boardid=25&threadage=-1&pageitems=30&pagenumber=974">Last Page</a>
Grab the parameter value from "pagenumber" (in this case "974")
Save it to an integer variable
My code so far:
string PageLink = "http://forum.tibia.com/forum/?action=board&boardid=25&threadage=-1";
Task.Run(async () =>
{
using (var client = new HttpClient())
{
// Load the html of the page
var html = await client.GetStringAsync(PageLink);
var document = new HtmlAgilityPack.HtmlDocument();
document.LoadHtml(html);
// Find the "Last Page" link at bottom of page
var lastPageLink = document.DocumentNode.Descendants("a").First(x => x.Attributes["href"].Value.Contains("&threadage=-1&pageitems=30&pagenumber=")).InnerHtml;
// Print out the pagenumber value
Console.WriteLine(lastPageLink);
}
}).Wait(1000);
However, my code does not print anything so I am wondering what I am doing wrong here. I don't get any error. I basically tried to find all the links (a-tag), I look at the "href" value of them and see if it contains "&threadage=-1&pageitems=30&pagenumber=". And if it does, it should select the html code of it.
So right now, I want my code to print: http://forum.tibia.com/forum/?action=board&boardid=25&threadage=-1&pageitems=30&pagenumber=974
I can then move forward to use Regex or something, to get the "974".
It is very important that the url contains "board&boardid=25&threadage=-1", because there are other links with the "Last Page" value in it.