【问题标题】:Opening several javascript link from page从页面打开几个 javascript 链接
【发布时间】:2020-03-11 16:54:41
【问题描述】:

我正在使用"fabpot/goutte": "^3.2" 并使用PHP 7.3.5

我正在尝试访问以下页面并单击链接 - https://www.forexfactory.com/calendar.php?month=nov.2019 - 打开以下框:

我试图过滤所有这些链接,但是 no links are found:

$subCrawler->filter('td.calendar__cell.calendar__detail.detail > a')->each(function ($node) {
    $link = $node->link();
    print $link ."\n";
    print $node->text() ."\n";
});

有什么建议如何点击带有goutte的链接并获取源文本和通常的效果文本?

【问题讨论】:

  • PHP5.3 现在非常陈旧且不安全。你能升级吗
  • @RiggsFolly 是的,我可以!
  • 我不确定过滤器是否支持链式类名。你试过filter('td.calendar__detail > a')filter('a.calendar_detail') 吗?
  • @FerdyPruis 这行不通,因为 DOM 中不存在这些链接。当您单击事件详细信息(文件夹图标)时,它会发出 GET 请求并附加到 DOM。

标签: php goutte


【解决方案1】:

使用 Goutte:

<?php
require 'vendor/autoload.php';
use Goutte\Client;
use Symfony\Component\DomCrawler\Crawler;


$x = 1;
$LIMIT = 20; 

$client = new Client();
$crawler = $client->request('GET', 'https://www.forexfactory.com/calendar.php?month=nov.2019');
$resArray = array();
$TEMP = array();


$crawler->filter('.calendar_row')->each(function ($node) {
    global $x;
    global $LIMIT;
    global $resArray;
    global $TEMP;
    $x++;


    $EVENTID   = $node->attr('data-eventid');


    $API_RESPONSE = file_get_contents('https://www.forexfactory.com/flex.php?do=ajax&contentType=Content&flex=calendar_mainCal&details='.$EVENTID);

    $API_RESPONSE = str_replace("<![CDATA[","",$API_RESPONSE);
    $API_RESPONSE = str_replace("]]>","",$API_RESPONSE);

$html = <<<HTML
<!DOCTYPE html>
<html>
    <body>
       $API_RESPONSE
    </body>
</html>
HTML;

 $subcrawler = new Crawler($html);

 $subcrawler->filter('.calendarspecs__spec')->each(function ($LEFT_TD) {
     global $resArray;
     global $TEMP;
     $LEFT_TD_INNER_TEXT = trim($LEFT_TD->text());

     if($LEFT_TD_INNER_TEXT == "Source"){

            $TEMP = array(); 
            $LEFT_TD->nextAll()->filter('a')->each(function ($LINK) {
                global $TEMP;   
                array_push($TEMP,$LINK->text(),$LINK->attr('href'));
            });

            $EVENT['sourceTEXT'] = $TEMP[0];
            $EVENT['sourceURL']  = $TEMP[1];
            $EVENT['latestURL']  = $TEMP[3];

            array_push($resArray,$EVENT);
    }

});   

  if($x>$LIMIT){
        echo "<pre>"; var_dump($resArray); echo "</pre>";
        exit;
   }

});

使用Simple HTML DOM。你可以从here获得它。

<?php
include('simple_html_dom.php');
$html = file_get_html('https://www.forexfactory.com/calendar.php?month=nov.2019');

$x = 1;
$LIMIT = 10;

foreach($html->find('.calendar_row') as $e){
    $x++;
    $EVENTID = $e->attr['data-eventid'];
    $EVENTNAME = $e->find('.event')[0]->find('div')[0]->innertext;
    echo "<h4>".$EVENTNAME."</h4><br>";

    $API_RESPONSE = file_get_html('https://www.forexfactory.com/flex.php?do=ajax&contentType=Content&flex=calendar_mainCal&details='.$EVENTID);

    $API_RESPONSE = str_replace("<![CDATA[","",$API_RESPONSE);
    $API_RESPONSE = str_replace("]]>","",$API_RESPONSE);
    $API_RESPONSE = str_get_html($API_RESPONSE);
    foreach($API_RESPONSE->find('.calendarspecs__spec') as $LEFT_TD){

        $LEFT_TD_INNER_TEXT = trim($LEFT_TD->innertext);
        if($LEFT_TD_INNER_TEXT == "Source" || $LEFT_TD_INNER_TEXT == "Usual Effect"){
            echo $LEFT_TD_INNER_TEXT.": ".$LEFT_TD->next_sibling()->innertext."<br>";
        }
    }

    if($x>$LIMIT)
        break;
    echo "<hr>";

}

截图(Goutte):

屏幕截图(简单的 HTML DOM):

【讨论】:

  • 谢谢!请使用 gotte,因为我的其他实现已经在这个库中。此外,我的问题也是关于痛风的。你能重新编码这个例子吗?
  • @Anna.Klee 完成!
  • @Anna.Klee 您可以通过在开发者控制台中监控网络选项卡来获取 URL。当您单击这些文件夹图标时,它会向该链接发出 GET 请求(动态事件 ID)。
  • @Anna.Klee 我更新了获取所有链接的代码。
  • 抱歉,不清楚,但我的意思是将$sourceURL$relatedURL$source(文本)和$related 添加到像array_push($resArray, [$sourceURL, $source, $relatedURL, $related]) 这样的数组中。
猜你喜欢
  • 1970-01-01
  • 2021-11-05
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 2012-03-18
  • 2018-09-30
相关资源
最近更新 更多