Reputation: 184
I am trying to get some data from a web page with HtmlAgilityPack which gets some variables and gives some results. I want to retrieve 3 data fields from this webpage and so far I can only get the 2 of them. My code so far
struct Result
{
public string Description;
public string thirdCountryDuty;
public string tarifPreference;
}
private Result LoadWebPage(string url, string taric)
{
//This is the webpage which contains all three datas that I want. I just write it here as
url for testing
url = "https://ec.europa.eu/taxation_customs/dds2/taric/measures.jsp?Lang=en&SimDate=20200503&Area=SG&MeasType=&StartPub=&EndPub=&MeasText=&GoodsText=&op=&Taric=6213900010&search_text=goods&textSearch=&LangDescr=el&OrderNum=&Regulation=&measStartDat=&measEndDat=%22;"
var result = new Result();
taric = "6213900010";//This is a variable. I give it here for testing purposes
txtEditCountry.Text = "SG";//This is a variable. I give it here for testing purposes
try
{
var web2 = new HtmlWeb();
var doc2 = web2.LoadFromBrowser(url, html =>
{
// WAIT until the dynamic text is set
return !html.Contains("<div id=\"" + taric.ToString() + "\"></div>");
});
//t1 is the data that I cannot get
var t1 = doc2.DocumentNode.SelectSingleNode("//span[contains(text(),'" + txtEditCountry.Text + "')] and .//span[contains(.,'duty_rate')]]").InnerText;
//This is working
var t2 = doc2.DocumentNode.SelectSingleNode("//*[contains(@id,'"+ taric + "')]/table/tbody/tr/td[2]/table/tbody/tr/td[2]").InnerText;
//This is working
var t3 = doc2.DocumentNode.SelectSingleNode("//span[contains(@class,'duty_rate')]").InnerText;
Console.WriteLine("Text 1: " + t1);
Console.WriteLine("Text 2: " + t2);
Console.WriteLine("Text 3: " + t3);
result = new Result
{
Description = t2,
thirdCountryDuty = t3,
tarifPreference = t1
};
return result;
}
catch (Exception ex)
{
result.Description= null;
result.thirdCountryDuty = null;
result.tarifPreference = null;
MessageBox.Show("Check your data and try again \n" + ex.ToString());
return result;
}
}
The data that I cannot get is t1 as I wrote in the code. This field is visible when I put a specific country in url "&Area=country code". If I put another country it will give me another number or 0%. If I don't put anything it will give me a list with all countries. If I use this as Xpath
var t1 = doc2.DocumentNode.SelectSingleNode("//span[contains(text(),'" + txtEditXora.Text + "')]").InnerText;
It returns the country correct for example
Singapore (SG)
I want the tarif Preference percentage for this country
This is the first time that I use XPath and I am still learning but I wan't this for my project.
Upvotes: 0
Views: 112
Reputation: 7187
You can try this. I don't have time to check if it would work with other countries.
doc2.DocumentNode.SelectNodes("//div[@id='" + taric + "']//td[@name='measure_description_search']//td")[4].InnerText
Or this:
doc2.DocumentNode.SelectNodes("//div[@id='" + taric + "']//span[@class='duty_rate']")[1].InnerText
Upvotes: 1