【问题标题】:PHP Parse webpage with GET Request using file_get_contents on paginated page使用分页页面上的 file_get_contents 使用 GET 请求的 PHP 解析网页
【发布时间】:2016-06-17 21:43:39
【问题描述】:

我正在尝试使用 file_get_contents 解析网页。对于方法 post,我能够解析列表中的第一页。在此之后,页面内容通过分页列出。

我们如何使用 GET 请求解析分页页面的内容?

例如:http://sample.com/results?page=3

代码:

$data = array(
        'page' => 1
        );

    $opts = array(
      'http'=>array(
      'method'=> 'GET',
      'headers' => $http_response_header,
       'content' => http_build_query($data),
       'timeout' => 100
        )

    );

    $context = stream_context_create($opts);

    $listfile1 = file_get_contents("http://sample.com/results?",false, $context);

我得到分页页面的 0 条记录。

【问题讨论】:

  • 我建议使用 cURL 代替更复杂的 URL 解析示例,例如您的。
  • @divix 谢谢。我也尝试过使用 cURL,结果是一样的。 file_get_contents 在循环内执行。每次循环后是否需要关闭任何流或会话?这是因为如果我尝试使用 URL link,首先在循环中,返回正确解析的第二页,然后为空。
  • 确保传递所有必需的标头(如果有的话)。

标签: php http get pagination file-get-contents


【解决方案1】:

我能够使用上面列出的相同函数,定义了一个自定义函数,它打破了分页。

但我也更改了网址。

       $listfile1 = file_get_contents("sample.com/results?page=".$number,false, $context); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-23
    • 2013-11-24
    相关资源
    最近更新 更多