C#获取网页信息核心方法(入门一)
目录:信息采集入门系列目录
下面记录的是我自己整理的C#请求页面核心类,主要有如下几个方法
1.HttpWebRequest Get请求获得页面html
2.HttpWebRequest Post请求获得页面html
3.模拟登录获得cookie内容
4.模拟登录获得cookie字符串
5.代理的设置
6.利用webbrowser 获取js生成的页面
7.为webbrowser设置cookie,模拟登录
8.使用demo
HttpWebRequest Get请求获得页面html
注意点:以前抓取觉得很慢,最后发现是代理的问题,没有代理就设置为null,这样就不用每次去找代理,影响执行效率,还有一些参数可以自习设置,比如模拟浏览器等。
/// <summary>
/// get请求获得页面的html
/// </summary>
/// <param name="url">需要获取的url</param>
/// <param name="proxy">代理,没有设置为null,不然每次去读代理造成请求很慢</param>
/// <param name="cookie">该网站所需要的cookie</param>
/// <param name="timeout">超时时间</param>
/// <returns>页面请求后的html</returns>
public static string Crawl(string url, WebProxy proxy, CookieContainer cookie, int timeout = )
{
string result = string.Empty;
HttpWebRequest request = null;
WebResponse response = null;
StreamReader streamReader = null;
try
{
request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Proxy = proxy;
request.Timeout = timeout;
request.AllowAutoRedirect = true;
request.CookieContainer = cookie;
response = (HttpWebResponse)request.GetResponse();
streamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
result = streamReader.ReadToEnd(); }
catch (Exception ex)
{
throw ex;
}
finally
{
if (request != null)
{
request.Abort();
}
if (response != null)
{
response.Close();
}
if (streamReader != null)
{
streamReader.Dispose();
}
} return result;
}
HttpWebRequest Post请求获得页面html
/// <summary>
/// post请求获得页面
/// </summary>
/// <param name="url">需要获取的url</param>
/// <param name="postdata">post的数据字符串,如id=1&name=test</param>
/// <param name="proxy">代理</param>
/// <param name="cookie">coolie</param>
/// <param name="timeout">超时</param>
/// <returns></returns>
public static string Crawl(string url, string postdata,WebProxy proxy, CookieContainer cookie, int timeout = )
{
string result = string.Empty;
HttpWebRequest request = null;
WebResponse response = null;
StreamReader streamReader = null;
try
{
request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Proxy = proxy;
request.Timeout = timeout;
request.AllowAutoRedirect = true;
request.CookieContainer = cookie; byte[] bs = Encoding.ASCII.GetBytes(postdata);
string responseData = String.Empty;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bs.Length;
using (Stream reqStream = request.GetRequestStream())
{
reqStream.Write(bs, , bs.Length);
reqStream.Close();
}
response = (HttpWebResponse)request.GetResponse();
streamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
result = streamReader.ReadToEnd(); }
catch (Exception ex)
{
throw ex;
}
finally
{
if (request != null)
{
request.Abort();
}
if (response != null)
{
response.Close();
}
if (streamReader != null)
{
streamReader.Dispose();
}
} return result;
}
模拟登录获得cookie内容
先找到登录的页面,分析登录页面的post参数和链接,获得cookie后可以直接传到上面的方法
/// <summary>
///根据模拟请求页面获得cookie
/// </summary>
/// <param name="url">模拟的url</param>
/// <returns>cookie</returns>
public static CookieContainer GetCookie(string url, WebProxy proxy, int timeout = )
{
HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
CookieContainer cc = new CookieContainer();
request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Proxy = proxy;
request.Timeout = timeout;
request.AllowAutoRedirect = true;
request.CookieContainer = cc;
response = (HttpWebResponse)request.GetResponse();
response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
return cc;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (request != null)
{
request.Abort();
}
if (response != null)
{
response.Close();
}
} }
模拟登录获得cookie字符串
/// <summary>
/// 获得cookie字符串,webbrowser可以使用
/// </summary>
/// <param name="url"></param>
/// <param name="proxy"></param>
/// <param name="timeout"></param>
/// <returns></returns>
public static string GetCookieString(string url, WebProxy proxy, int timeout = )
{
HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
CookieContainer cc = new CookieContainer();
request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Proxy = proxy;
request.Timeout = timeout;
request.AllowAutoRedirect = true;
request.CookieContainer = cc;
response = (HttpWebResponse)request.GetResponse();
response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);
return strcrook; }
catch (Exception ex)
{
throw ex;
}
finally
{
if (request != null)
{
request.Abort();
}
if (response != null)
{
response.Close();
}
}
}
代理的设置
/// <summary>
/// 创建代理
/// </summary>
/// <param name="port">代理端口</param>
/// <param name="user">用户名</param>
/// <param name="password">密码</param>
/// <returns></returns>
public static WebProxy CreatePorxy(string port, string user, string password)
{
WebProxy proxy = new WebProxy();
proxy.Address = new Uri(port);
proxy.Credentials = new NetworkCredential(user, password);
return proxy;
}
利用webbrowser 获取js生成的页面
说明:由于不知道页面什么时候执行完成,这里是等待5s,默认执行完成,效率有待提高。
另外执行需要线程安全添加[STAThread]
/// <summary>
/// 抓取js生成的页面
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static string CrawlDynamic(string url)
{
WebBrowser browser = new WebBrowser(); browser.ScriptErrorsSuppressed = true; browser.Navigate(url); //先要等待加载完毕
while (browser.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
} System.Timers.Timer timer = new System.Timers.Timer(); var isComplete = false; timer.Elapsed += new System.Timers.ElapsedEventHandler((sender, e) =>
{
//加载完毕
isComplete = true; timer.Stop();
}); timer.Interval = * ; timer.Start(); //继续等待 5s,等待js加载完
while (!isComplete)
Application.DoEvents(); var htmldocument = browser.Document;
return htmldocument.ActiveElement.InnerHtml;
}
为webbrowser设置cookie,模拟登录
刚开始始终不成功以为这个方法不能用,后面发现原来是doain设置有问题,我的例子是www.aa.xxx.com,设置的为http://xx.com可以使用,这个地方可能需要根据自己的情况来选择域名。
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData); /// <summary>
/// 为webbrowser设置cookie
/// </summary>
/// <param name="cookieStr">cookie字符串,可以从上面方法获得</param>
/// <param name="domain">需要设置的域名</param>
public static void SetCookie(string cookieStr,string domain)
{
foreach (string c in cookieStr.Split(';'))
{
string[] item = c.Split('=');
if (item.Length == )
{
string name = item[];
string value = item[];
InternetSetCookie(domain, name, value);
} }
}
使用demo
//代理,没有就直接传null
WebProxy proxy = WebCrawl.WebRequestHelper.CreatePorxy("xx.com", "user", "password"); //根据登录页得到cookie
CookieContainer cookie = WebCrawl.WebRequestHelper.GetCookie("http://xxxx.login.com", proxy); //获取页面
string html = WebCrawl.WebRequestHelper.Crawl("http://xxx.index.com", proxy, cookie); //根据登录页得到cookie字符串
string cookiestr = WebCrawl.WebRequestHelper.GetCookieString("http://xxxx.login.com", proxy); //为webbrowser设置cookie
WebCrawl.WebRequestHelper.SetCookie(cookiestr, "https://xx.com"); //获取需要登录切用js生成的页面,当然普通页面也可以
string htmlWithJs = WebCrawl.WebRequestHelper.CrawlDynamic("http://xxx.index.com");
C#获取网页信息核心方法(入门一)的更多相关文章
- C# HttpWebRequest 绝技 根据URL地址获取网页信息
如果要使用中间的方法的话,可以访问我的帮助类完全免费开源:C# HttpHelper,帮助类,真正的Httprequest请求时无视编码,无视证书,无视Cookie,网页抓取 1.第一招,根据URL地 ...
- 使用URLConnection获取网页信息的基本流程
参考自core java v2, chapter3 Networking. 注:URLConnection的子类HttpURLConnection被广泛用于Android网络客户端编程,它与apach ...
- PHP版微信第三方实现一键登录及获取用户信息的方法
本文实例讲述了PHP版微信第三方实现一键登录及获取用户信息的方法.分享给大家供大家参考,具体如下: 注意,要使用微信在第三方网页登录是需要“服务号”才可以哦,所以必须到官方申请. 一开始你需要进入微信 ...
- 使用URLConnection获取网页信息的基本流程 分类: H1_ANDROID 2013-10-12 23:51 3646人阅读 评论(0) 收藏
参考自core java v2, chapter3 Networking. 注:URLConnection的子类HttpURLConnection被广泛用于Android网络客户端编程,它与apach ...
- JS获取网页宽高方法集合
JS获取网页宽高等方法的集合:document.body.clientWidth - 网页可见区域宽document.body.clientHeight - 网页可见区域高 document.body ...
- 在php中分别使用curl的post提交数据的方法和get获取网页数据的方法
在php中分别使用curl的post提交数据的方法和get获取网页数据的方法整理分享一下额,具体代码如下: (1)使用php curl获取网页数据的方法: $ch=curl_init(); //设置选 ...
- Oracle 和 SQLSERVER 重新获取统计信息的方法
1. Oracle 重新获取统计信息的命令 exec dbms_stats.gather_schema_stats(ownname =>) # 需要修改 ownername options 指定 ...
- JavaScript获取浏览器信息的方法
Window有navigator对象让我们得知浏览器的全部信息.我们可以利用一系列的API函数得知浏览器的信息. JavaScript代码如下: ? 1 2 3 4 5 6 7 8 9 10 11 1 ...
- C#获取网页信息并存入数据库
1,获取以及商品分类信息 给一网页获取网页上商品信息的分类 using Skay.WebBot; using System; using System.Collections.Generic; usi ...
随机推荐
- 分析一个贴图社交app的失败原因:FORK(相机)
FORK(相机)是一个通过分享图片来建立社交的app,它有着鲜明的配色,还算不错的贴图创新,细腻的产品设计,但是由于产品定位不清晰.设计亮点不多以及推广不利,从2014年5月第一版开始就没有火过.所以 ...
- 35 个你也许不知道的 Google 开源项目
转载自:http://blog.csdn.net/cnbird2008/article/details/18953113 Google是支持开源运动的最大公司之一,它们现在总共发布有超过500个的开源 ...
- 用pip命令安装Python第三方库
一.准备工作 1. 安装pip (1)下载 pip下载地址:https://pypi.python.org/pypi/pip#downloads (2)安装 下载后解压,控制台下进入解压后的目录,运行 ...
- ZooKeeper动态配置(十四)
概述 在3.5.0发行之前,ZK的全体成员和所有其它的配置参数是静态加载的在启动的时候并且在运行的时候不可变.操作员诉诸于"滚动重启" - 一个手动密集和改变配置文件容易出错的方法 ...
- 51nod 1873 高精度计算
JAVA BigDecimal import java.util.*; import java.math.*; public class Main { public static void main( ...
- 【HNOI】矩阵染色 数论
[题目描述]一个2*i的矩阵,一共有m种颜色,相邻两个格子颜色不能相同,m种颜色不必都用上,f[i]表示这个答案,求Σf[i]*(2*i)^m (1<=i<=n)%p. [数据范围] 20 ...
- react组件之间的几种通信情况
组件之间的几种通信情况 父组件向子组件通信 子组件向父组件通信 跨级组件通信 没有嵌套关系组件之间的通信 1,父组件向子组件传递 React数据流动是单向的,父组件向子组件通信也是最常见的;父组件通过 ...
- Android控件——监听按钮的点击事件
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAroAAAFTCAIAAABZPDiZAAAgAElEQVR4nOy9918UWfb///1jdu2uBs
- 通过JDBC连接HiveServer2
如果通过JDBC连接HiveServer2时提示:User: hive is not allowed to impersonate hive,需要在core-site.xml中新增如下配置: hado ...
- Caffe学习笔记3
Caffe学习笔记3 本文为原创作品,未经本人同意,禁止转载,禁止用于商业用途!本人对博客使用拥有最终解释权 欢迎关注我的博客:http://blog.csdn.net/hit2015spring和h ...