[C#]网络扒虫
来源:互联网 发布:linux nc命令 端口 编辑:程序博客网 时间:2024/06/11 10:39
这个其实不能算是完全意义上的网络扒虫,只是对某个社交网络进行扒取,然后得到邻接矩阵,以及相应的头像等信息。
主要的步骤:
1,扒取信息
2,正则匹配
正则表达式主要参考了:http://deerchao.net/tutorials/regex/regex.htm
扒取信息中用的是 WebClient这个方法相对HttpRequest的HttpResponse更简洁一些。
难点是克服网站的认证机制,用的是保存Cookies的方法。
扒虫部分代码:
using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
public class Crawler
{
public static string GetCont(string url)//扒取页面
{
string cookies = "_r01_=; depovince=BJ; p=; ap=; t=; societyguester=55a777838c4286ab5f657382dbd25c736; id=; xnsid=";
WebClient WebC = new WebClient();
WebC.Headers.Add("Cookie", cookies);
byte[] WebPa = WebC.DownloadData(url);
string PageHtml = Encoding.UTF8.GetString(WebPa);
return PageHtml;
}
public static void GetImag(string ImgUrl,string UserName)//下载小图片
{
string imageFileName;
string imageFilePath;
WebClient myClient = new WebClient();
Regex regex = new Regex("//w*");
MatchCollection UsNameMatches = regex.Matches(UserName);
imageFileName = UsNameMatches[0].Value.ToString() + ".jpg";
imageFilePath = @"D:/picture/" + imageFileName;
try
{
myClient.DownloadFile(ImgUrl, imageFilePath);
}
catch
{
}
}
}
//正则表达式部分
using System.Text;
using System.Text.RegularExpressions;
public class MyRegex
{
public static string[] GetAddr(string PageHtml)
{
string[] PageUrl=new string[24];
Regex regex = new Regex("http://www..com/profile.do//?portal=//w*&id=//d+(?=/"//stitle=)");
MatchCollection urlMatches = regex.Matches(PageHtml);
for(int i=0;i<urlMatches.Count;i++)
{
PageUrl[i]=urlMatches[i].Value.ToString();
}
return PageUrl;
}
public static string[] GetImgAddr(string PageHtml)
{
string[] ImgAddr = new string[24];
Regex regex = new Regex("(?<=stats=/"pf_friend/"//ssrc//=/").*(?=/"//swidth=/"50/"//s/>)");
MatchCollection ImgMatches = regex.Matches(PageHtml);
for(int i=0;i<ImgMatches.Count;i++)
{
ImgAddr[i] = ImgMatches[i].Value.ToString();
}
return ImgAddr;
}
public static string[] GetUsName(string PageHtml)
{
string[] UsName = new string[24];
Regex regex = new Regex("(?<=title=/"查看).*(?=的个人主页/">//W<img//sstats=/"pf_friend/")");
MatchCollection UsNameMatches = regex.Matches(PageHtml);
for (int i = 0; i < UsNameMatches.Count; i++)
{
UsName[i] = UsNameMatches[i].Value.ToString();
}
return UsName;
}
}
- [C#]网络扒虫
- 网络扒取学习(1)
- 网络扒取学习(1)
- Python网络爬虫,在网站上扒数据
- 扒网页~~~
- 扒照片
- 扒新闻
- 扒完社交网络关系才明白,《权力的游戏》凭什么是神作
- 扒扒我的微信朋友圈
- 扒网站神器
- 扒光TP
- wget扒站
- 扒网页并且解析
- 扒旧乐趣
- 怎么扒JS代码?
- 扒网页内容
- php-扒数据
- 如何扒网站
- 调测页面常见几种方法
- 关于云计算的思考
- 使用SVN管理软件版本,解决协作问题
- [C#]文本相似度检测(1)
- 坏的cpu不要轻易丢掉
- [C#]网络扒虫
- Android Uncaught Exception Handler邮件发送错误日志
- ATL工程创建控制台的方法
- 百度降权后进入沙盒后恢复实例
- 联通3G
- Linux下启动Oracle服务和监听程序
- 过程管理
- Secure File Transfer Client错误提示 "encountered 1 errors during the transfer"
- 当当李国庆