#include <iostream>
#include <string>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <strings.h>
#include <string.h>
#include <unistd.h>
using namespace std; void func()
{
string url;
cout << "输入网址:" << endl;
cin >> url; //依据域名获取ip地址
struct hostent *website_host = NULL;
website_host = gethostbyname(url.c_str());
if (website_host == NULL)
{
perror("gethostbyname error");
exit(-1);
}
cout << "主机名称:";
cout << website_host->h_name << endl;
cout << "地址类型:";
cout << website_host->h_addrtype << endl;
cout << "地址长度:";
cout << website_host->h_length << endl; //建立socket描写叙述符
int sockfd;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1)
{
perror("socket error");
exit(-1);
}
cout << "建立socket完毕" << endl; //初始化地址结构
struct sockaddr_in website_addr;
bzero((void*)&website_addr, sizeof(website_addr));
website_addr.sin_family = AF_INET;
website_addr.sin_port = htons(80);
website_addr.sin_addr.s_addr = ((struct in_addr *)(website_host->h_addr))->s_addr;
cout << "地址初始化完毕" << endl; //连接
int ret;
ret = connect(sockfd, (struct sockaddr*)&website_addr, sizeof(website_addr));
if (ret == -1)
{
perror("connect error");
exit(-1);
}
cout << "连接完毕" << endl; //向80端口发送http头
char buf[10*1024];
char addr[100];
sprintf(buf, "GET / HTTP/1.1\r\n");
strcat(buf, "Host:");
strcat(buf,url.c_str());
strcat(buf, "\r\n");
strcat(buf, "Accept: */*\r\n");
strcat(buf, "User-Agent: Mozilla/4.0(compatible)\r\n");
strcat(buf, "connection:Keep-Alive\r\n");
strcat(buf, "\r\n\r\n");
cout << "请求头构造完毕" << endl;
cout << buf << endl;
ret = send(sockfd, buf, strlen(buf), 0);
cout << "发送完毕" << endl;
cout << "send:\n" << ret << endl; //打开接收文件
int fd;
fd = open("recv.html", O_RDWR);
if (fd == -1)
{
perror("open error");
exit(-1);
} //開始接收
while(1)
{
ret = recv(sockfd, buf, sizeof(buf), 0);
if (ret == 0)
{
cout << "对端关闭" << endl;
exit(-1);
}
if (ret == -1)
{
perror("read error");
exit(-1);
}
buf[ret] = 0;
cout << "recv:" << ret << endl;
cout << buf << endl;
write(fd, buf, strlen(buf));
}
} int main()
{
func();
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

socket抓取网页的更多相关文章

  1. 使用Socket抓取网页源码

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...

  2. [转]Linux Socket编程 Socket抓取网页源码

    “一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的socket. ——有感于实际编程和开源项目研究. 我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览 ...

  3. python分布式抓取网页

    呵呵,前两节好像和python没多大关系..这节完全是贴代码, 这是我第一次写python,很多地方比较乱,主要就看看逻辑流程吧. 对于编码格式确实搞得我头大..取下来页面不知道是什么编码,所以先找c ...

  4. C语言Linix服务器网络爬虫项目(二)项目设计和通过一个http请求抓取网页的简单实现

    我们通过上一篇了解了爬虫具体要实现的工作之后,我们分析得出的网络爬虫的基本工作流程如下: 1.首先选取一部分精心挑选的种子URL: 2.将这些URL放入待抓取URL队列: 3.从待抓取URL队列中取出 ...

  5. 使用wget工具抓取网页和图片 成功尝试

    使用wget工具抓取网页和图片 发表于1年前(2014-12-17 11:29)   阅读(2471) | 评论(14) 85人收藏此文章, 我要收藏 赞7 wget 网页抓取 图片抓取 目录[-] ...

  6. Java 抓取网页中的内容【持续更新】

    背景:前几天复习Java的时候看到URL类,当时就想写个小程序试试,迫于考试没有动手,今天写了下,感觉还不错 内容1. 抓取网页中的URL 知识点:Java URL+ 正则表达式 import jav ...

  7. C语言调用curl库抓取网页图片

    思路是先用curl抓取网页源码,然后以关键字寻找出图片网址.   #include <stdio.h> #include <stdlib.h> #include <str ...

  8. [转载]爬虫的自我解剖(抓取网页HtmlUnit)

    网络爬虫第一个要面临的问题,就是如何抓取网页,抓取其实很容易,没你想的那么复杂,一个开源HtmlUnit包,4行代码就OK啦,例子如下: 1 2 3 4 final WebClient webClie ...

  9. C语言调用curl库抓取网页图片(转)

    思路是先用curl抓取网页源码,然后以关键字寻找出图片网址.  范例: #include <stdio.h> #include <stdlib.h> #include < ...

随机推荐

  1. Learning To Rank之LambdaMART前世今生

    1.       前言 我们知道排序在非常多应用场景中属于一个非常核心的模块.最直接的应用就是搜索引擎.当用户提交一个query.搜索引擎会召回非常多文档,然后依据文档与query以及用户的相关程度对 ...

  2. UDE-00008 ORA-31626 ORA-06512 ORA-25254

    今天在导出一个模式的时候,约140GB,出现例如以下错误: UDE-00008: operation generated ORACLE error 31626 ORA-31626: job does ...

  3. Java实现字符全阵列阵列

    import org.junit.Test; public class AllSort { public void permutation(char[] buf, int start, int end ...

  4. HDU4870:Rating(DP)

    Problem Description A little girl loves programming competition very much. Recently, she has found a ...

  5. Jquery中使用setInterval和setTimeout 容易犯的低级错误

    直接在ready中调用其他方法,会提示缺少对象的错误,解决方法如下: 方法1. 应用jQuery的扩展可以解决这个问题. $(document).ready(function(){ $.extend( ...

  6. Eclipse+Maven创建webapp项目<二> (转)

    Eclipse+Maven创建webapp项目<二> 1.开启eclipse,右键new——>other,如下图找到maven project 2.选择maven project,显 ...

  7. hdu3530Subsequence rmq

    //使用rmq办,ma[i][j],同i作为一个起点2^j阵列的最大长度值 //启动枚举问最长的子列 //枚举的最大长度2^(j-1)和2^(j)z之间 //然后在该范围内找到 #include< ...

  8. CSS: 解决Div float后,父Div无法高度自适应的问题

    在用CSS+DIV的布局中,常常会发现,当一个DIV float之后,假设他的高度超过了其父DIV的高度时,其父DIV的高度并不会对应的进行调整.要解决问题(也叫做闭合(清除)浮动),我们有四种办法: ...

  9. telnet模拟http訪问

    HTTP协议经常使用的无非就那么几个命令 GET HEAD PUT POST 此处简单说下http的GET和HEAD 举两个简单的样例: GET的样例 telnet serverip 80 GET h ...

  10. hdu1052 Tian Ji -- The Horse Racing 馋

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=1052">http://acm.hdu.edu.cn/showproblem.php ...