最近学习C#编程,在网上发现一篇winform下制作百度网盘搜索器的文章,故而下载源码学习一二。无奈原博所用的网址失效,故而自己改写了网址和相关源代码,也进行了实现。因为初学,接触的知识较多,为免忘记,进行整理复习。

1.知识点:

思路:主要是利用HttpWebRequest,HttpWebResponse进行http模拟请求,然后利用HtmlAgilityPack+XPath语法对html dom进行元素获取,将截取到的相关内容在datagridview中展示,最后利用process.start()方法进行点击访问。

2.具体实现:

2.1关于请求头的获取:

本例子使用网址为:http://www.pansoso.com/

分析上述网址的请求头进行模拟:

查看具体请求头信息:

根据获取的request url分析出其请求地址的规律为:所搜索的关键字:hello直接利用get方法添加到了url的最后,其中页数规律为hello_1,hello_2。。。(每页十条记录)

2.2关于结果的获取:

结果的获取,直接利用对response网页的分析截取关键信息即可。

3.代码实现:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Text;
  7. namespace 百度网盘资源搜索
  8. {
  9. class HttpHelper
  10. {
  11. static readonly string urlTemplate = "http://www.pansoso.com/zh/{0}";
  12. public static string Requset(string key)
  13. {
  14. string url = string.Format(urlTemplate, key);
  15. //Console.WriteLine(url);
  16. HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
  17. httpRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
  18. httpRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36";
  19. httpRequest.Host = "www.pansoso.com";
  20. httpRequest.Referer = "http://www.pansoso.com/zh/" + Uri.EscapeUriString(key);
  21. try
  22. {
  23. HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
  24. Stream s = httpResponse.GetResponseStream();
  25. StreamReader sr = new StreamReader(s);
  26. string jsonString = sr.ReadToEnd();
  27. //Console.WriteLine(jsonString);
  28. //string jsonProcessed = null;
  29. //if ((jsonProcessed = JsonPreProcessing(jsonString)) != null)
  30. //{
  31. // SearchResult searchResult = UtilityClass.GetObject<SearchResult>(jsonProcessed);
  32. // return searchResult;
  33. //}
  34. return jsonString;
  35. }
  36. catch
  37. {
  38. return null;
  39. }
  40. }
  41. public static SearchResult dodata(string str)
  42. {
  43. SearchResult searchResult = UtilityClass.GetObject<SearchResult>(str);
  44. return searchResult;
  45. }
  46. //if (doc.DocumentNode.SelectNodes("//comment()") != null)
  47. //{
  48. // foreach (var commet in doc.DocumentNode.SelectNodes("//comment"))
  49. // {
  50. // commet.Remove();
  51. // }
  52. //}
  53. public static string JsonPreProcessing(string jsonString)
  54. {
  55. int startIndex = jsonString.IndexOf("(");
  56. if (startIndex > 0)
  57. {
  58. string json = jsonString.Substring(startIndex + 1);
  59. return "{\"resources\":" + json.Remove(json.Length - 3) + "}";
  60. }
  61. else
  62. {
  63. return null;
  64. }
  65. }
  66. }
  67. }

Utility.Class

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. //using System.Linq;
  5. using System.Runtime.Serialization.Json;
  6. using System.Text;
  7. namespace 百度网盘资源搜索
  8. {
  9. class UtilityClass
  10. {
  11. public static T GetObject<T>(string json)
  12. {
  13. DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
  14. MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
  15. T obj = (T)serializer.ReadObject(ms);
  16. return obj;
  17. }
  18. }
  19. }

