获取HTML code,从网站在C# [英] Get HTML code from website in C#

查看:206
本文介绍了获取HTML code,从网站在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从网站获取HTML code,保存它,并找到一个LINQ前pression一些文本?

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

我用下面的code得到一个网页的源代码:

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 in a div in the source of the web page?

推荐答案

HTML入门$ C $从c网站。你可以用code这样的。

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 (response.CharacterSet == null)
  {
     readStream = new StreamReader(receiveStream);
  }
  else
  {
     readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
  }

  string data = readStream.ReadToEnd();

  response.Close();
  readStream.Close();
}

这会给你返回的 HTML $ C $从c网站。但通过查找文本的 LINQ 不是那么容易。
也许这是更好地使用常规的前pression但不发挥好 HTML code

This will give you the returned HTML code 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 code

这篇关于获取HTML code,从网站在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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