在这里,我试图从 HTML 页面中提取一个词。
例如,有两个文本框(1 和 2)。现在我正在尝试在 textbox1 上提供 stackoverflow 问题 ID,并在 textbox2 上获取“询问”值。
例如,如果我在 textbox1 上给出 36,这应该在 textbox2 上给我“9 年 4 个月前”。
WebClient 网页 = new WebClient();
String html = webpage.DownloadString("https://stackoverflow.com/questions/"+ textBox1.Text);
MatchCollection match = Regex.Matches(html, FILTERHERE, RegexOptions.Singleline);
问题是我不知道如何过滤我的输出(FILTERHERE)?
另外,如何将我的输出发送到 textbox2?
最佳答案
与 HtmlAgilityPack .
string url = "https://stackoverflow.com/questions/";
var web = new HtmlWeb();
var doc = web.Load(url + textBox1.Text); //the text is "36"
var tag = doc.DocumentNode.SelectSingleNode("//*[@id='qinfo']//td[./p[@class='label-key' and text()='asked']]/following-sibling::td//b");
textBox2.Text = tag.InnerText;
如果您不知道 XPath,Chrome 和 Firefox 的浏览器扩展可以为您获取任何 Html 标记的 XPath(我个人手动编写它们以使其对页面结构的变化不太敏感)。
关于c# - 如何从 HTML 页面中提取一个或多个单词 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47843967/