#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. 创建ASPState数据库

    原文:创建ASPState数据库 在C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727下找到生成ASPState的sql:InstallSqlState.sql ...

  2. Java4Android之BlockingQueue

    在研究Smack的源码的时候,我对它的连接Connection以及派生类XMPPConnection的关注是最多的,由于一个即时通信程序,它的网络模块必是它的核心. 而我非常在乎它是怎样实现的. 在收 ...

  3. RPC模式的Hub操作

    signalR 专题—— 第四篇 模拟RPC模式的Hub操作   在之前的文章中,我们使用的都是持久连接,但是使用持久连接的话,这种模拟socket的形式使用起来还是很不方便的,比如只有一个唯一的 O ...

  4. Bulk Insert Data

    Bulk Insert Data 命名空间:Oracle.DataAccess.Client 组件:Oracle.DataAccess.dll(2.112.1.0) ODP.NET 版本:ODP.NE ...

  5. 2014华为机试西安地区B组试题

    2014华为机试西安地区B组试题 题目一.亮着点灯的盏数 一条长廊里依次装有n(1≤n≤65535)盏电灯,从头到尾编号1.2.3.-n-1.n.每盏电灯由一个拉线开关控制.開始,电灯所有关着. 有n ...

  6. Codeforces554D:Kyoya and Permutation

    Let's define the permutation of length n as an array p = [p1, p2, ..., pn] consisting of n distinct ...

  7. Java、PHP训练场地选择成都传祺播客

    传智播客选择九类基础: 1 有不怕炫耀实力,我们会爱一本书,是一个开源项目 2 领先的新技术,让我们的学生走在别人前面,首先推出Hadoop.Unity3D.Nginx. 3 课程广博的知识,深入的技 ...

  8. Git打补丁常见问题

    Git打补丁常见问题 往往觉得得到某个功能的补丁就觉得这个功能我就已经成功拥有了,可是在最后一步的打补丁的工作也是须要相当慎重的,甚至有可能还要比你获取这个补丁花费的时间还要多.看到好多同行遇到这个问 ...

  9. 深入浅出JMS(一)——JMS简要

    假设手机只能实时通话.没有邮件和短信功能发生?一个电话回来.只是没有足够的时间去连接.然后传递这款手机的信息肯定是不接受. 么不能先将信息存下来.当用户须要查看信息的时候再去获得信息呢?伴随着这个疑惑 ...

  10. 【UML】概念、关联、画画(一)

    最近画UML画画,于UML观看视频后还没有学会.它是的结果UML九图是不是太懂,我想加深绘制过程的理解,我一个新的水平. 现在我觉得是时候..地介绍一下UML.了解一下它的基本内容.达到深入浅出的效果 ...