如何使用C#在网页源中的div中查找文本 [英] How do I find the text within a div in the source of a web page using C#

查看:42
本文介绍了如何使用C#在网页源中的div中查找文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从网站获取 HTML 代码,保存它并使用 LINQ 表达式查找一些文本?

How can I get the HTML code from a website, save it, and find some text by using a LINQ expression?

我正在使用以下代码来获取网页的来源:

I'm using the following code to get the source of a web page:


public static String code(string Url)
{
    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(Url);
    myRequest.Method = "GET";
    WebResponse myResponse = myRequest.GetResponse();
    StreamReader sr = new StreamReader(myResponse.GetResponseStream(),
        System.Text.Encoding.UTF8);
    string result = sr.ReadToEnd();
    sr.Close();
    myResponse.Close();
    
    return result;
}

如何在网页源中的div中查找文本?

How do I find the text within a div in the source of the web page?

推荐答案

从网站获取HTML代码.您可以使用如下代码:

Getting HTML code from a website. You can use code like this:

string urlAddress = "http://google.com";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

if (response.StatusCode == HttpStatusCode.OK)
{
    Stream receiveStream = response.GetResponseStream();
    StreamReader readStream = null;
    if (String.IsNullOrWhiteSpace(response.CharacterSet))
        readStream = new StreamReader(receiveStream);
    else
        readStream = new StreamReader(receiveStream,
            Encoding.GetEncoding(response.CharacterSet));
    string data = readStream.ReadToEnd();
    response.Close();
    readStream.Close();
}

这将为您提供从网站返回的 HTML .但是通过 LINQ 查找文本并不是那么容易.也许使用正则表达式会更好,但不能与 HTML 一起使用.

This will give you the returned HTML from the website. But find text via LINQ is not that easy. Perhaps it is better to use regular expression but that does not play well with HTML.

这篇关于如何使用C#在网页源中的div中查找文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