[英]How to load a page after downloading without first saving it to a file
I'd like to parse the following in a webpage: 我想在网页中解析以下内容:
<h1 class="eTitle">bla bla bla v1.0</h1>
I want to display "bla bla bla v.1.0" in a textbox that I created using WPF. 我想在使用WPF创建的文本框中显示“ bla bla bla v.1.0”。 My code is the followind, but it display nothing in the textbox when I click the button. 我的代码是followind,但是当我单击按钮时,它在文本框中什么也不显示。
private void Button_Click_1(object sender, RoutedEventArgs e)
{
WebClient webClient = new WebClient();
webClient.Encoding = Encoding.UTF8;
webClient.DownloadFile("http://blablabla.com", "blabla.htm");
HtmlDocument htmldoc = new HtmlDocument();
htmldoc.Load("blabla.htm");
var titlenode = htmldoc.DocumentNode.SelectSingleNode("blabla");
textbox1.Text = "" + titlenode;
}
private void textbox1_TextChanged(object sender, TextChangedEventArgs e)
{
}
Actually I'm saving the page into a .htm file and reading from it. 实际上,我将页面保存到.htm文件中并从中读取。 Can I avoid doing this? 我可以避免这样做吗?
为了避免下载文件,可以使用webClient.DownloadString("http://blablabla.com/blabla.htm");
You're XPath expression to get the node is not correct. 您在XPath表达式中获取的节点不正确。
If you want to get single h1
node use this 如果要获取单个h1
节点,请使用此
var titlenode = htmldoc.DocumentNode.SelectSingleNode("//h1");
If you want to get single h1
with title eTitle
node use this 如果要获取带有标题eTitle
节点的单个h1
, eTitle
使用此
var titlenode = htmldoc.DocumentNode.SelectSingleNode("//h1[@title = 'eTitle']");
For more see this page . 有关更多信息,请参见此页面 。
Then you have to access the node value and display it. 然后,您必须访问并显示节点值。
Why not just use an HttpWebRequest
to pull the html
file itself? 为什么不只使用HttpWebRequest
来提取html
文件本身呢?
Source: Get html source of web page using HttpWebRequest class 源: 使用HttpWebRequest类获取网页的html源
private string getHtml(string url)
{
if(String.IsNullOrWhiteSpace(url)) { return; }
// Create Web Request
HttpWebRequest myWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
// Set GET method
myWebRequest.Method = "GET";
// Get a response
HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();
// Open a Stream to read the response
StreamReader myWebSource = new StreamReader(myWebResponse.GetResponseStream());
// Create a string to store the response
string myPageSource = string.Empty;
myPageSource= myWebSource.ReadToEnd();
// Close the stream
myWebResponse.Close();
// Return the string
return myPageSource;
}
Then using Google homepage as an example, you call string htmlPage = getHtml("http://www.google.ca");
然后以Google主页为例,您调用string htmlPage = getHtml("http://www.google.ca");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.