简体   繁体   English

下载后如何加载页面而不先将其保存到文件

[英]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节点的单个h1eTitle使用此

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.

相关问题 文件下载到浏览器后如何修改页面? - How can I modify a page after a file is finished downloading to the browser? 保存文件后刷新页面 - refreshing a page after saving a file 下载并保存Excel文件 - Downloading and saving excel file 下载文件后指向新页面 - Directing to a new page after downloading a file 如何在不先加载到RAM的情况下将文件加载到Blob? - how to load a file into a blob without loading into RAM first? downloader.DownloadFileTaskAsync 下载第一个文件后卡住 - downloader.DownloadFileTaskAsync stuck after downloading the first file 使用xml.load(url)下载xml文件和先下载文件,然后保存然后加载它有什么区别? - What's the difference between downloading xml file using xml.load(url) and downloading the file first, save it and then load it? 将byte []作为文件打开而不首先将其实际保存为文件 - Opening byte[] as a file without actually saving it as a file first 如何将内存位图保存到 ZipArchive 而不先将位图保存到文件系统? - How do I save an in memory Bitmap to a ZipArchive without saving the Bitmap to the file system first? 如何在不先保存本地的情况下将文件直接流式传输到C#中的S3 - How to stream a file directly to S3 in C# without saving locally first
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM