C# 无法从XPath获取数据
我试图从一个带有HtmlAgilityPack的网页上获取一些数据,它可以获取一些变量并给出一些结果。 我想从这个网页检索3个数据字段,到目前为止,我只能得到其中的2个。 到目前为止我的代码C# 无法从XPath获取数据,c#,xpath,html-agility-pack,C#,Xpath,Html Agility Pack,我试图从一个带有HtmlAgilityPack的网页上获取一些数据,它可以获取一些变量并给出一些结果。 我想从这个网页检索3个数据字段,到目前为止,我只能得到其中的2个。 到目前为止我的代码 struct Result { public string Description; public string thirdCountryDuty; public
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;
}
}
例如,它返回正确的国家/地区
新加坡(SG)
我想要这个国家的tarif优惠百分比
这是我第一次使用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
看起来数据在一个
$x(//span[contains(text(),'SG')]和//td[contains(,'duty')])
@Saravanan中,我试过了,但不起作用。它抛出System.XML.XPath.XPathException。你有一个小错误。代码应该是//span[contains(text(),'SG')和//td[contains(,'duty')]],但仍然不返回数据它返回true,这意味着条件和
已经成功。如果要选择,请使用中的选择器hierarchy@Saravanan根据你的评论,我尝试了//span[contains(text(),'SG')和//*[(@class='duty_rate')]],但它返回的是国家名称,而不是8.30%中的8.30%。这返回的是第一个“duty_rate”,在特定变量中为10.00%。我可以选择第二个值吗?我想这是因为有不止一个类使用了duty\u rate
,也许可以尝试//div[@id=“measure\u 3712467”]//span[@class=“duty\u rate”]
。如果没有网站,不了解它是如何组合在一起的,那么很难回答这个问题。这是可行的,但我的问题是“measure_3712467”不是一个变量。它会在不知情的情况下发生变化,并且在每个代码中都会有所不同。我认为是这样的:\n你能把链接发送到网站吗?然后//text()[包含(,“首选项”)]/following::span[@class=“duty\u rate”][1]
应该是一种方法。这两种方法似乎都能奏效。我将用不同的代码和不同的国家测试它们,并更新我的问题。谢谢,我用同一个框架和同一个网页对它们进行了测试,并确保它们都能正常工作。第二个测试的结果至少与我测试的结果一致。我相信这会对他们所有人起作用。谢谢你的帮助
doc2.DocumentNode.SelectNodes("//div[@id='" + taric + "']//span[@class='duty_rate']")[1].InnerText