• 获取网页源码
 ///通过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. hihocoder 1631

    时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 There are many homeless cats in PKU campus. They are all happy ...

  2. cs实时系统之网关设计

    今天给大家讲一下client-server系统(cs)设计,基本结构 1.client 客户端,插件式开发,负责对应ui的展示 2.gateway 网关层,管理客户端通信连接,负载后端集群服务 3.s ...

  3. webpack remove console.log

    webpack remove console.log https://stackoverflow.com/questions/41040266/remove-console-logs-with-web ...

  4. .NET 面试题: C# override && overloading (C# 覆写 && 重载)

    1 1 1 .NET 面试题, C# ,override , overloading, 覆写, 重载,.NET,ASP.NET, override (覆写/重写): 方法名相同,参数的个数和类型相同, ...

  5. React Native & Fast Refresh

    React Native & Fast Refresh 0.61 https://reactnative.dev/blog/2019/09/18/version-0.61/ Fast Refr ...

  6. WoT

    WoT IoT / AIoT Web of Things (WoT) Architecture W3C Recommendation 9 April 2020 https://www.w3.org/T ...

  7. How to use JavaScript to implement precise setTimeout and setInterval

    How to use JavaScript to implement precise setTimeout and setInterval 如何使用 JavaScript 实现精确的 setTimeo ...

  8. import script module

    import script module .mjs <script type="module"> import {addTextToBody} from './util ...

  9. shit 钉钉

    shit 钉钉 钉钉 圈子 入口, 没有 https://www.dingtalk.com/qidian/help-detail-1000131196.html shit bug 全员圈 这个好像是要 ...

  10. NGK福利再升级,1万枚VAST限时免费送

    NGK在推出持有算力获得SPC空投活动后,福利再升级,于美国加州时间2021年2月8日下午4点推出新人福利活动,注册NGK成为新会员,即可获得0.2枚VAST奖励. VAST免费福利送活动仅送出1万枚 ...