php cURL 操作在 120308 毫秒后超时,接收到 -1 字节中的 X

标签 php curl screen-scraping

我偶尔会在我的抓取脚本中遇到这个错误(见标题)。

X 是整数字节数 > 0,网络服务器在响应中发送的实际字节数。我用 Charles 代理调试了这个问题,这是我所看到的

Charles debug session with problematic request

如您所见,响应中没有 Content-Length: header ,代理仍在等待数据(因此 cURL 等待 2 分钟后放弃)

cURL 错误代码为 28。

下面是使用该请求的 var_export'ed curl_getinfo() 的详细 curl 输出的一些调试信息:

* About to connect() to proxy 127.0.0.1 port 8888 (#584)
*   Trying 127.0.0.1...
* Adding handle: conn: 0x2f14d58
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 584 (0x2f14d58) send_pipe: 1, recv_pipe: 0
* Connected to 127.0.0.1 (127.0.0.1) port 8888 (#584)
> GET http://bakersfield.craigslist.org/sof/3834062623.html HTTP/1.0
User-Agent: Firefox (WindowsXP) Ц Mozilla/5.1 (Windows; U; Windows NT 5.1; en-GB
; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6
Host: bakersfield.craigslist.org
Accept: */*
Referer: http://bakersfield.craigslist.org/sof/3834062623.html
Proxy-Connection: Keep-Alive

< HTTP/1.1 200 OK
< Cache-Control: max-age=300, public
< Last-Modified: Thu, 11 Jul 2013 21:50:17 GMT
< Date: Thu, 11 Jul 2013 21:50:17 GMT
< Vary: Accept-Encoding
< Content-Type: text/html; charset=iso-8859-1
< X-MCP-Cache-Control: max-age=2592000, public
< X-Frame-Options: SAMEORIGIN
* Server Apache is not blacklisted
< Server: Apache
< Expires: Thu, 11 Jul 2013 21:55:17 GMT
* HTTP/1.1 proxy connection set close!
< Proxy-Connection: Close
<
* Operation timed out after 120308 milliseconds with 4636 out of -1 bytes receiv
ed
* Closing connection 584
Curl error: 28 Operation timed out after 120308 milliseconds with 4636 out of -1
 bytes received http://bakersfield.craigslist.org/sof/3834062623.htmlarray (
  'url' => 'http://bakersfield.craigslist.org/sof/3834062623.html',
  'content_type' => 'text/html; charset=iso-8859-1',
  'http_code' => 200,
  'header_size' => 362,
  'request_size' => 337,
  'filetime' => -1,
  'ssl_verify_result' => 0,
  'redirect_count' => 0,
  'total_time' => 120.308,
  'namelookup_time' => 0,
  'connect_time' => 0,
  'pretransfer_time' => 0,
  'size_upload' => 0,
  'size_download' => 4636,
  'speed_download' => 38,
  'speed_upload' => 0,
  'download_content_length' => -1,
  'upload_content_length' => 0,
  'starttransfer_time' => 2.293,
  'redirect_time' => 0,
  'certinfo' =>
  array (
  ),
  'primary_ip' => '127.0.0.1',
  'primary_port' => 8888,
  'local_ip' => '127.0.0.1',
  'local_port' => 63024,
  'redirect_url' => '',
)

我可以做一些事情,比如添加 curl 选项来避免这些超时吗?这不是连接超时,也不是数据等待超时——这两个设置都不起作用,因为 curl 实际上连接成功并接收了一些数据,所以错误超时总是 ~= 120000 毫秒。

最佳答案

我注意到您正在尝试解析 Craigslist;难道是他们的防洪保护? 如果您尝试解析其他网站,问题是否仍然存在?我曾经在尝试递归映射 FTP 时遇到同样的问题。

关于超时,如果您确定这既不是连接超时也不是数据等待超时 (CURLOPT_CONNECTTIMEOUT/CURLOPT_TIMEOUT),我会尝试增加 PHP 脚本本身的限制:

set_time_limit(0);

关于php cURL 操作在 120308 毫秒后超时,接收到 -1 字节中的 X,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17610808/

相关文章:

Python 抓取空标签

c# - 在一个网站中抓取所有网页的最快方法

php - 我如何编辑带有附件的表单 [Symfony]

具有静态方法访问的变量中的 PHP 引用类

php - 使用 PHP 变量加载 View 的一部分

c# - C# 上 CURLOPT_RETURNTRANSFER 的等价物是什么?

php - 如何在 php 中不显示 cURL 响应数据?

php - 使用 cURL 通过 javascript 抓取网站

PHP 从两个表中选择并回显表名

python - 如何使用 lxml、XPath 和 Python 从网页中提取链接?