[英]Parsing specific data from a webpage
I am getting data from the this webpage using following code block. 我正在使用以下代码块从此网页获取数据。 I think this code block is not reasonable because I used to bordercolor of table.
我认为此代码块是不合理的,因为我曾经习惯于使用表格的bordercolor。 But I could not find different way to get data.
但是我找不到其他方法来获取数据。 Is there a different way because I am newbie in C#.
是否有其他方法,因为我是C#的新手。
Thanks for your help. 谢谢你的帮助。
foreach (HtmlNode node in document.DocumentNode.SelectNodes("//table[@bordercolor='#3366cc']/tr"))
{
sXPath = node.XPath + "/td[2]/font[1]";
htmlNode = document.DocumentNode.SelectSingleNode(sXPath);
if(htmlNode != null)
{
if (htmlNode.InnerText.Length >= 7)
{
string freq = htmlNode.InnerText.Substring(0, 5);
if (int.TryParse(freq, out intFrequency) == true)
{
string pol = htmlNode.InnerText.Substring(6, 1);
if (pol == "H")
bPolarity = false;
else if (pol == "V")
bPolarity = true;
}
}
}
sXPath = node.XPath + "/td[3]/font[1]";
htmlNode = document.DocumentNode.SelectSingleNode(sXPath);
if (htmlNode != null)
{
if (htmlNode.InnerText.Length >= 5)
{
string sr = htmlNode.InnerText.Substring(0, 5);
if (int.TryParse(sr, out intSymbolRate) == false)
{
sr = htmlNode.InnerText.Substring(0, 4);
int.TryParse(sr, out intSymbolRate);
}
}
}
}
Try using web client() or HTTP request for getting content from website which is much simpler 尝试使用Web client()或HTTP请求从网站获取内容,这要简单得多
Sample code: 样例代码:
string winPhoneGeekTweetsUrl = @"http://sss.sss78.sset/wsss.php";
WebClient webClient = new WebClient();
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
webClient.DownloadStringAsync(new Uri(winPhoneGeekTweetsUrl));
carry remaining function with Downloadstringcompleted
event handler 带有
Downloadstringcompleted
事件处理程序的其余功能
I have found different way and i think this is better and stronger. 我发现了不同的方式,我认为这是更好和更强的。
foreach (HtmlNode node in document.DocumentNode.SelectNodes("//table/tr/td/font"))
{
if (node.InnerText == "Freq PolMode" || node.InnerText == "SR-FEC")
{
sXPath = node.ParentNode.ParentNode.ParentNode.XPath + "//tr";
HtmlNodeCollection rows = document.DocumentNode.SelectNodes(sXPath);
for (int i = 0; i < rows.Count; i++)
{
sXPath = rows[i].XPath + "/td[2]/font[1]";
htmlNode = document.DocumentNode.SelectSingleNode(sXPath);
if (htmlNode != null)
{
if (htmlNode.InnerText.Length >= 7)
{
string freq = htmlNode.InnerText.Substring(0, 5);
if (int.TryParse(freq, out intFrequency) == true)
{
string pol = htmlNode.InnerText.Substring(6, 1);
if (pol == "H")
bPolarity = false;
else if (pol == "V")
bPolarity = true;
}
}
}
sXPath = rows[i].XPath + "/td[3]/font[1]";
htmlNode = document.DocumentNode.SelectSingleNode(sXPath);
if (htmlNode != null)
{
if (htmlNode.InnerText.Length >= 5)
{
string sr = htmlNode.InnerText.Substring(0, 5);
if (int.TryParse(sr, out intSymbolRate) == false)
{
sr = htmlNode.InnerText.Substring(0, 4);
int.TryParse(sr, out intSymbolRate);
}
}
}
}
break;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.