这是一个简单的c++爬虫,效率并不是很高...

  1. #include<stdio.h>
  2. int s1[],s2[];
  3. void fun(int a, int b)
  4. {
  5. int i,ii;
  6. bool t1,t2,t3,t4;
  7. s1[] = s2[] = s1[] = s2[] = ;
  8. for(i=a; i <= b; i++){
  9. ii = i;
  10. t1 = t2 = t3 = t4 =false;
  11. while(ii != ){
  12. int a = ii %;
  13. if( a == )
  14. {
  15. t1 = true;
  16. }
  17. else if( a == )
  18. {
  19. t2 = true;
  20. }
  21. else if( a == )
  22. {
  23. t3 = true;
  24. }
  25. ii = ii / ;
  26. }
  27. if(t1 && t2 && t3){
  28. s1[i-] = s1[i-] + ;
  29. ii = i;
  30. while(ii != ){
  31. int a = ii % ;
  32. int b = (ii / ) % ;
  33. int c = (ii / ) % ;
  34. if( c > && a == && b == && c ==)
  35. t4 = true;
  36. ii = ii / ;
  37. }
  38. if(t4)
  39. s2[i-] = s2[i-] + ;
  40. else
  41. s2[i-] = s2[i-];
  42. }
  43. else{
  44. s2[i-] = s2[i-];
  45. s1[i-] = s1[i-];
  46. }
  47. }
  48. }
  49.  
  50. int main()
  51. {
  52. int a,b,i=;
  53. fun(,);
  54. while(scanf("%d%d",&a,&b) != EOF){
  55. if(a == )
  56. printf("Case %d:%d %d\n",i,s1[b-]-s1[a-],s2[b-]-s2[a-]);
  57. else
  58. printf("Case %d:%d %d\n",i,s1[b-]-s1[a-],s2[b-]-s2[a-]);
  59. i++;
  60. }
  61. return ;
  62. }
  1. #include"urlThread.h"
  2. #include<QFile>
  3. #include<QMessageBox>
  4. #include<QTextStream>
  5. #include <QMainWindow>
  6. void urlThread::run()
  7. {
  8. open();
  9. }
  10.  
  11. void urlThread::startThread()
  12. {
  13. start();
  14. }
  15.  
  16. //显示找到的url
  17. void urlThread::open()
  18. {
  19. QString path = "url.txt";
  20. QFile file(path);
  21. if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
  22. // QMessageBox::warning(this,tr("Read File"),
  23. // tr("Cannot open file:\n%1").arg(path));
  24. send("error!cannot open url.txt!");
  25. return;
  26. }
  27. QTextStream in(&file);
  28. while(in.readLine().compare("") != ){
  29. //ui->textBrowser->append(in.readLine());
  30. send(q2s(in.readLine()));
  31. Sleep();
  32. }
  33. file.close();
  34. }
  1. #include "mainwindow.h"
  2. #include <QApplication>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6. QApplication a(argc, argv);
  7. MainWindow w;
  8. w.setWindowTitle("小小爬虫");
  9. w.show();
  10.  
  11. return a.exec();
  12. }
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3.  
  4. MainWindow::MainWindow(QWidget *parent) :
  5. QMainWindow(parent),
  6. ui(new Ui::MainWindow)
  7. {
  8. ui->setupUi(this);
  9. QObject::connect(ui->start,SIGNAL(released()),this,SLOT(beginGeturl()));
  10. //QObject::connect(ui->display,SIGNAL(released()),this,SLOT(open()));
  11. QObject::connect(ui->display,SIGNAL(released()),&uth,SLOT(startThread()));
  12. QObject::connect(&uth,&urlThread::sendMessage,this,&MainWindow::receiveMessage);
  13. QObject::connect(&crawler,&Crawler::sendMessage,this,&MainWindow::receiveMessage);
  14. }
  15.  
  16. MainWindow::~MainWindow()
  17. {
  18. delete ui;
  19. }
  20.  
  21. void MainWindow::receiveMessage(const QString name)
  22. {
  23. ui->textBrowser->append(name);
  24. ui->textBrowser->moveCursor(QTextCursor::End);
  25. }
  26.  
  27. void MainWindow::open()
  28. {
  29. QString path = "url.txt";
  30. QFile file(path);
  31. if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
  32. QMessageBox::warning(this,tr("Read File"),
  33. tr("Cannot open file:\n%1").arg(path));
  34. return;
  35. }
  36. QTextStream in(&file);
  37. while(in.readLine().compare("") != ){
  38. //ui->textBrowser->append(in.readLine());
  39. crawler.send(q2s(in.readLine()));
  40. }
  41. file.close();
  42. }
  43.  
  44. void MainWindow::beginGeturl()
  45. {
  46. //crawler = new Crawler();
  47. string url = "" ,dep, filter = "www";
  48. if(!ui->site->text().isEmpty())
  49. url = q2s(ui->site->text());
  50. crawler.addURL(url);
  51. int depth = ;
  52. if(!ui->depth->text().isEmpty())
  53. {
  54. url = q2s(ui->depth->text());
  55. depth = atoi(url.c_str());
  56. }
  57. if(!ui->filter->text().isEmpty())
  58. filter = q2s(ui->filter->text());
  59. crawler.setJdugeDomain(filter);
  60. crawler.setDepth(depth);
  61. crawler.startThread();
  62. }
  1. #ifndef CRAWLER_H
  2. #define CRAWLER_H
  3.  
  4. #include<set>
  5. #include<string>
  6. #include<queue>
  7. #include "winsock2.h"
  8. #include <iostream>
  9. #include <fstream>
  10. #include <stdio.h>
  11. #include<time.h>
  12. #include<winsock.h>
  13. #include<QThread>
  14.  
  15. #pragma comment(lib, "ws2_32.lib")
  16. using namespace std;
  17.  
  18. bool ParseURL(const string & url, string & host, string & resource);
  19. bool GetHttpResponse(const string & url, char * &response, int &bytesRead);
  20. QString s2q(const string &s);
  21. string q2s(const QString &s);
  22.  
  23. #define DEFAULT_PAGE_BUF_SIZE 1000000
  24.  
  25. class Crawler: public QThread
  26. {
  27. Q_OBJECT
  28. private:
  29. queue<string> urlWaiting;
  30. set<string> urlWaitset;
  31. set<string> urlProcessed;
  32. set<string> urlError;
  33. set<string> disallow;
  34. set<string>::iterator it;
  35. int numFindUrl;
  36. time_t starttime, finish;
  37. string filter;
  38. int depth;
  39.  
  40. public:
  41. Crawler(){ filter = "\0";numFindUrl = ;}
  42. ~Crawler(){}
  43. void begin();
  44. void setDepth(int depth);
  45. void processURL(string& strUrl);
  46. void addURL(string url);
  47. void log(string entry, int num);
  48. void HTMLParse(string & htmlResponse, const string & host);
  49. bool getRobotx(const string & url, char * &response, int &bytesRead);
  50. void setJdugeDomain(const string domain);
  51. long urlOtherWebsite(string url);
  52. void send(string s)
  53. {
  54. QString qs = s2q(s);
  55. emit sendMessage(qs);
  56. }
  57. signals:
  58. void sendMessage(const QString name);
  59.  
  60. public slots:
  61. bool startThread();
  62.  
  63. protected:
  64. void run();
  65.  
  66. };
  67. #endif // CRAWLER_H
  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QMainWindow>
  5. #include<QFile>
  6. #include<QMessageBox>
  7. #include<QTextStream>
  8. #include<QLineEdit>
  9. #include<QDebug>
  10. #include"crawler.h"
  11. #include"urlThread.h"
  12.  
  13. namespace Ui {
  14. class MainWindow;
  15. }
  16.  
  17. class MainWindow : public QMainWindow
  18. {
  19. Q_OBJECT
  20.  
  21. public:
  22. explicit MainWindow(QWidget *parent = );
  23. ~MainWindow();
  24. void receiveMessage(const QString name);
  25.  
  26. public slots:
  27. void beginGeturl();
  28. void open();
  29.  
  30. private:
  31. Ui::MainWindow *ui;
  32. Crawler crawler;
  33. urlThread uth;
  34. };
  35.  
  36. #endif // MAINWINDOW_H
  1. #ifndef URLTHREAD_H
  2. #define URLTHREAD_H
  3. #include"crawler.h"
  4.  
  5. class urlThread: public QThread
  6. {
  7. Q_OBJECT
  8. public slots:
  9. void startThread();
  10. void open();
  11. void send(string s)
  12. {
  13. QString qs = s2q(s);
  14. emit sendMessage(qs);
  15. }
  16. signals:
  17. void sendMessage(const QString name);
  18. protected:
  19. void run();
  20. };
  21.  
  22. #endif // URLTHREAD_H
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ui version="4.0">
  3. <class>MainWindow</class>
  4. <widget class="QMainWindow" name="MainWindow">
  5. <property name="geometry">
  6. <rect>
  7. <x></x>
  8. <y></y>
  9. <width></width>
  10. <height></height>
  11. </rect>
  12. </property>
  13. <property name="windowTitle">
  14. <string>MainWindow</string>
  15. </property>
  16. <widget class="QWidget" name="centralWidget">
  17. <widget class="QLabel" name="label">
  18. <property name="geometry">
  19. <rect>
  20. <x></x>
  21. <y></y>
  22. <width></width>
  23. <height></height>
  24. </rect>
  25. </property>
  26. <property name="text">
  27. <string>初始网址:</string>
  28. </property>
  29. </widget>
  30. <widget class="QLineEdit" name="site">
  31. <property name="geometry">
  32. <rect>
  33. <x></x>
  34. <y></y>
  35. <width></width>
  36. <height></height>
  37. </rect>
  38. </property>
  39. <property name="text">
  40. <string>http://www.scut.edu.cn</string>
  41. </property>
  42. </widget>
  43. <widget class="QLabel" name="label_2">
  44. <property name="geometry">
  45. <rect>
  46. <x></x>
  47. <y></y>
  48. <width></width>
  49. <height></height>
  50. </rect>
  51. </property>
  52. <property name="text">
  53. <string>搜索深度:</string>
  54. </property>
  55. </widget>
  56. <widget class="QLineEdit" name="depth">
  57. <property name="geometry">
  58. <rect>
  59. <x></x>
  60. <y></y>
  61. <width></width>
  62. <height></height>
  63. </rect>
  64. </property>
  65. <property name="text">
  66. <string></string>
  67. </property>
  68. </widget>
  69. <widget class="QLabel" name="label_3">
  70. <property name="geometry">
  71. <rect>
  72. <x></x>
  73. <y></y>
  74. <width></width>
  75. <height></height>
  76. </rect>
  77. </property>
  78. <property name="text">
  79. <string>过滤字符串:</string>
  80. </property>
  81. </widget>
  82. <widget class="QLineEdit" name="filter">
  83. <property name="geometry">
  84. <rect>
  85. <x></x>
  86. <y></y>
  87. <width></width>
  88. <height></height>
  89. </rect>
  90. </property>
  91. <property name="text">
  92. <string>scut</string>
  93. </property>
  94. </widget>
  95. <widget class="QPushButton" name="start">
  96. <property name="geometry">
  97. <rect>
  98. <x></x>
  99. <y></y>
  100. <width></width>
  101. <height></height>
  102. </rect>
  103. </property>
  104. <property name="text">
  105. <string>开始</string>
  106. </property>
  107. </widget>
  108. <widget class="QPushButton" name="display">
  109. <property name="geometry">
  110. <rect>
  111. <x></x>
  112. <y></y>
  113. <width></width>
  114. <height></height>
  115. </rect>
  116. </property>
  117. <property name="text">
  118. <string>显示url</string>
  119. </property>
  120. </widget>
  121. <widget class="QTextBrowser" name="textBrowser">
  122. <property name="geometry">
  123. <rect>
  124. <x></x>
  125. <y></y>
  126. <width></width>
  127. <height></height>
  128. </rect>
  129. </property>
  130. </widget>
  131. </widget>
  132. <widget class="QMenuBar" name="menuBar">
  133. <property name="geometry">
  134. <rect>
  135. <x></x>
  136. <y></y>
  137. <width></width>
  138. <height></height>
  139. </rect>
  140. </property>
  141. </widget>
  142. <widget class="QToolBar" name="mainToolBar">
  143. <attribute name="toolBarArea">
  144. <enum>TopToolBarArea</enum>
  145. </attribute>
  146. <attribute name="toolBarBreak">
  147. <bool>false</bool>
  148. </attribute>
  149. </widget>
  150. <widget class="QStatusBar" name="statusBar"/>
  151. </widget>
  152. <layoutdefault spacing="" margin=""/>
  153. <resources/>
  154. <connections/>
  155. </ui>

