【问题标题】:get data from read more buttons in goutte web scraper in php从 php 中 goutte web scraper 中的 read more 按钮获取数据
【发布时间】:2018-07-22 05:36:26
【问题描述】:

我有一个包含多篇文章和阅读更多按钮的页面。 我想单击每个阅读更多按钮并从该页面获取帖子文本,但我不知道该怎么做。 这是我的php代码。谢谢大家

$client = new Client();
$guzzleClient = new GuzzleClient(array(
     'timeout' => 60,
));
$client->setClient($guzzleClient);
$crawler = $client->request('GET', 'http://example.com/');
$crawler->filter('article > div > p > span > a')->each(function ($node)  use ($client,$crawler)
{
    $crawler = $client->click($crawler->selectLink('read more')->link());
    echo $crawler->html();
    $crawler = $client->request('GET', 'http://example.com/');
});

【问题讨论】:

  • 你现在遇到了什么问题?
  • @AmitMerchant 我真的不知道该怎么做
  • echo $crawler->html(); 说什么?
  • @stef 我只是想测试一下...你知道我该怎么做吗?

标签: php symfony web-scraping goutte


【解决方案1】:

点击阅读更多按钮可能有两种情况

1- 文章将被加载请求将被发送到带有一些参数的新 URL,例如 http://www.example.com?page=2 然后 page=3 等等。

2- 文章将通过 Ajax 请求加载。

如果您有案例 1,那么我们可以轻松点击阅读更多按钮并获取下一篇文章等等。这是您编辑的代码,请注意您必须在 selectLink 函数中编写原始按钮文本,例如 <button>Read More</button> 然后 Read More 将作为参数。

$client = new Client();
$guzzleClient = new GuzzleClient(array(
 'timeout' => 60,
 ));
$client->setClient($guzzleClient);
$crawler = $client->request('GET', 'http://example.com/');
$crawler->filter('article > div > p > span > a')->each(function 
($node)  use ($client,$crawler)
{
$articles = $client->click($crawler->selectLink('As text appear on button')->link());
echo $articles->html(); // this crawler object will have next loaded articles

$article2 = $client->click($articles->selectLink('As text appear on button')->link());

// article2 will have have next latest articles then article3 and so on.

$crawler = $client->request('GET', 'http://example.com/');
});

我们知道 Guzzle 不支持启用 Javascript 的网站,这意味着案例 2 无法使用 goutte click 功能,我们需要跟踪 ajax 请求 URL 和参数,然后向该 URL 发送 Post 或 Get 请求并根据一些过滤文章选择器。

【讨论】:

    猜你喜欢
    • 2019-02-13
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 2022-01-17
    • 2020-01-23
    • 1970-01-01
    相关资源
    最近更新 更多