- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在用 C++ 编写一个网络爬虫,想找出读取 http 请求响应的最佳方法。
目前我正在使用一个 1M 大小的缓冲区来包含 read() 中的 recv 消息(read() 的计数是 4K 字节)。这是我要抓取的网页的最大尺寸。然而,这是一种浪费,所以我也在考虑以下其他方法:
第一轮发送http HEAD请求,从header中读取content-length信息。创建一个大小为 content-length 的字符数组,然后发送 http GET 以检索内容。
Q1:如果来自服务器的header信息没有content-lenght怎么办?
Q2:这种方法使网络流量翻倍。付出这样的开销值得吗?
直接发送 http GET 并使用较小的缓冲区(例如 16K 字节)。但在接收到所有数据之前不处理响应,而是在缓冲区已满后处理数据,然后清理缓冲区以接收其余数据。
Q1: 这样,爬虫可能需要几次迭代才能完整读取大型网页。如果处理工作耗时,同时读取多个网页,网络等待数据是否会超出系统缓冲区而导致丢包?
谢谢。
最佳答案
Currently I'm using a buffer of 1M size to contain the recv message in read() (count is 4K bytes for read()). This is the max size of webpage I would like to crawl. However this is sort of waste
确实如此。无论如何,每次读取操作读取的数据不会超过几 K,因此巨大的缓冲区毫无意义。
Send http HEAD request in the first round and read the content-length info from header. Create a char array with size of content-length and send http GET then to retrieve the content.
那是另一个网络操作。也很浪费。
Q1: What if the header info from server does not have content-length?
不确定这对 HEAD 是否有效,但您必须检查 RFC。
Q2: This approach doubles the network traffic.
不,它没有。它使请求/响应对的数量加倍。这不是一回事。
Is it worthy paying such overhead?
没有。
Send http GET directly and using a smaller buffer (e.g. 16K bytes).
当然。
But not processing the response until all data is received
为什么不呢?为什么不在收到时处理它?这是最好的方法。最小的缓冲区,最低的延迟。
instead processing the data once the buffer is full and then clean the buffer to receive the rest.
您永远不需要清理缓冲区。
Q1: In this way the crawler may need a few iterations to read a large webpage completely
您总是需要迭代才能从网络中读取网页或任何其他内容。 recv() 函数只保证在阻塞模型中传输至少一个字节,除非出现 EOS 或错误。它没有义务填充缓冲区,除非您的套接字接收缓冲区也是 1M 并且您在读取之间浪费了足够的时间,否则它已经填充了。如果您正确编程,就不会发生这种情况。
If the processing job is time-costly and multiple webpages are being read at the same time, could the waiting data from network exceeds system buffer and cause packet loss?
不在 TCP 中。这只会导致发件人停顿并浪费时间。
关于c++ - 网络爬虫在 read() 中下载网页的 recv 缓冲区应该有多大?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23102726/
这个问题在这里已经有了答案: Possible to make an event handler wait until async / Promise-based code is done? (2
我经常有多个运行的进程(R,Python,eshell/shell),对于每个进程,我经常都有一个相关的脚本,可以从中发送摘要。为此,我通常将每个框架垂直地分成两个窗口,以便脚本文件(例如.py)位于
如何修改 emacs 在关闭缓冲区后选择要显示的缓冲区的方式? 当我有多个列显示相同的缓冲区,然后在其中一个缓冲区中打开另一个文件,然后关闭新打开的缓冲区时,它不会切换回前一个缓冲区,而是切换到另一个
如何将 ex 命令复制到剪贴板或粘贴到缓冲区? 在 Windows 上使用 gvim。 最佳答案 windows剪贴板可以通过the buffer + 访问.因此,可以使用 + 将剪贴板粘贴为前命令。
在 javascript 中如何以比以下更简单的方式获取 b 缓冲区? var num=6553599 var a = new Buffer(4); a.writeInt32LE(num)
每次我在 Google 上搜索有关 OpenGL 编程的文章时,我都会找到一些文章,但似乎所有文章都提到了着色器和缓冲区。那些是什么?你能解释其中的一些吗: 深度缓冲区 模板缓冲区 像素着色器 帧缓冲
我有java考试,当我学习时,我看到了这个练习,我尝试解决它,但我发现一些困难,所以请帮助我考虑实用程序中方法的以下注释、 header 和部分代码名为 Atbash 的加密类。 /**
每次我在 Google 上搜索有关 OpenGL 编程的文章时,我都会找到一些文章,但似乎所有文章都提到了着色器和缓冲区。那些是什么?你能解释其中的一些吗: 深度缓冲区 模板缓冲区 像素着色器 帧缓冲
对于每个属性使用跨步顶点缓冲区与紧密打包缓冲区有何优缺点?我的意思是例如: 步幅:xyzrgb xyzrgb xyzrgb 紧:xyzxyzxyz rgbrgbrgb 乍一看,使用步幅时您似乎可以轻松
我正在尝试将文本文件中每行的数字读取到 ArrayList 中。当我执行以下函数时,它总是跳过最后一个元素。有人可以帮我吗?因为我在这里没有遇到问题,因为它读取直到缓冲区为空,所以他应该在到达 Fil
#include #include int main () { time_t time_raw_format; struct tm * ptr_time; char *buff
基本上我有一个包含不同类型数据的自定义结构。例如: typedef struct example_structure{ uint8_t* example_1[4]; int example_2[4];
我之前的列表实现是一个简单的 LinearLayout,位于一个装满我的项目的 ScrollView 中。 我切换到 ListView 的 Android 实现以简单地使用 CursorAdapter
我想创建一个可变长度的输入事件窗口/缓冲区,当它接收到额外的事件时会变长。 这是为了实现“键入时搜索”功能。我想捕获点击,但为了不给服务器造成压力,我想明智地进行服务调用。 我想到的逻辑是缓冲击键,从
我想将 yuv420P 像素写入缓冲区而不是二进制文件。假设我在指针中存储了 luma 、 Cb 和 Cr。 luma = output_pixel.luma; cb = output_pixel.c
我想在 Go 中构建一个支持多个并发读取器和一个写入器的缓冲区。所有写入缓冲区的内容都应由所有读者读取。允许新读者随时加入,这意味着已经写入的数据必须能够为迟到的读者回放。 缓冲区应满足以下接口(in
本文转载自微信公众号「小明菜市场」,作者小明菜市场。转载本文请联系小明菜市场公众号。 前言 Java NIO 需要理解的主要有缓冲区,通道,选择器,这三个主要的部分。 基础
一 点睛 NIO,可以称为 New IO 或 Non Blocking IO,是在 JDK 1.4 后提供的新 API。传统的I/O 是阻塞式的 I/O、面向流的操作;而 NIO 是非阻塞 I/O 、
我正在寻找一种切换到包含搜索文本的缓冲区的方法。 例如。如果我打开了 100 个缓冲区,我想切换到一个包含 'fooBar = 1' 的缓冲区 最佳答案 我写了一个 Vim 插件来做到这一点:buff
我正在尝试将提取的视频帧(我使用 ffmpeg)推送到 FFMPEG 缓冲区中。我已经查看了 ffmpeg 的缓冲区源文件,例如 buffersrc.c 和 fifo.c,以确定我是否可以这样做,但我
我是一名优秀的程序员,十分优秀!