ui文件要自己设计

爬虫还有一些小问题,抓取的url content并不完全,某些地址还有一点小问题...

貌似博客园没有上传附件的地方?还是我没找到?希望得到提示~

c++ 爬虫的更多相关文章

  1. 设计爬虫Hawk背后的故事

    本文写于圣诞节北京下午慵懒的午后.本文偏技术向,不过应该大部分人能看懂. 五年之痒 2016年,能记入个人年终总结的事情没几件,其中一个便是开源了Hawk.我花不少时间优化和推广它,得到的评价还算比较 ...

  2. Scrapy框架爬虫初探——中关村在线手机参数数据爬取

    关于Scrapy如何安装部署的文章已经相当多了,但是网上实战的例子还不是很多,近来正好在学习该爬虫框架,就简单写了个Spider Demo来实践.作为硬件数码控,我选择了经常光顾的中关村在线的手机页面 ...

  3. Python 爬虫模拟登陆知乎

    在之前写过一篇使用python爬虫爬取电影天堂资源的博客,重点是如何解析页面和提高爬虫的效率.由于电影天堂上的资源获取权限是所有人都一样的,所以不需要进行登录验证操作,写完那篇文章后又花了些时间研究了 ...

  4. scrapy爬虫docker部署

    spider_docker 接我上篇博客,为爬虫引用创建container,包括的模块:scrapy, mongo, celery, rabbitmq,连接https://github.com/Liu ...

  5. scrapy 知乎用户信息爬虫

    zhihu_spider 此项目的功能是爬取知乎用户信息以及人际拓扑关系,爬虫框架使用scrapy,数据存储使用mongo,下载这些数据感觉也没什么用,就当为大家学习scrapy提供一个例子吧.代码地 ...

  6. 120项改进:开源超级爬虫Hawk 2.0 重磅发布!

    沙漠君在历时半年,修改无数bug,更新一票新功能后,在今天隆重推出最新改进的超级爬虫Hawk 2.0! 啥?你不知道Hawk干吗用的? 这是采集数据的挖掘机,网络猎杀的重狙!半年多以前,沙漠君写了一篇 ...

  7. Python爬虫小白入门(四)PhatomJS+Selenium第一篇

    一.前言 在上一篇博文中,我们的爬虫面临着一个问题,在爬取Unsplash网站的时候,由于网站是下拉刷新,并没有分页.所以不能够通过页码获取页面的url来分别发送网络请求.我也尝试了其他方式,比如下拉 ...

  8. Python多线程爬虫爬取电影天堂资源

    最近花些时间学习了一下Python,并写了一个多线程的爬虫程序来获取电影天堂上资源的迅雷下载地址,代码已经上传到GitHub上了,需要的同学可以自行下载.刚开始学习python希望可以获得宝贵的意见. ...

  9. QQ空间动态爬虫

    作者:虚静 链接:https://zhuanlan.zhihu.com/p/24656161 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 先说明几件事: 题目的意 ...

  10. 让你从零开始学会写爬虫的5个教程(Python)

    写爬虫总是非常吸引IT学习者,毕竟光听起来就很酷炫极客,我也知道很多人学完基础知识之后,第一个项目开发就是自己写一个爬虫玩玩. 其实懂了之后,写个爬虫脚本是很简单的,但是对于新手来说却并不是那么容易. ...