JSontoObject.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace 百度网盘资源搜索
  6. {
  7. public class SearchResult
  8. {
  9. public BDWPResource[] resources { get; set; }
  10. }
  11. public class BDWPResource
  12. {
  13. public string title { get; set; }
  14. public string content { get; set; }
  15. public string unescapedUrl { get; set; }
  16. }
  17. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Threading;
  10. using System.Diagnostics;
  11. namespace 百度网盘资源搜索
  12. {//主窗体
  13. public partial class FrmMain : Form
  14. {
  15. bool isSearch = true;
  16. string url = "http://www.pansoso.com";
  17. public FrmMain()
  18. {
  19. InitializeComponent();
  20. }
  21. private void btnSearch_Click(object sender, EventArgs e)
  22. {
  23. string key = this.txtKey.Text;
  24. if (!string.IsNullOrEmpty(key))
  25. {
  26. this.dataGridView1.Rows.Clear();
  27. this.lblResult.Text = "0";
  28. this.pgsBar.Value = 0;
  29. this.btnSearch.Text = "正在搜索";
  30. this.btnSearch.Enabled = false;
  31. this.btnStop.Enabled = true;
  32. Thread thread = new Thread(() =>
  33. {
  34. for (int i = 1; i < 11; i ++)//共取得10页网页数据
  35. {
  36. if (isSearch)
  37. {
  38. gethtml(HttpHelper.Requset(key+"_"+i));
  39. //gethtml(HttpHelper.Requset(key));
  40. //if(textBox1.Text!=null)
  41. //{
  42. // string name=textBox1.Text;
  43. // SearchResult sr= HttpHelper.dodata(name);
  44. // if (sr != null)
  45. // {
  46. // foreach (BDWPResource resource in sr.resources)
  47. // {
  48. // BindResource(resource);
  49. // }
  50. // }
  51. // }
  52. // webBrowser1.DocumentText = HttpHelper.Requset(key);
  53. // Navigate to HTML document string
  54. //webBrowser1.Navigate(HttpHelper.Requset(key));
  55. // SearchResult sr = HttpHelper.Requset(key);
  56. }
  57. else break;
  58. }
  59. //搜索完成
  60. SearchOver();
  61. });
  62. thread.IsBackground = true;
  63. thread.Start();
  64. }
  65. }
  66. public void gethtml(string docs)
  67. {
  68. try
  69. {
  70. HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
  71. doc.LoadHtml(docs);
  72. if (doc.DocumentNode.SelectNodes("//script") != null)
  73. {
  74. foreach (var script in doc.DocumentNode.SelectNodes("//script"))
  75. {
  76. script.Remove();
  77. }
  78. HtmlAgilityPack.HtmlNodeCollection hrefList = doc.DocumentNode.SelectNodes(".//h2/a[@href]");
  79. HtmlAgilityPack.HtmlNodeCollection list2 = doc.DocumentNode.SelectNodes(".//div[@class='des']");
  80. HtmlAgilityPack.HtmlNodeCollection list3 = doc.DocumentNode.SelectNodes(".//h2/a[@href]");
  81. if (hrefList != null && list2 != null && list3 != null)
  82. {
  83. for (int i = 0; i < list2.Count; i++)
  84. {
  85. string url1 = url + list3[i].Attributes["href"].Value;
  86. string json = "title:" + hrefList[i].InnerText + "content:" + list2[i].InnerText + "unescapedUrl:" +"【"+url1+"】" ;
  87. // Process.Start(url1);
  88. SearchOver1(json);
  89. this.Invoke(new Action<string, string, string>((tle, ctt, url3) =>
  90. {
  91. this.dataGridView1.Rows.Add(tle, ctt, url3);
  92. this.lblResult.Text = (Int32.Parse(this.lblResult.Text) + 1).ToString();
  93. if (this.pgsBar.Value < this.pgsBar.Maximum)
  94. {
  95. this.pgsBar.Value++;
  96. }
  97. }), hrefList[i].InnerText,list2[i].InnerText, url1);
  98. }
  99. }
  100. }
  101. }
  102. catch (Exception)
  103. {
  104. MessageBox.Show("该关键字没有收录资源!!!");
  105. }
  106. }
  107. //if (doc.DocumentNode.SelectNodes("//style") != null)
  108. //{
  109. // foreach (var style in doc.DocumentNode.SelectNodes("style"))
  110. // {
  111. // style.Remove();
  112. // }
  113. //}
  114. private void BindResource(BDWPResource resource)
  115. {
  116. string title = resource.title.Replace("</b>", "").Replace("<b>", "");
  117. string content = resource.content.Replace("</b>", "").Replace("<b>", "");
  118. this.Invoke(new Action<string, string, string>((tle, ctt, url) =>
  119. {
  120. this.dataGridView1.Rows.Add(tle, ctt, url);
  121. this.lblResult.Text = (Int32.Parse(this.lblResult.Text) + 1).ToString();
  122. if (this.pgsBar.Value < this.pgsBar.Maximum)
  123. {
  124. this.pgsBar.Value++;
  125. }
  126. }), title, content, resource.unescapedUrl);
  127. }
  128. private void SearchOver()
  129. {
  130. this.Invoke(new Action(() =>
  131. {
  132. this.btnSearch.Text = "开始搜索";
  133. this.btnSearch.Enabled = true;
  134. this.btnStop.Enabled = false;
  135. this.isSearch = true;
  136. }));
  137. }
  138. public void SearchOver1(string str)
  139. {
  140. this.Invoke(new Action(() =>
  141. {
  142. this.richTextBox1.Text += str + System.Environment.NewLine;
  143. }));
  144. }
  145. private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
  146. {
  147. SolidBrush b = new SolidBrush(this.dataGridView1.RowHeadersDefaultCellStyle.ForeColor);
  148. e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), this.dataGridView1.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 6);
  149. e.Graphics.FillRectangle(Brushes.White, new Rectangle(new Point(e.RowBounds.Location.X + 2, e.RowBounds.Location.Y + 2), new Size(20, 20)));//隐藏每行前面的图标
  150. }
  151. //打开网页链接
  152. private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
  153. {
  154. if (e.RowIndex > -1)
  155. {
  156. string url = this.dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
  157. Process.Start(url);//进行打开浏览器的方法。
  158. }
  159. }
  160. private void btnStop_Click(object sender, EventArgs e)
  161. {
  162. isSearch = false;
  163. this.btnSearch.Enabled = true;
  164. }
  165. private void richTextBox1_LinkClicked(object sender, LinkClickedEventArgs e)
  166. {
  167. System.Diagnostics.Process.Start(e.LinkText);
  168. }
  169. }
  170. }

