我想用html敏捷包解析html表。我只想從表中提取一些預定義的列數據。HTML敏捷包
但我是新來的解析和HTML敏捷包,我試過了,但我不知道如何使用HTML敏捷包爲我的需要。
如果有人知道然後給我舉例來說,如果可能的
編輯:
是否可以解析HTML表格一樣,如果我們只想提取的決定列名數據?就像有4列名稱,地址,phno一樣,我只想提取姓名和地址數據。
我想用html敏捷包解析html表。我只想從表中提取一些預定義的列數據。HTML敏捷包
但我是新來的解析和HTML敏捷包,我試過了,但我不知道如何使用HTML敏捷包爲我的需要。
如果有人知道然後給我舉例來說,如果可能的
編輯:
是否可以解析HTML表格一樣,如果我們只想提取的決定列名數據?就像有4列名稱,地址,phno一樣,我只想提取姓名和地址數據。
在討論論壇here中有一個例子。向下滾動一下以查看錶格答案。我希望他們能提供更容易找到的更好的樣本。
編輯: 要從特定列中提取數據,您必須首先找到與您想要的列對應的<th>
標記並記住它們的索引。然後您需要爲相同的索引找到<td>
標籤。假設你知道列的索引,你可以做這樣的事情:
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml("http://somewhere.com");
HtmlNode table = doc.DocumentNode.SelectSingleNode("//table");
foreach (var row in table.SelectNodes("//tr"))
{
HtmlNode addressNode = row.SelectSingleNode("td[2]");
//do something with address here
HtmlNode phoneNode = row.SelectSingleNode("td[5]");
// do something with phone here
}
EDIT2: 如果你不知道列的索引,你可以做這樣整個事情。我沒有測試過這個。
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml("http://somewhere.com");
var tables = doc.DocumentNode.SelectNodes("//table");
foreach(var table in tables)
{
int addressIndex = -1;
int phoneIndex = -1;
var headers = table.SelectNodes("//th");
for (int headerIndex = 0; headerIndex < headers.Count(); headerIndex++)
{
if (headers[headerIndex].InnerText == "address")
{
addressIndex = headerIndex;
}
else if (headers[headerIndex].InnerText == "phone")
{
phoneIndex = headerIndex;
}
}
if (addressIndex != -1 && phoneIndex != -1)
{
foreach (var row in table.SelectNodes("//tr"))
{
HtmlNode addressNode = row.SelectSingleNode("td[addressIndex]");
//do something with address here
HtmlNode phoneNode = row.SelectSingleNode("td[phoneIndex]");
// do something with phone here
}
}
}
@Harikrishna - 它是每個表中的同一種數據嗎?你想從所有表中提取相同的列嗎?你只想找到一個特定的表?在這裏幫我一把。我一直試圖回答,然後提供更多信息。讓我們瞭解所有的信息。 – 2010-03-11 10:04:40
@Mike Two Sir ..好吧,對不起...就像在網頁上有不止一個表格標籤,但我想從只有一個表格中提取數據,這些表格有我們定義的列名稱,如地址和電話號碼。其他表標籤是用於其他信息,而不是有用的。 – Harikrishna 2010-03-11 10:08:02
@Mike Two Sir ..有很多網頁有多個表格。而且我想從每個網頁中提取只有一個表格的數據,這個表格的名字是電話號碼和地址。 – Harikrishna 2010-03-11 10:09:57
@Harikrishna - 你有一個表結構的小樣本? – 2010-03-11 09:32:07
有關使用html Agility pack從html數據中提取數據的更多信息:http://stackoverflow.com/questions/2431652/html-agility-pack – Harikrishna 2010-03-13 10:09:07