实例介绍
【实例截图】
【核心代码】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading;
using System.Runtime.InteropServices;
using System.IO;
/*本人C#新手,这个软件是半成品,仅供大家学习交流之用*/
namespace BaiDuYun
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
class ListViewNF : System.Windows.Forms.ListView//此类是为了防止ListView控件加载数据时闪烁
{
public ListViewNF()
{
// 开启双缓冲
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
// Enable the OnNotifyMessage event so we get a chance to filter out
// Windows messages before they get to the form's WndProc
this.SetStyle(ControlStyles.EnableNotifyMessage, true);
}
protected override void OnNotifyMessage(Message m)
{
//Filter out the WM_ERASEBKGND message
if (m.Msg != 0x14)
{
base.OnNotifyMessage(m);
}
}
}
public struct strPCFileinfo
{
public string strName;
public string strSize;
public string strOwner;
public string strTime;
public string strCount;
public string strRemark;
public string strDownloadUrl;
};
List<strPCFileinfo> file = new List<strPCFileinfo>();//这个应该可以替换下面的fileinfo数组,本版本未用
public static object locker = new object();//添加一个对象作为锁
private delegate void SearchResultCallBack(int index,strPCFileinfo file);
private SearchResultCallBack searchResultCallBack;
private void SearchResultMethod(int index,strPCFileinfo file)//往listview控件添加信息
{
ListViewItem firstrecord = new ListViewItem(index.ToString());
firstrecord.SubItems.Add(file.strName);
firstrecord.SubItems.Add(file.strSize);
firstrecord.SubItems.Add(file.strOwner);
firstrecord.SubItems.Add(file.strTime);
firstrecord.SubItems.Add(file.strCount);
firstrecord.SubItems.Add(file.strRemark);
listView1.Items.Add(firstrecord);
}
int index;
strPCFileinfo[] fileinfo;
string htmlCompare=null;
int page;
private bool IsValideMethod(string url)//判断文件是否有效
{
// DateTime start = DateTime.Now;
//url = @"http://pan.baidu.com/share/link?shareid=1407451583&uk=2318901111";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "GET";
req.ContentType = "application/x-www-form-urlencoded";
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
Stream ReceiveStream = res.GetResponseStream();
Encoding encode = System.Text.Encoding.UTF8;
StreamReader sr = new StreamReader(ReceiveStream, encode);
string strResult = "";
Char[] read = new Char[512];
int count = sr.Read(read, 0, 512);
while (count > 0)
{
String str = new String(read, 0, count);
strResult = str;
count = sr.Read(read, 0, 256);
}
MatchCollection match3 = Regex.Matches(strResult, "<title>百度云 网盘-链接不存在</title>");
try
{
if (match3[0].Success)
return false;
else
return true;
}
catch { }
return true;
}
private void RegexMatchResult()//核心代码,正则匹配所有符合要求的资源列表
{
//}\"[\s]*?(href=\"http://.*\") ?[^(文件名)]*立即关注 (匹配文件夹)
//string strPat ="}\"[\\s]*?(href=\"http://.*\") ?[\\s\\S]*?文件名:(. ?) 文件大小:(. ?) 分享者:(. ?) 分享时间:(. ?) 下载次数:(. ?)</div>";
index=page =0;
fileinfo = new strPCFileinfo[2000];
string strPat = "}\"[\\s]*?(href=\"http://.*\") ?[^(立即关注)]*?文件名:(. ?) 文件大小:(. ?) 分享者:(. ?) 分享时间:(. ?) 下载次数:(. ?)</div>";
Regex reg = new Regex(strPat);
WebClient client = new WebClient();
client.Proxy = null;
client.Encoding = Encoding.GetEncoding("utf-8");
string strUrl = string.Format(@"http://www.baidu.com/s?wd=site%3Apan.baidu.com%20{0}&pn={1}&ie=utf-8", textBox1.Text, page);
string html = client.DownloadString(strUrl);
bool IsSame = true;
//string html = client.DownloadString(@"http://www.baidu.com/s?wd=site%3Apan.baidu.com%20易语言&pn=10&ie=utf-8");
lock (locker)//锁
{
strPCFileinfo[] fileinfocompare = new strPCFileinfo[10];
while (IsSame && (page <= 2000))
{
MatchCollection matches = Regex.Matches(html, strPat);
//Trace.WriteLine("hello");
//fileinfo = new strPCFileinfo[matches.Count];
for (int i = 0; i < matches.Count; i )
{
if (matches[i].Success)
{
//Trace.WriteLine(matches[i].Value);
int j = index;
index ;
string strMatch = matches[i].Value;
strMatch = strMatch.Replace("<em>", "");
strMatch = strMatch.Replace("</em>", "");
strMatch = strMatch.Replace("</div>", "");
strMatch = strMatch.Replace(" ", "");
//strMatch = strMatch.Replace("\n", "");
//Regex reg1 = new Regex("[a-zA-z] ://[^\\s]*");
MatchCollection match = Regex.Matches(strMatch, "[a-zA-z] ://[^\\s]*");
fileinfo[j].strDownloadUrl = match[0].Value.Substring(0, match[0].Value.IndexOf("\""));
fileinfo[j].strName = strMatch.Substring(strMatch.IndexOf("文件名") 4);
fileinfo[j].strName = fileinfo[j].strName.Substring(0, fileinfo[j].strName.IndexOf("文件大小"));
fileinfo[j].strSize = strMatch.Substring(strMatch.IndexOf("文件大小") 5);
fileinfo[j].strSize = fileinfo[j].strSize.Substring(0, fileinfo[j].strSize.IndexOf("分享者"));
fileinfo[j].strOwner = strMatch.Substring(strMatch.IndexOf("分享者") 4);
fileinfo[j].strOwner = fileinfo[j].strOwner.Substring(0, fileinfo[j].strOwner.IndexOf("分享时间"));
fileinfo[j].strTime = strMatch.Substring(strMatch.IndexOf("分享时间") 5);
fileinfo[j].strTime = fileinfo[j].strTime.Substring(0, fileinfo[j].strTime.IndexOf("下载次数"));
fileinfo[j].strCount = strMatch.Substring(strMatch.IndexOf("下载次数") 5);
MatchCollection match2 = Regex.Matches(fileinfo[j].strCount, "[0-9] ");
try
{
if (match2[0].Success)
{
fileinfo[j].strCount = match2[0].Value;
}
else
fileinfo[j].strCount = "...";
}
catch (Exception ex)
{
// MessageBox.Show(ex.Message);
}
if (fileinfo[j].strSize == "-")
{
fileinfo[j].strRemark = "文件夹";
}
else
{
if(IsValideMethod(fileinfo[j].strDownloadUrl)==false)
fileinfo[j].strRemark = "该资源已失效";
else
fileinfo[j].strRemark = " ";
}
if (fileinfocompare[0].strOwner == fileinfo[j].strOwner && fileinfocompare[0].strTime == fileinfo[j].strTime)
{
IsSame = false;
}
else
{
fileinfocompare[i] = fileinfo[j];
//fileinfo[i].strSize = fileinfo[i].strSize.Substring(0, fileinfo[i].strSize.IndexOf("分享者"));
listView1.Invoke(searchResultCallBack, index, fileinfo[j]);
//Trace.WriteLine(strMatch);
}
}
}
htmlCompare = html;
page = page 10;
string strUrl1 = string.Format(@"http://www.baidu.com/s?wd=site%3Apan.baidu.com%20{0}&pn={1}&ie=utf-8", textBox1.Text, page);
html = client.DownloadString(strUrl1);
}
} //锁
}
private void button1_Click(object sender, EventArgs e)//单击搜索按钮触发事件
{
listView1.Items.Clear();
//RegexMatch();
searchResultCallBack = new SearchResultCallBack(SearchResultMethod);
Thread[] SearchResultThread = new Thread[5];
for (int p = 0; p < 5; p )
{
SearchResultThread[p] = new Thread(RegexMatchResult);
SearchResultThread[p].Start();
}
//SearchResultThread.Start();
}
//右击打开资源地址事件
[DllImport("shell32.dll")]
public static extern int ShellExecute(IntPtr hwnd, StringBuilder lpszOp, StringBuilder lpszFile, StringBuilder lpszParams, StringBuilder lpszDir, int FsShowCmd);
private void 打开资源地址ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (listView1.SelectedIndices.Count > 0)
{
// 转到下载页ToolStripMenuItem.Visible = true;
int index = listView1.SelectedIndices[0];//选中第index 1行
ShellExecute(IntPtr.Zero, new StringBuilder("Open"), new StringBuilder(fileinfo[index].strDownloadUrl), new StringBuilder(""), new StringBuilder(""), 1);
}
}
}
}
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论