c++ 爬虫
这是一个简单的c++爬虫,效率并不是很高...
- #include<stdio.h>
- int s1[],s2[];
- void fun(int a, int b)
- {
- int i,ii;
- bool t1,t2,t3,t4;
- s1[] = s2[] = s1[] = s2[] = ;
- for(i=a; i <= b; i++){
- ii = i;
- t1 = t2 = t3 = t4 =false;
- while(ii != ){
- int a = ii %;
- if( a == )
- {
- t1 = true;
- }
- else if( a == )
- {
- t2 = true;
- }
- else if( a == )
- {
- t3 = true;
- }
- ii = ii / ;
- }
- if(t1 && t2 && t3){
- s1[i-] = s1[i-] + ;
- ii = i;
- while(ii != ){
- int a = ii % ;
- int b = (ii / ) % ;
- int c = (ii / ) % ;
- if( c > && a == && b == && c ==)
- t4 = true;
- ii = ii / ;
- }
- if(t4)
- s2[i-] = s2[i-] + ;
- else
- s2[i-] = s2[i-];
- }
- else{
- s2[i-] = s2[i-];
- s1[i-] = s1[i-];
- }
- }
- }
- int main()
- {
- int a,b,i=;
- fun(,);
- while(scanf("%d%d",&a,&b) != EOF){
- if(a == )
- printf("Case %d:%d %d\n",i,s1[b-]-s1[a-],s2[b-]-s2[a-]);
- else
- printf("Case %d:%d %d\n",i,s1[b-]-s1[a-],s2[b-]-s2[a-]);
- i++;
- }
- return ;
- }
- #include"urlThread.h"
- #include<QFile>
- #include<QMessageBox>
- #include<QTextStream>
- #include <QMainWindow>
- void urlThread::run()
- {
- open();
- }
- void urlThread::startThread()
- {
- start();
- }
- //显示找到的url
- void urlThread::open()
- {
- QString path = "url.txt";
- QFile file(path);
- if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
- // QMessageBox::warning(this,tr("Read File"),
- // tr("Cannot open file:\n%1").arg(path));
- send("error!cannot open url.txt!");
- return;
- }
- QTextStream in(&file);
- while(in.readLine().compare("") != ){
- //ui->textBrowser->append(in.readLine());
- send(q2s(in.readLine()));
- Sleep();
- }
- file.close();
- }
- #include "mainwindow.h"
- #include <QApplication>
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- MainWindow w;
- w.setWindowTitle("小小爬虫");
- w.show();
- return a.exec();
- }
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- QObject::connect(ui->start,SIGNAL(released()),this,SLOT(beginGeturl()));
- //QObject::connect(ui->display,SIGNAL(released()),this,SLOT(open()));
- QObject::connect(ui->display,SIGNAL(released()),&uth,SLOT(startThread()));
- QObject::connect(&uth,&urlThread::sendMessage,this,&MainWindow::receiveMessage);
- QObject::connect(&crawler,&Crawler::sendMessage,this,&MainWindow::receiveMessage);
- }
- MainWindow::~MainWindow()
- {
- delete ui;
- }
- void MainWindow::receiveMessage(const QString name)
- {
- ui->textBrowser->append(name);
- ui->textBrowser->moveCursor(QTextCursor::End);
- }
- void MainWindow::open()
- {
- QString path = "url.txt";
- QFile file(path);
- if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
- QMessageBox::warning(this,tr("Read File"),
- tr("Cannot open file:\n%1").arg(path));
- return;
- }
- QTextStream in(&file);
- while(in.readLine().compare("") != ){
- //ui->textBrowser->append(in.readLine());
- crawler.send(q2s(in.readLine()));
- }
- file.close();
- }
- void MainWindow::beginGeturl()
- {
- //crawler = new Crawler();
- string url = "" ,dep, filter = "www";
- if(!ui->site->text().isEmpty())
- url = q2s(ui->site->text());
- crawler.addURL(url);
- int depth = ;
- if(!ui->depth->text().isEmpty())
- {
- url = q2s(ui->depth->text());
- depth = atoi(url.c_str());
- }
- if(!ui->filter->text().isEmpty())
- filter = q2s(ui->filter->text());
- crawler.setJdugeDomain(filter);
- crawler.setDepth(depth);
- crawler.startThread();
- }
- #ifndef CRAWLER_H
- #define CRAWLER_H
- #include<set>
- #include<string>
- #include<queue>
- #include "winsock2.h"
- #include <iostream>
- #include <fstream>
- #include <stdio.h>
- #include<time.h>
- #include<winsock.h>
- #include<QThread>
- #pragma comment(lib, "ws2_32.lib")
- using namespace std;
- bool ParseURL(const string & url, string & host, string & resource);
- bool GetHttpResponse(const string & url, char * &response, int &bytesRead);
- QString s2q(const string &s);
- string q2s(const QString &s);
- #define DEFAULT_PAGE_BUF_SIZE 1000000
- class Crawler: public QThread
- {
- Q_OBJECT
- private:
- queue<string> urlWaiting;
- set<string> urlWaitset;
- set<string> urlProcessed;
- set<string> urlError;
- set<string> disallow;
- set<string>::iterator it;
- int numFindUrl;
- time_t starttime, finish;
- string filter;
- int depth;
- public:
- Crawler(){ filter = "\0";numFindUrl = ;}
- ~Crawler(){}
- void begin();
- void setDepth(int depth);
- void processURL(string& strUrl);
- void addURL(string url);
- void log(string entry, int num);
- void HTMLParse(string & htmlResponse, const string & host);
- bool getRobotx(const string & url, char * &response, int &bytesRead);
- void setJdugeDomain(const string domain);
- long urlOtherWebsite(string url);
- void send(string s)
- {
- QString qs = s2q(s);
- emit sendMessage(qs);
- }
- signals:
- void sendMessage(const QString name);
- public slots:
- bool startThread();
- protected:
- void run();
- };
- #endif // CRAWLER_H
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include <QMainWindow>
- #include<QFile>
- #include<QMessageBox>
- #include<QTextStream>
- #include<QLineEdit>
- #include<QDebug>
- #include"crawler.h"
- #include"urlThread.h"
- namespace Ui {
- class MainWindow;
- }
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
- public:
- explicit MainWindow(QWidget *parent = );
- ~MainWindow();
- void receiveMessage(const QString name);
- public slots:
- void beginGeturl();
- void open();
- private:
- Ui::MainWindow *ui;
- Crawler crawler;
- urlThread uth;
- };
- #endif // MAINWINDOW_H
- #ifndef URLTHREAD_H
- #define URLTHREAD_H
- #include"crawler.h"
- class urlThread: public QThread
- {
- Q_OBJECT
- public slots:
- void startThread();
- void open();
- void send(string s)
- {
- QString qs = s2q(s);
- emit sendMessage(qs);
- }
- signals:
- void sendMessage(const QString name);
- protected:
- void run();
- };
- #endif // URLTHREAD_H
- <?xml version="1.0" encoding="UTF-8"?>
- <ui version="4.0">
- <class>MainWindow</class>
- <widget class="QMainWindow" name="MainWindow">
- <property name="geometry">
- <rect>
- <x></x>
- <y></y>
- <width></width>
- <height></height>
- </rect>
- </property>
- <property name="windowTitle">
- <string>MainWindow</string>
- </property>
- <widget class="QWidget" name="centralWidget">
- <widget class="QLabel" name="label">
- <property name="geometry">
- <rect>
- <x></x>
- <y></y>
- <width></width>
- <height></height>
- </rect>
- </property>
- <property name="text">
- <string>初始网址:</string>
- </property>
- </widget>
- <widget class="QLineEdit" name="site">
- <property name="geometry">
- <rect>
- <x></x>
- <y></y>
- <width></width>
- <height></height>
- </rect>
- </property>
- <property name="text">
- <string>http://www.scut.edu.cn</string>
- </property>
- </widget>
- <widget class="QLabel" name="label_2">
- <property name="geometry">
- <rect>
- <x></x>
- <y></y>
- <width></width>
- <height></height>
- </rect>
- </property>
- <property name="text">
- <string>搜索深度:</string>
- </property>
- </widget>
- <widget class="QLineEdit" name="depth">
- <property name="geometry">
- <rect>
- <x></x>
- <y></y>
- <width></width>
- <height></height>
- </rect>
- </property>
- <property name="text">
- <string></string>
- </property>
- </widget>
- <widget class="QLabel" name="label_3">
- <property name="geometry">
- <rect>
- <x></x>
- <y></y>
- <width></width>
- <height></height>
- </rect>
- </property>
- <property name="text">
- <string>过滤字符串:</string>
- </property>
- </widget>
- <widget class="QLineEdit" name="filter">
- <property name="geometry">
- <rect>
- <x></x>
- <y></y>
- <width></width>
- <height></height>
- </rect>
- </property>
- <property name="text">
- <string>scut</string>
- </property>
- </widget>
- <widget class="QPushButton" name="start">
- <property name="geometry">
- <rect>
- <x></x>
- <y></y>
- <width></width>
- <height></height>
- </rect>
- </property>
- <property name="text">
- <string>开始</string>
- </property>
- </widget>
- <widget class="QPushButton" name="display">
- <property name="geometry">
- <rect>
- <x></x>
- <y></y>
- <width></width>
- <height></height>
- </rect>
- </property>
- <property name="text">
- <string>显示url</string>
- </property>
- </widget>
- <widget class="QTextBrowser" name="textBrowser">
- <property name="geometry">
- <rect>
- <x></x>
- <y></y>
- <width></width>
- <height></height>
- </rect>
- </property>
- </widget>
- </widget>
- <widget class="QMenuBar" name="menuBar">
- <property name="geometry">
- <rect>
- <x></x>
- <y></y>
- <width></width>
- <height></height>
- </rect>
- </property>
- </widget>
- <widget class="QToolBar" name="mainToolBar">
- <attribute name="toolBarArea">
- <enum>TopToolBarArea</enum>
- </attribute>
- <attribute name="toolBarBreak">
- <bool>false</bool>
- </attribute>
- </widget>
- <widget class="QStatusBar" name="statusBar"/>
- </widget>
- <layoutdefault spacing="" margin=""/>
- <resources/>
- <connections/>
- </ui>
ui文件要自己设计
爬虫还有一些小问题,抓取的url content并不完全,某些地址还有一点小问题...
貌似博客园没有上传附件的地方?还是我没找到?希望得到提示~
c++ 爬虫的更多相关文章
- 设计爬虫Hawk背后的故事
本文写于圣诞节北京下午慵懒的午后.本文偏技术向,不过应该大部分人能看懂. 五年之痒 2016年,能记入个人年终总结的事情没几件,其中一个便是开源了Hawk.我花不少时间优化和推广它,得到的评价还算比较 ...
- Scrapy框架爬虫初探——中关村在线手机参数数据爬取
关于Scrapy如何安装部署的文章已经相当多了,但是网上实战的例子还不是很多,近来正好在学习该爬虫框架,就简单写了个Spider Demo来实践.作为硬件数码控,我选择了经常光顾的中关村在线的手机页面 ...
- Python 爬虫模拟登陆知乎
在之前写过一篇使用python爬虫爬取电影天堂资源的博客,重点是如何解析页面和提高爬虫的效率.由于电影天堂上的资源获取权限是所有人都一样的,所以不需要进行登录验证操作,写完那篇文章后又花了些时间研究了 ...
- scrapy爬虫docker部署
spider_docker 接我上篇博客,为爬虫引用创建container,包括的模块:scrapy, mongo, celery, rabbitmq,连接https://github.com/Liu ...
- scrapy 知乎用户信息爬虫
zhihu_spider 此项目的功能是爬取知乎用户信息以及人际拓扑关系,爬虫框架使用scrapy,数据存储使用mongo,下载这些数据感觉也没什么用,就当为大家学习scrapy提供一个例子吧.代码地 ...
- 120项改进:开源超级爬虫Hawk 2.0 重磅发布!
沙漠君在历时半年,修改无数bug,更新一票新功能后,在今天隆重推出最新改进的超级爬虫Hawk 2.0! 啥?你不知道Hawk干吗用的? 这是采集数据的挖掘机,网络猎杀的重狙!半年多以前,沙漠君写了一篇 ...
- Python爬虫小白入门(四)PhatomJS+Selenium第一篇
一.前言 在上一篇博文中,我们的爬虫面临着一个问题,在爬取Unsplash网站的时候,由于网站是下拉刷新,并没有分页.所以不能够通过页码获取页面的url来分别发送网络请求.我也尝试了其他方式,比如下拉 ...
- Python多线程爬虫爬取电影天堂资源
最近花些时间学习了一下Python,并写了一个多线程的爬虫程序来获取电影天堂上资源的迅雷下载地址,代码已经上传到GitHub上了,需要的同学可以自行下载.刚开始学习python希望可以获得宝贵的意见. ...
- QQ空间动态爬虫
作者:虚静 链接:https://zhuanlan.zhihu.com/p/24656161 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 先说明几件事: 题目的意 ...
- 让你从零开始学会写爬虫的5个教程(Python)
写爬虫总是非常吸引IT学习者,毕竟光听起来就很酷炫极客,我也知道很多人学完基础知识之后,第一个项目开发就是自己写一个爬虫玩玩. 其实懂了之后,写个爬虫脚本是很简单的,但是对于新手来说却并不是那么容易. ...
随机推荐
- Android 常用工具类之RuntimeUtil
public class RuntimeUtil { /** 通过查询su文件的方式判断手机是否root */ public static boolean hasRootedSilent() { re ...
- init_MUTEX 与 sema_init 函数【转】
转自:http://blog.chinaunix.net/uid-7332782-id-3211627.html 在编译Linux设备驱动程序学习(1)-字符设备驱动程序中scull.c程序时,报错: ...
- taglib指令
taglib:用来引用标签库并设置标签库的前缀,(允许JSP页面使用用户自定义标签) 语法:<%@ taglib uri="tagLibraryURI" prefix=&qu ...
- windows win10上传文件到linux服务器
1.最直接当然使用终端secucrt和xshell putty之类的,然后使用sz rz 2.如果服务器端不支持sz rz可以使用scp命令,下面这个pscp.exe就是支持scp的,基于ssh,很好 ...
- MyISAM表杂记实验
一.本文说明 由于刚学mysql所以动手做了一些实验. 二.实验内容 1.验证MyISAM有AUOT_INCREMENT coloumn功能 ----在这里是对现有表t,增加一个主键----mysql ...
- 在MyEclipse中搭建Spring MVC开发环境
环境版本 IDE:MyEclipse 8.5 Spring:spring 3.2.8 JDK:1.6 1.打开MyEclipse-->File-->New-->Web Project ...
- 关于sql server 2008过期导致 MSSQLSERVER服务就无法启动,手动启动就报告错误代码17051。
1.基本现象:MSSQLSERVER服务就无法启动,手动启动就报告17051错误. 2.解决办法: 第一步:进入SQL2008配置工具中的安装中心, 第二步:再进入维护界面,选择版本升级, 第三步:进 ...
- java 判断某一天是当年的哪一天
题目:输入年份,月份,日,判断这一天是这一年的第几天?(闰年的2月份为29天,平年为28天) public class Runnian { /** * 能被4整除且不能被100整除或者能被400整除的 ...
- 递归,动态规划,找最短路径,Help Jimmy
题目链接:http://poj.org/problem?id=1661 解题报告: 1.老鼠每次来到一块木板上都只有两条路可以走,可以使用递归 #include <stdio.h> #in ...
- sql 清空所有数据
近来发现数据库过大,空间不足,因此打算将数据库的数据进行全面的清理,但表非常多,一张一张的清空,实在麻烦,因此就想利用SQL语句一次清空所有数据.找到了三种方法进行清空.使用的数据库为MS SQL S ...