我试图从这个网站标签如何使用C#
sometext
提取文本,我有这样的代码来提取网页数据:
using System;
using System.Net;
using HtmlAgilityPack;
namespace GC_data_console
{
class Program
{
public static void Main(string[] args)
{
using (var client = new WebClient())
{
// Download the HTML
string html = client.DownloadString("https://www.requestedwebsite.com");
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
foreach(HtmlNode link in
doc.DocumentNode.SelectNodes("//span"))
{
HtmlAttribute href = link.Attributes["id='example1'"];
if (href != null)
{
Console.WriteLine(href.Value.ToString());
Console.ReadLine();
}
}
}
}
}
}
}
但我仍然没有得到文字“sometext”。
但是,如果我插入HtmlAttribute href = link.Attributes [“id”]; 我会得到所有的ID名称。
我在做什么错了?
您可以分享您试图获取内容的实际URL吗?你也试图获得'HtmlAttribute'的值而不是元素。你需要尝试获得的是'link.InnerText'。 –
你好,例如从这个网页https://www.geocaching.com/geocache/GC257YR_slivercup-studios-east ,我想从标签中获取文本: SliverCup Studios East – Shiwers
知道了....你尝试了我建议的另一种方式吗?你是否也调试过并检查你是否获得了正确的元素? –