【问题标题】:Scaping IFrame inside a HTML page with values loaded using Ajax request在 HTML 页面中使用 Ajax 请求加载值的 Scaping IFrame
【发布时间】:2017-08-12 12:47:24
【问题描述】:

我需要使用 PHP 抓取这个 HTML 页面...

http://www.cittadellasalute.to.it/index.php?option=com_content&view=article&id=6786:situazione-pazienti-in-pronto-soccorso&catid=165:pronto-soccorso&Itemid=372

...我需要提取“Rosso”、“Giallo”、“Verde”和“Bianco”行的数字(请注意,这些数字是动态的,因此它们可以在您刷新页面时更改,但它不会问题....)。

我已经看到这些行在一些 IFrame 中(例如 ...http://listeps.cittadellasalute.to.it/?id=01090201),并且这些值是使用 ajax 请求加载的(例如 http://listeps.cittadellasalute.to.it/gtotal.php?id=01090101)。

是否有一些解决方案可以直接抓取(我想避免解析单数 jsons ....),这些值来自使用 PHP 和 $xpath->query 的原始 HTML 页面?

建议/例子?

【问题讨论】:

    标签: php iframe xpath web-scraping


    【解决方案1】:

    我认为问题在于这些值不在原始页面中,它们是在页面加载后构建的。因此,您需要使用能够尊重所有 Javascript 功能(即 Selinium webdriver)的东西,这对于您想要做的事情来说有点矫枉过正(我假设)。直接处理 IFrame 要容易得多。

    您可以从原始页面中提取 IFrame 的 URL ...

    $url = "http://www.cittadellasalute.to.it/index.php?option=com_content&view=article&id=6786:situazione-pazienti-in-pronto-soccorso&catid=165:pronto-soccorso&Itemid=372";
    $pageContents = file_get_contents($url);
    $page = simplexml_load_string($pageContents, "SimpleXMLElement", LIBXML_NOERROR |  LIBXML_ERR_NONE);
    
    $ns = $page->getDocNamespaces();
    $page->registerXPathNamespace('def', array_values($ns)[0]);
    $iframes = $page->xpath("//def:iframe");
    foreach ( $iframes as $frame )    {
        echo "iframe:".$frame['src'].PHP_EOL;
    }
    

    这给了(刚刚)

    iframe:http://listeps.cittadellasalute.to.it/?id=01090101
    iframe:http://listeps.cittadellasalute.to.it/?id=01090201
    iframe:http://listeps.cittadellasalute.to.it/?id=01090301
    iframe:http://listeps.cittadellasalute.to.it/?id=01090302
    

    然后您可以处理这些页面。

    【讨论】:

    • 是的,我看过 IFrame 代码,并且他们使用 php(例如 listeps.cittadellasalute.to.it/gtotal.php?id=01090101)来加载值......我想避免解析 4 个单独的 json由php调用返回.....可能无法直接执行...
    • 正如我所提到的,唯一可能的直接方法是解释 Javascript。您可能会找到替代方案,但同时您可以解决问题并继续前进。
    猜你喜欢
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 2012-05-02
    • 1970-01-01
    • 1970-01-01
    • 2016-12-20
    • 2016-04-14
    • 1970-01-01
    相关资源
    最近更新 更多