gpt4 book ai didi

php - 检索部分网页

转载 作者:可可西里 更新时间:2023-11-01 00:11:57 26 4
gpt4 key购买 nike

有什么方法可以限制 CURL 获取的数据量吗?我正在从一个 50kb 的页面上抓取数据,但是我需要的数据位于页面的顶部 1/4,因此我实际上只需要检索页面的前 10kb。

我问这个问题是因为我需要监控大量数据,这导致我每月传输近 60GB 的数据,而其中只有大约 5GB 的带宽是相关的。

我正在使用 PHP 来处理数据,但是我的数据检索方法很灵活,我可以使用 CURL、WGET、fopen 等。

我正在考虑的一种方法是

$fp = fopen("http://www.website.com","r");
fseek($fp,5000);
$data_to_parse = fread($fp,6000);

以上是否意味着我只会从 www.website.com 传输 6kb,或者 fopen 将 www.website.com 加载到内存中意味着我仍会传输完整的 50kb?

最佳答案

这实际上是一个 HTTP 而不是一个 CURL 问题。

如您所料,如果您使用 fopen,将下载整个页面。无论您是否在偏移量 5000 处寻找。

如 HTML RFC ( http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html ) 中所述,实现您想要的效果的最佳方法是使用部分 HTTP GET 请求:

The semantics of the GET method change to a "partial GET" if the request message includes a Range header field. A partial GET requests that only part of the entity be transferred, as described in section 14.35. The partial GET method is intended to reduce unnecessary network usage by allowing partially-retrieved entities to be completed without transferring data already held by the client.

此处描述了使用范围的部分 GET 请求的详细信息: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35.2

关于php - 检索部分网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1538952/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com