无法从 XPath 获取数据
Cannot get data from XPath
我正在尝试使用 HtmlAgilityPack 从网页获取一些数据,它获取一些变量并给出一些结果。
我想从此网页中检索 3 个数据字段,到目前为止我只能得到其中的 2 个。
到目前为止我的代码
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;
}
}
获取不到的数据是我在代码中写的t1。当我在 url "&Area=country code" 中输入特定国家时,此字段可见。如果我输入另一个国家,它会给我另一个数字或 0%。如果我什么都不放,它会给我一份所有国家的名单。
如果我将其用作 Xpath
var t1 = doc2.DocumentNode.SelectSingleNode("//span[contains(text(),'" + txtEditXora.Text + "')]").InnerText;
它returns国家例如正确
Singapore (SG)
我想要这个国家的关税优惠百分比
这是我第一次使用 XPath,我还在学习,但我不想在我的项目中使用它。
这应该有效
//text()[contains(.,"preference")]/../../td[2]
你可以试试这个。我没有时间检查它是否适用于其他国家/地区。
doc2.DocumentNode.SelectNodes("//div[@id='" + taric + "']//td[@name='measure_description_search']//td")[4].InnerText
或者这样:
doc2.DocumentNode.SelectNodes("//div[@id='" + taric + "']//span[@class='duty_rate']")[1].InnerText
我正在尝试使用 HtmlAgilityPack 从网页获取一些数据,它获取一些变量并给出一些结果。 我想从此网页中检索 3 个数据字段,到目前为止我只能得到其中的 2 个。 到目前为止我的代码
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;
}
}
获取不到的数据是我在代码中写的t1。当我在 url "&Area=country code" 中输入特定国家时,此字段可见。如果我输入另一个国家,它会给我另一个数字或 0%。如果我什么都不放,它会给我一份所有国家的名单。 如果我将其用作 Xpath
var t1 = doc2.DocumentNode.SelectSingleNode("//span[contains(text(),'" + txtEditXora.Text + "')]").InnerText;
它returns国家例如正确
Singapore (SG)
我想要这个国家的关税优惠百分比
这是我第一次使用 XPath,我还在学习,但我不想在我的项目中使用它。
这应该有效
//text()[contains(.,"preference")]/../../td[2]
你可以试试这个。我没有时间检查它是否适用于其他国家/地区。
doc2.DocumentNode.SelectNodes("//div[@id='" + taric + "']//td[@name='measure_description_search']//td")[4].InnerText
或者这样:
doc2.DocumentNode.SelectNodes("//div[@id='" + taric + "']//span[@class='duty_rate']")[1].InnerText