我正在优化我的简单网络爬虫(目前使用 PHP/curl_multi)。
目标是智能抓取整个网站,并跳过非 html 内容。我尝试使用 nobody,并且只发送 HEAD 请求,但这似乎并不适用于每个网站(某些服务器不支持 HEAD),导致 exec 暂停很长时间(有时比加载页面本身长得多)。
有没有其他方法可以在不下载全部内容的情况下获取页面类型,或者如果文件不是 html 则强制 CURL 放弃下载?
(编写我自己的 http 客户端不是一个选项,因为我打算稍后将 CURL 函数用作 cookie 和 ssl)。
最佳答案
我还没有尝试过,但我看到了 CURLOPT_PROGRESSFUNCTION
。我敢打赌,您可以逐步阅读响应以查找 content-type
header ,可能还有 curl_close () 句柄,如果您对正在下载的内容不感兴趣。
CURLOPT_PROGRESSFUNCTION The name of a callback function
where the callback function takes three parameters. The first is the
cURL resource, the second is a file-descriptor resource, and the
third is length. Return the string containing the data.
关于php - 如何只加载 html(并跳过媒体文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12101838/