根据抓取的页面,用正则来匹配页面href和src

string UserAgent = "Mozilla/5.0 (Windows NT 5.2; rv:29.0) Gecko/20100101 Firefox/29.0";
string ContentType = ""; Uri strReqUrl = new Uri("http://m.lhrb.ufstone.net/");
protected void Application_BeginRequest(object sender, EventArgs e)
{ Uri u = new Uri(strReqUrl, Request.RawUrl);
byte[] b = getVerificationCode(u); //MemoryStream ms = new MemoryStream(b);
//Response.ClearContent();
//Response.ContentType = ContentType;
//Response.BinaryWrite(b); StringBuilder strHtml = new StringBuilder(Encoding.GetEncoding("gb2312").GetString(b));
StringBuilder sb = new StringBuilder();
GetHtmlUrl(ref strHtml);
Response.Write(strHtml.ToString());
Response.End();
}
public byte[] getVerificationCode(Uri url)
{
WebClient MyWebClient = new WebClient();
MyWebClient.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
MyWebClient.Headers.Add("Accept-Language", " zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
MyWebClient.Headers.Add("User-Agent", this.UserAgent);
MyWebClient.Credentials = CredentialCache.DefaultCredentials;
try
{
Byte[] pageData = MyWebClient.DownloadData(url.AbsoluteUri);
ContentType = MyWebClient.ResponseHeaders["Content-Type"];
return (pageData);
}
catch
{
return null;
}
}
    void GetHtmlUrl(ref StringBuilder strHtml)
{
//string headstr = "(src|href)=", endstr = "(\")";
//string reg = @"(?<=" + headstr + ")(.*?)(?=" + endstr + ")"; string reg = "(src|href)\\s*=\\s*(?:\"(?<1>[^\"]*)\"|(?<1>\\S+))";
Regex r = new Regex(reg, RegexOptions.None);
Match match = r.Match(strHtml.ToString());
StringBuilder sb = new StringBuilder();
while (match.Success)
{
//sb.Append(match.Groups["url"].Value + "\n");//得到href值
//sb.Append(match.Groups["text"].Value + "\n");//得到<a><a/>中间的内容 sb.Append(match + "\n");//得到href值
match = match.NextMatch();
//try
//{
// Uri u = new Uri(strReqUrl, match.Value.Replace("\"", "").Replace("'", ""));
// strHtml.Replace(match.Value, @"/" + u.ToString().Replace(strReqUrl.ToString(), ""));
//}
//catch
//{
//}
}
}

正则抓取网页所有href和src的更多相关文章

  1. Asp.net 使用正则和网络编程抓取网页数据(有用)

    Asp.net 使用正则和网络编程抓取网页数据(有用) Asp.net 使用正则和网络编程抓取网页数据(有用) /// <summary> /// 抓取网页对应内容 /// </su ...

  2. python分布式抓取网页

    呵呵,前两节好像和python没多大关系..这节完全是贴代码, 这是我第一次写python,很多地方比较乱,主要就看看逻辑流程吧. 对于编码格式确实搞得我头大..取下来页面不知道是什么编码,所以先找c ...

  3. Python爬虫【三】利用requests和正则抓取猫眼电影网上排名前100的电影

    #利用requests和正则抓取猫眼电影网上排名前100的电影 import requests from requests.exceptions import RequestException imp ...

  4. Python -- 网络编程 -- 简单抓取网页

    抓取网页: urllib.request.urlopen(url).read().decode('utf-8')  ---  (百度是utf-8,谷歌不是utf-8,也不是cp936,ascii也不行 ...

  5. jsoup抓取网页+具体解说

    jsoup抓取网页+具体解说 Java 程序在解析 HTML 文档时,相信大家都接触过 htmlparser 这个开源项目.我以前在 IBM DW 上发表过两篇关于 htmlparser 的文章.各自 ...

  6. Java 抓取网页中的内容【持续更新】

    背景:前几天复习Java的时候看到URL类,当时就想写个小程序试试,迫于考试没有动手,今天写了下,感觉还不错 内容1. 抓取网页中的URL 知识点:Java URL+ 正则表达式 import jav ...

  7. C语言调用curl库抓取网页图片

    思路是先用curl抓取网页源码,然后以关键字寻找出图片网址.   #include <stdio.h> #include <stdlib.h> #include <str ...

  8. C语言调用curl库抓取网页图片(转)

    思路是先用curl抓取网页源码,然后以关键字寻找出图片网址.  范例: #include <stdio.h> #include <stdlib.h> #include < ...

  9. Jumony快速抓取网页 --- Jumony使用笔记--icode

    作者:郝喜路   个人主页:http://www.cnicode.com      博客地址:http://haoxilu.cnblogs.com    时间:2014年6月26日 19:25:02 ...

随机推荐

  1. HDU 5806 NanoApe Loves Sequence Ⅱ

    将大于等于m的数改为1,其余的改为0.问题转变成了有多少个区间的区间和>=k.可以枚举起点,二分第一个终点 或者尺取法. #pragma comment(linker, "/STACK ...

  2. Android状态选择器用法总结

    原创文章,转载请注明出处http://www.cnblogs.com/baipengzhan/p/6284682.html 本文首先列出常见状态选择器的创建,然后按照常用控件来分别列出状态选择器的具体 ...

  3. VBS调用系统API

    如Beep的API声明为 Public Declare Function Beep Lib “kernel32″ Alias “Beep” (ByVal dwFreq As Long, ByVal d ...

  4. Bootstrap 3 与 Foundation 5

    开发工程师, 使用 Bootstrap. 前端开发人员, 使用 Foundation. 我们来谈谈为什么. Bootstrap 与 Foundation 有许多关键的区别, 但是, 我想你只需要记住一 ...

  5. mysql中AES_ENCRYPT、AES_DNCRYPT及CONVERT的用法

    这两天在弄公司的服务端项目的时候,发现mysql比较实用的3个函数,在这里给大家分享一下. 1.AES_ENCRYPT函数,这个函数的使用场景是对一些安全性要求比较高的数据使用AES加密,语法为: A ...

  6. IOS中Label根据上个label的内容设置下个label的frame

    #import "ViewController.h" @interface ViewController () @property(nonatomic,strong)UILabel ...

  7. struts2获取请求参数的三种方式及传递给JSP参数的方式

    接上一篇文章 package test; import com.opensymphony.xwork2.ActionSupport; import javax.servlet.http.*; impo ...

  8. SLF4JLoggerContext cannot be cast to LoggerContext

    Getting Exception org.apache.logging.slf4j.SLF4JLoggerContext cannot be cast to org.apache.logging.l ...

  9. Sql server 开窗函数over()的语法

    用法一:与ROW_NUMBER()函数结合用,给结果进行排序编号,如图: 代码如下: SELECT ROW_NUMBER() over(order by RequiredDate) num ,* fr ...

  10. java 区分error和exception

    1) java.lang.Error: Throwable的子类,用于标记严重错误.合理的应用程序不应该去try/catch这种错误.绝大多数的错误都是非正常的,就根本不该出现的.java.lang. ...