第一个问题是:C#如何读取HTML网页的源文件。
第二个问题:如果HTML源码中有一部分如下所示,我想要把<h1>森林蜘蛛</h1>中的“森林蜘蛛”提取出来付给一个变量,应该怎么做啊
<div class="h1">
<h1>森林蜘蛛</h1>
<div class="clearfix"></div>
</div>
第二个问题:如果HTML源码中有一部分如下所示,我想要把<h1>森林蜘蛛</h1>中的“森林蜘蛛”提取出来付给一个变量,应该怎么做啊
<div class="h1">
<h1>森林蜘蛛</h1>
<div class="clearfix"></div>
</div>
解决方案 »
- datagridview导出EXCEL
- treeview控件,动态加载不显示横向滚动条???
- 在.NET环境下引用c++dll函数出现“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”错误
- C# 重构 封装字段 一次只能封装一个吗
- ASP.NET的WEB.CONFIG中allow标签的users和roles的区别
- biztalk
- C#如何调用老VB6的COM组件(DLL)
- Visual C#中的程序中如何给一个ASP.NET网站上传消息???
- datagridview中回车选择数据?
- C#公告抓取工具,读取PDF内存溢出错误
- 请教winform中用sendMessage出送鼠标单击另一个窗口的指定位置
- 怎么释放ActiveX的资源呀?
(?<=<div class="h1">\s*<h1>)\w*(?=</h1>)
有其它要求再加条件:)
// 通过节点名称以及节点的一个属性值在源码中过滤出相应的信息
public NodeList getNodeListByAttribute(string nodeName, string attributeName, string attributeValue, Parser paser)
{
NodeFilter nodeFilter = new TagNameFilter(nodeName);
NodeFilter nameFilter = new HasAttributeFilter(attributeName, attributeValue);
AndFilter andFilter = new AndFilter(nodeFilter, nameFilter);
NodeList nodeList = paser.ExtractAllNodesThatMatch(andFilter);
return nodeList;
}
// urlParam 网址,返回网页源代码
public string GetStringByResponse(string urlParam, string coding)
{
if (coding.Length == 0) // 如果传过来的编码类型为空 则默认为 UTF-8
coding = "UTF-8";
WebRequest request; // 实例一个请求对象
try
{
request = WebRequest.Create(urlParam); // 创建一个参数为下载页面的请求
}
catch (UriFormatException)
{
request = null;
}
request.Credentials = CredentialCache.DefaultCredentials; // 若服务器需要,则设置凭据 HttpWebResponse response = null; // 设置响应为null
Stream dataStream = null; // 设置服务器端的响应流为null
StreamReader reader = null;
string responseFromServer = null; // 从服务器中获得响应的字符串 try
{
response = (HttpWebResponse)request.GetResponse(); // 获得响应
if (response.StatusCode == HttpStatusCode.OK) // 请求成功
{
dataStream = response.GetResponseStream(); // 从响应中获得响应的流信息
reader = new StreamReader(dataStream, Encoding.GetEncoding(coding));
responseFromServer = reader.ReadToEnd(); // 从streamReader中读出响应流string,得出相应的具体内容
}
}
catch (WebException)
{
return null;
}
finally
{
if (reader != null)
reader.Close();
if (dataStream != null)
dataStream.Close();
if (response != null)
response.Close();
} return responseFromServer; ;
}
对了 getNodeListByAttribute()这个方法需要引用一个DLL文件 我的下载资源里面有
如果是自己写的html的话 就不用GetStringByResponse() 用IO流去读这个文件就行 然后以字符串形式返回html的源码
看不懂的话 Q联系 我Q 464582858
/// <summary>
/// 根据链接地址 获取 Html文本
/// </summary>
public static string Get_Html(string Url)
{
System.Net.WebClient wc = new System.Net.WebClient();
try
{
Byte[] pageData = wc.DownloadData(Url);
wc.Credentials = System.Net.CredentialCache.DefaultCredentials;
wc.Dispose();
return System.Text.Encoding.Default.GetString(pageData);
}
catch (Exception ex)
{
}
finally
{
}
return "";
}