C# 4.0 读取多个Div';带HtmlAgilityPack的s

C# 4.0 读取多个Div';带HtmlAgilityPack的s,c#-4.0,web-scraping,html-agility-pack,C# 4.0,Web Scraping,Html Agility Pack,我试图从2个不同的分区中提取数据,但我只能从第一个分区(城市)获取数据。我以wiki页面中的代码设置为例,其中所有li元素都来自H2 id=cities和id=Other\u目的地: var xpathData = "//h2[span/@id='Cities' or @id='Other_destinations']" + "/following-sibling::ul[1]" + "/li"; 然后,我把李家的东西都写进文本文件 private void button1_Click(obj

我试图从2个不同的分区中提取数据,但我只能从第一个分区(城市)获取数据。我以wiki页面中的代码设置为例,其中所有li元素都来自
H2 id=cities
id=Other\u目的地:

var xpathData = "//h2[span/@id='Cities' or @id='Other_destinations']" + "/following-sibling::ul[1]" + "/li";
然后,我把李家的东西都写进文本文件

private void button1_Click(object sender, EventArgs e)
    {

        List<string> destinations = new List<string>();
        var xpathData = "//h2[span/@id='Cities' or @id='Other destinations']" + "/following-sibling::ul[1]" + "/li";

        WebClient web = new WebClient();
        String html = web.DownloadString("http://wikitravel.org/en/Germany");

        hap.HtmlDocument doc = new hap.HtmlDocument();
        doc.LoadHtml(html);

        using (StreamWriter write = new StreamWriter(@"C:\path\testText.txt"))
        { 
            foreach (hap.HtmlNode node in doc.DocumentNode.SelectNodes(xpathData)) 
            {

            string all = node.InnerText;

            //Writes to text file
            write.WriteLine(all);
            }
        }

      }
private void按钮1\u单击(对象发送者,事件参数e)
{
列表目的地=新列表();
var xpathData=“//h2[span/@id='Cities'或@id='Other destinations']”“+”/以下同级::ul[1]“+”/li”;
WebClient web=新的WebClient();
字符串html=web.DownloadString(“http://wikitravel.org/en/Germany");
hap.HtmlDocument doc=新的hap.HtmlDocument();
doc.LoadHtml(html);
使用(StreamWriter write=newstreamwriter(@“C:\path\testText.txt”))
{ 
foreach(doc.DocumentNode.SelectNodes(xpathData)中的hap.HtmlNode节点)
{
string all=node.InnerText;
//写入文本文件
write.WriteLine(全部);
}
}
}
关于
'hap'
,我必须使用
hap=HtmlAgilityPack由于一些奇怪的冲突


谢谢你的帮助/建议/指导

原始代码中的第二个id输入错误:

var xpathData = "//h2[span/@id='Cities' or span/@id='Other_destinations']" + "/following-sibling::ul[1]" +
                        "/li";
这是我使用的代码:

var destinations = new List<string>();
var xpathData = "//h2[span/@id='Cities' or span/@id='Other_destinations']" + "/following-sibling::ul[1]" +
                        "/li";

var webClient = new WebClient();
var html = webClient.DownloadString("http://wikitravel.org/en/Germany");

// to control the encoding 
var doc = new HtmlDocument
{
    OptionDefaultStreamEncoding = Encoding.UTF8
};

doc.LoadHtml(html);

using (var write = new StreamWriter("testText.txt"))
{
   foreach (var node in doc.DocumentNode.SelectNodes(xpathData))
   {
       var all = node.InnerText;

       //Writes to text file
       write.WriteLine(all);
   }

}       
var destinations=newlist();
var xpathData=“//h2[span/@id='Cities'或span/@id='Other_destinations']”+“/以下同级::ul[1]”+
“/li”;
var webClient=新的webClient();
var html=webClient.DownloadString(“http://wikitravel.org/en/Germany");
//控制编码
var doc=新的HtmlDocument
{
optionDefaultStreamEncode=Encoding.UTF8
};
doc.LoadHtml(html);
使用(var write=newstreamwriter(“testText.txt”))
{
foreach(doc.DocumentNode.SelectNodes(xpathData)中的var节点)
{
var all=node.InnerText;
//写入文本文件
write.WriteLine(全部);
}
}       
使用工作解决方案更新
所以现在的问题是一些国家有奇怪的加价。大多数Div设置为:

<h2>
<span id="cities"></span>
</h2>
<ul>
<li>...</li>
<li>...</li>
...
</ul>
<h2>
...
</h2>
此查询仅用于以上述HTML格式从网页中获取两部分信息。一个重要的注意事项是需要对文本进行编码,否则它将以“-”作为“–欧元”打印成文本。我为web客户端添加了以下编码:

var web = new WebClient();
web.Encoding = System.Text.Encoding.UTF8;
String html = string.Empty;
html = //get URL's
文件的编码如下:

var doc = new hap.HtmlDocument
{
    OptionDefaultStreamEncoding = Encoding.UTF8
};

doc.LoadHtml(html);

接得好!不幸的是,它并没有完全解决这个问题。现在它只出来抓取Div中的第一个li…我已经测试了你的代码,在我的机器上运行良好。我将用我的代码编辑我的答案。请运行它。嗯,你是对的。这段代码工作得很好。我已经修改了这个部分正在使用的代码,它只从“Other_destinations”分区获取第一个li。。。一定是在别的地方被绊倒了。。。谢谢。我相信问题在于维基旅行代码的格式。再次感谢!如果需要,您可以在WebClient中指定编码,也可以使用如下内容:
xpath=“//div[@id='div']]/child::ul/li”
,我建议您阅读,这确实非常有用。如果您有更多问题,请将其发布以正确回答。我希望这对你有帮助哦,很抱歉回答这个问题。当我找到解决方案时,我正打算重新格式化它。我尝试使用child,但它只返回一个空文本文档。看,发布这个问题,并在注释中让我知道,这样可以显示我的代码,因为在这里我无法发布大型文档code@Vkt0r我提出了一个新问题。谢谢。以上答案来自
var web = new WebClient();
web.Encoding = System.Text.Encoding.UTF8;
String html = string.Empty;
html = //get URL's
var doc = new hap.HtmlDocument
{
    OptionDefaultStreamEncoding = Encoding.UTF8
};

doc.LoadHtml(html);