4.效果实现:

C#制作网盘搜索工具(简单的爬虫)的更多相关文章

  1. 推荐一个百度网盘搜索工具www.sososo.me

    推荐一个百度网盘搜索工具 http://www.sososo.me

  2. [C#]使用Windows Form开发的百度网盘搜索工具

    BaiduDiskSearcher 用C#编写的百度网盘搜索工具(.NET4.0 & Visual Studio 2017) 功能 1.搜索任意名称的百度网盘共享文件 2.可以左键双击打开网盘 ...

  3. [PHP] 网盘搜索引擎-采集爬取百度网盘分享文件实现网盘搜索

    标题起的太大了,都是骗人的.最近使用PHP实现了简单的网盘搜索程序,并且关联了微信公众平台.用户可以通过公众号输入关键字,公众号会返回相应的网盘下载地址.就是这么一个简单的功能,类似很多的网盘搜索类网 ...

  4. 分享一个开源的网盘下载工具BaiduPCS-Go

    大家在使用网盘的时候,一定忍受不了限速下载的速度.今天给大家分享一个开源的网盘下载项目BaiduPCS-Go.Go语言编写,仿 Linux shell 文件处理命令的百度网盘命令行客户端.多平台支持, ...

  5. SpringBoot2.0+ElasticSearch网盘搜索实现

    1.ES是如何实现分布式高并发全文检索 2.简单介绍ES分片Shards分片技术 3.为什么ES主分片对应的备分片不在同一台节点存放 4.索引的主分片定义好后为什么不能做修改 5.ES如何实现高可用容 ...

  6. 网盘直链工具 winform版 V1.0

    软件需要.net2.0支持 win7及以上版本用户无需安装 xp用户需要安装 支持网盘:好盘 坚果云 百度云 乐视云 华为网盘 微云 新浪网盘 126disk 速度盘 乐齐盘 天空网盘 千脑网盘 可乐 ...

  7. osx 10.11 一键制作U盘傻瓜工具最新版 无需任何命令

    osx 10.11 最新版U盘制作工具   无需任何命令   纯傻瓜式  !!!只要把app下载下来放在应用程序  鼠标点点就可以做了... 下载地址:http://diskmakerx.com/do ...

  8. 4款Github泄漏敏感信息搜索工具简单比较

    gitrob Ruby开发,支持通过postgresql数据库https://github.com/michenriksen/gitrob weakfilescan Python开发,多线程,猪猪侠开 ...

  9. 跑满带宽的一款百度网盘下载工具 : PanDownload

    下载地址 : 点击进入 官网上面也有介绍使用.在这里,我再说一下 下载之后,解压,运行,登录, 登录好之后,准备进行设置 重要:下载情况分以下三部分 下载内容 < 300M,选择`打包下载`,只 ...

随机推荐

  1. opencv——import导包出现错误

    原因:编辑器找不到,CV2的模块,也就是导入这个模块失败: 原因可能是sublime找不到这个这个模块的位置,不知道这个包在哪里,这时候需要我们安装OpenCV的一个扩展包. 解决步骤: ①:找到py ...

  2. 【题解】滑雪 luogu1434 记忆化搜索

    记忆化搜索入门题 题目 Michael喜欢滑雪.这并不奇怪,因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道在 ...

  3. mysql的主从复制延迟问题--看这一篇就够了

    ​ 在之前我们已经讲解了一主一从,双主双从的mysql集群搭建,在单机应用的时候看起来没有问题,但是在企业的生产环境中,在很多情况下都会有复制延迟的问题. ​ 主从复制的原理我们在此处就不再赘述了,之 ...

  4. USB上位机通信:CyAPI

    至今的工作中,有USB接口通信的需求,记录一下. 建立一个USB设备对象 CCyUSBDevice *USBDevice = new CCyUSBDev(Handle): 打开USB设备 一个USB设 ...

  5. JavaWeb入门知识梳理

    万维网 Web App(Web应用程序)是一种可以通过万维网访问的应用程序,用户只需要连接互联网和计算机安装浏览器,即可通过URI在线使用某个Web App,而不需要再安装客户端到计算机上.Web A ...

  6. Ha1cyon_CTF-公开赛(wp)

    一.babyasm 00007FF7A8AC5A50 push rbp 00007FF7A8AC5A52 push rdi 00007FF7A8AC5A53 sub rsp,238h 00007FF7 ...

  7. webpack(11)配置文件分离为开发配置、生成配置和基础配置

    前言 上篇我们已经配置好了本地开发服务器,但是配置的相对比较凌乱,一个文件中有些是开发时用到的配置,有些是生成时用到的配置,有些是开发和生成都要用到的配置,所以我们这里把环境分为3个环境 webpac ...

  8. 虚拟机centos7环境搭建,系统分区,静态IP配置

    文章目录 1.虚拟机安装centos7 2.系统分区 3.配置静态IP centos7下载地址 http://mirrors.aliyun.com/centos/7/isos/x86_64/ Cent ...

  9. Docker从容器拷贝文件到宿主机或从宿主机拷贝文件到容器

    1.从容器里面拷文件到宿主机? 答:在宿主机里面执行以下命令 docker cp 容器名:要拷贝的文件在容器里面的路径       要拷贝到宿主机的相应路径 示例: 假设容器名为testtomcat, ...

  10. 题解 SP3591 PATHEADS - Patting Heads

    类似桶排 先看有多少头奶牛抽出这个数 再看这个数的奶牛能拍多少人的头(别忘了-1,自己不能拍自己) 最后根据输入输出 110ms #include<bits/stdc++.h> using ...