libcurl获取网页
来源:互联网 发布:中国程序员地域分布图 编辑:程序博客网 时间:2024/06/10 18:26
libcurl官方链接 https://curl.haxx.se/libcurl/
程序功能:
- doc()函数,通过时间获得一个随机数,随机数实际上是微妙数。因为执行的时候重复的几率很小,姑且认为是随机的。
- errMsg()函数,记录日志,输出信息
- writeFunc()函数,这是curl写文件的回调函数,函数内部调用fwrite实现。
- getContent()函数,这是主体部分,接受url参数并将获得的网页内容解压之后存储在函数doc()返回值为名字的文件里。
说明:
内容解压通过curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, “”);实现,这句会自动解析libcurl所支持的压缩格式,否则不作处理。
这就免去了在块传输时候自己需要组包,解压的麻烦。
改进:
现在支持以ip,域名为参数。但是在以域名为参数时候会增加DNS的时间,这部分时间可能比较长(视情况而定)。所以能够缩短这部分时间将会,加快内容获取的速度。
这可以通过curl_easy_setopt()设置http头而解决。
源代码如下:
#include "stdio.h"#include "stdlib.h"#include "errno.h"#include "syslog.h"#include "sys/types.h"#include "sys/stat.h"#include "fcntl.h"#include "unistd.h"#include <time.h>#include <sys/time.h>#include "curl/curl.h"//const char *file()int doc(){ struct timeval tv; int ret = gettimeofday(&tv, NULL); if(ret){ return rand(); } //create a file and return name return (int)tv.tv_usec;}static void errMsg(const char *msg, int level){ char buf[256] = {0}; sprintf(buf, "[File: %s, line: %d] msg: %s\n", __FILE__, __LINE__, msg); fprintf(stderr, "%s", buf); syslog(level, "%s", buf);}static size_t writeFunc(void *ptr, size_t size, size_t nmemb, void *stream){ size_t count = fwrite(ptr, size, nmemb, (FILE*)stream); return count;}int getContent(const char *url){ int name = doc(); char fname[64] = {0}; sprintf(fname, "%d", name); FILE *fp = fopen(fname, "a"); if(NULL == fp){ errMsg("error fopen()", LOG_WARNING); return -1; } CURL *curl= curl_easy_init(); if(NULL == curl){ errMsg("error curl_easy_init()", LOG_WARNING); return -1; } curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, ""); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeFunc); curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); CURLcode res = curl_easy_perform(curl); if(CURLE_OK != res){ char buf[256] = {0}; sprintf(buf, "error %s: %s", __FUNCTION__, curl_easy_strerror(res)); errMsg(buf, LOG_WARNING); } curl_easy_cleanup(curl); return name;}int main(int argc, char **argv){ getContent(argv[1]); return 0;}
编译:
gcc -o test curl.c -lrt -lcurl
接下来要做:
关键字检测,不出意外是多模匹配的AC算法。一定要等我看懂论文,调好代码。。。
0 0
- libcurl获取网页
- 使用libcurl库获取网页源码
- 使用libcurl 函数库获取网页数据代码
- 入门libcurl之获取网页源码
- libcurl获取网页内容的PHP代码
- win7+vs2012编译、使用libcurl获取网页源代码
- 使用libcurl获取经过gzip压缩的网页文件
- win7+vs2012编译、使用libcurl获取网页源代码
- 使用libcurl获取经过gzip压缩的网页文件
- 使用libcurl库获取网页信息(C++ php)
- libcurl获取网页内容的C/C++代码
- 用htmlcxx解析从libcurl中获取到的web网页源码
- 如何在Windows+VS2005使用最新静态libcurl 7.35.0获取网页数据,支持HTTPS
- c++利用libcurl获取http网页的 response headers 等信息
- libcurl 获取服务器文件大小
- libcurl远程获取文件大小源码
- libcurl保存网页内容以及图片
- libcurl抓取网页并保存cookie
- 2017天猫算法工程师面试
- HBase负载均衡之集群负载的评分方法
- 基于RF的web自动化测试
- java集合操作-去重
- 2017蓝桥杯模拟赛 Problem D: 字符串比较(9分)
- libcurl获取网页
- thinkPHP5下扩展encryptedData解密算法文件的注意事项
- CodeSmith 根据表生产SQL存储insert/updata过程 和 C#调用存储过程代码
- 本地文件同步在远程服务器上使用
- centos启用ftp功能
- C/C++中的那些不同:结构体和联合体
- C/C++函数实现接口
- 2017蓝桥杯模拟赛 Problem E: 还款计算(13分)
- Java注解