随机推荐

  1. Android 常用工具类之RuntimeUtil

    public class RuntimeUtil { /** 通过查询su文件的方式判断手机是否root */ public static boolean hasRootedSilent() { re ...

  2. init_MUTEX 与 sema_init 函数【转】

    转自:http://blog.chinaunix.net/uid-7332782-id-3211627.html 在编译Linux设备驱动程序学习(1)-字符设备驱动程序中scull.c程序时,报错: ...

  3. taglib指令

    taglib:用来引用标签库并设置标签库的前缀,(允许JSP页面使用用户自定义标签) 语法:<%@ taglib uri="tagLibraryURI" prefix=&qu ...

  4. windows win10上传文件到linux服务器

    1.最直接当然使用终端secucrt和xshell putty之类的,然后使用sz rz 2.如果服务器端不支持sz rz可以使用scp命令,下面这个pscp.exe就是支持scp的,基于ssh,很好 ...

  5. MyISAM表杂记实验

    一.本文说明 由于刚学mysql所以动手做了一些实验. 二.实验内容 1.验证MyISAM有AUOT_INCREMENT coloumn功能 ----在这里是对现有表t,增加一个主键----mysql ...

  6. 在MyEclipse中搭建Spring MVC开发环境

    环境版本 IDE:MyEclipse 8.5 Spring:spring 3.2.8 JDK:1.6 1.打开MyEclipse-->File-->New-->Web Project ...

  7. 关于sql server 2008过期导致 MSSQLSERVER服务就无法启动,手动启动就报告错误代码17051。

    1.基本现象:MSSQLSERVER服务就无法启动,手动启动就报告17051错误. 2.解决办法: 第一步:进入SQL2008配置工具中的安装中心, 第二步:再进入维护界面,选择版本升级, 第三步:进 ...

  8. java 判断某一天是当年的哪一天

    题目:输入年份,月份,日,判断这一天是这一年的第几天?(闰年的2月份为29天,平年为28天) public class Runnian { /** * 能被4整除且不能被100整除或者能被400整除的 ...

  9. 递归,动态规划,找最短路径,Help Jimmy

    题目链接:http://poj.org/problem?id=1661 解题报告: 1.老鼠每次来到一块木板上都只有两条路可以走,可以使用递归 #include <stdio.h> #in ...

  10. sql 清空所有数据

    近来发现数据库过大,空间不足,因此打算将数据库的数据进行全面的清理,但表非常多,一张一张的清空,实在麻烦,因此就想利用SQL语句一次清空所有数据.找到了三种方法进行清空.使用的数据库为MS SQL S ...