• 获取网页源码
 ///通过HttpWebResponse
public string GetUrlHtml(string url)
{ string strHtml = string.Empty; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse();//从Internet资源返回数据流 if (response.CharacterSet.ToLower() == "gbk")
{
using (Stream respStream = response.GetResponseStream())//读取数据流
{
using (StreamReader str = new StreamReader(respStream, Encoding.GetEncoding("gb2312")))//读取数据
{
strHtml = str.ReadToEnd();
}
}
}
else
{
using (Stream respStream = response.GetResponseStream())//读取数据流
{
using (StreamReader str = new StreamReader(respStream, Encoding.UTF8))//读取数据
{
strHtml = str.ReadToEnd();
}
}
}
return strHtml;
}
///通过WebClient
private static string htmlcontent(string url)
{
System.Net.WebClient wc = new System.Net.WebClient();
Byte[] pageData = wc.DownloadData(url);
string s = System.Text.Encoding.Default.GetString(pageData);
////s = System.Text.Encoding.UTF8.GetString(pageData);去除中文乱码
return s;
}
  • 获取网页元素(HtmlAgilityPack)
using HtmlAgilityPack;
//HtmlNode node = doc.DocumentNode.SelectSingleNode("Xpath"); 元素
//HtmlNodeCollection nodes = doc1.DocumentNode.SelectNodes(Xpath); //元素集合
// private static Menu Level_chidren_Menu(Menu menu1,string Xpath)
{
foreach (var item in menu1.ChildMenus)
{
HtmlDocument doc1 = new HtmlDocument();//1步:声明
doc1.LoadHtml(HttpHelper.GetUrlHtml(item.MenuUrl)); //加载HTML源码
HtmlNodeCollection nodes = doc1.DocumentNode.SelectNodes(Xpath);//取节点元素
foreach (var page in nodes)
{
item.ChildMenus.Add(new Menu() {
LevelMenu = 3,
MenuName = page.InnerText,
MenuUrl = HttpHelper.baseUrl + HttpHelper.GetHtmlAttribute(page.InnerHtml, "a").Attributes["href"].Value
});
}
}
return menu1;
}

  

C# 获取网页信息的更多相关文章

  1. C# HttpWebRequest 绝技 根据URL地址获取网页信息

    如果要使用中间的方法的话,可以访问我的帮助类完全免费开源:C# HttpHelper,帮助类,真正的Httprequest请求时无视编码,无视证书,无视Cookie,网页抓取 1.第一招,根据URL地 ...

  2. 使用URLConnection获取网页信息的基本流程

    参考自core java v2, chapter3 Networking. 注:URLConnection的子类HttpURLConnection被广泛用于Android网络客户端编程,它与apach ...

  3. 使用URLConnection获取网页信息的基本流程 分类: H1_ANDROID 2013-10-12 23:51 3646人阅读 评论(0) 收藏

    参考自core java v2, chapter3 Networking. 注:URLConnection的子类HttpURLConnection被广泛用于Android网络客户端编程,它与apach ...

  4. C#获取网页信息并存入数据库

    1,获取以及商品分类信息 给一网页获取网页上商品信息的分类 using Skay.WebBot; using System; using System.Collections.Generic; usi ...

  5. C#获取网页信息核心方法(入门一)

    目录:信息采集入门系列目录 下面记录的是我自己整理的C#请求页面核心类,主要有如下几个方法 1.HttpWebRequest Get请求获得页面html 2.HttpWebRequest Post请求 ...

  6. python获取网页信息的三种方法

    import urllib.request import http.cookiejar url = 'http://www.baidu.com/' # 方法一 print('方法一') req_one ...

  7. 获取网页上数据(图片、文字、视频)-b

    Demo地址:http://download.csdn.net/detail/u012881779/8831835 获取网页上所有图片.获取所有html.获取网页title.获取网页内容文字... . ...

  8. Python 获取车票信息

    提示:该代码仅供学习使用,切勿滥用!!! 先来一个git地址:https://gitee.com/wang_li/li_wang 效果图: 逻辑: 1.获取Json文件的内容 2.根据信息生成URL ...

  9. Powershell抓取网页信息

    一般经常使用invoke-restmethod和invoke-webrequest这两个命令来获取网页信息,如果对象格式是json或者xml会更容易 1.invoke-restmethod 我们可以用 ...

随机推荐

  1. springboot(五)Scheduling demo

    在项目开发过程中,经常会使用到定时任务(跑批),springboot默认已经实现了,只需要添加相应的注解就可以实现 在启动类上加入注解,开启定时任务 @SpringBootApplication @E ...

  2. online QRcode generator , QRcode=== (Quick Response Code) , 二维条码,二维码,彩色二维码,图片二维码,

    online QRcode generator ,  QRcode=== (Quick Response Code)    , 二维条码,二维码,彩色二维码,图片二维码, 1 http://cli.i ...

  3. CSS hover box

    CSS hover box transition 踩坑指南, display: none; 作为初始状态,不会产生动画效果,必须设置 height: 0; 或 width: 0; 来实现隐藏! tra ...

  4. TypeORM Entity

    TypeORM Entity Entity Entity is a class that maps to a database table when using SQL database(or col ...

  5. css border-radius & yin-yang & taiji

    css border-radius & yin-yang & taiji solution css border-radius & tabs effect https://co ...

  6. calendar merge date

    calendar merge date componentDidMount () { const { monthDays, // monthDates, } = this.props; const d ...

  7. SVG Tutorials

    SVG Tutorials W3C https://www.w3.org/TR/SVG11/ https://www.w3.org/TR/SVG2/ https://www.w3.org/TR/svg ...

  8. qt 移动窗口MoveWindow

    RECT r; GetWindowRect(this->gameHwnd, &r); // 获取窗口的宽度和高度 int nWidth = r.right - r.left; int n ...

  9. 一文助你了解NGK商城

    按照NGK的发展逻辑,将会在2021年上半年上线链商商城,解决传统消费行业真伪难辨的弊端,之后,将会推出小额支付功能,让NGK真正成为结算中的数字资产,目前两者落地性应用已经在开发内测阶段中,期初的部 ...

  10. postman功能介绍