【问题标题】:Cant seem to scrape with website with PHP Simple HTML DOM Parser似乎无法使用 PHP Simple HTML DOM Parser 抓取网站
【发布时间】:2016-12-23 19:49:49
【问题描述】:

我是抓取网站的新手,我有兴趣从该网站获取票价。 https://www.cheaptickets.com/events/tickets/firefly-music-festival-4-day-pass-2867495 我在 p#price-selected-label.filters-selected-label 标签中看到了票价,但我似乎无法访问它。我尝试了一些东西并查看了一些教程,但要么返回空白,要么出现一些错误。代码基于http://blog.endpoint.com/2016/07/scrape-web-content-with-php-no-api-no.html

    <?php
 
require('simple_html_dom.php');
 
// Create DOM from URL or file
$html = file_get_html('https://www.cheaptickets.com/events/tickets/firefly-music-festival-4-day-pass-2867495');
 
// creating an array of elements
$videos = [];
 
// Find top ten videos
$i = 1;
        $videoDetails = $html->find('p#price-selected-label.filters-selected-label')-> innertext;

//        $videoDetails = $html->find('p#price-selected-label.filters-selected-label',0);



echo $videoDetails;
/*
foreach ($html->find('li.expanded-shelf-content-item-wrapper') as $video) {
        if ($i > 10) {
                break;
        }
 
        // Find item link element 
        $videoDetails = $video->find('a.yt-uix-tile-link', 0);
 
        // get title attribute
        $videoTitle = $videoDetails->title;
 
        // get href attribute
        $videoUrl = 'https://youtube.com' . $videoDetails->href;
 
        // push to a list of videos
        $videos[] = [
                'title' => $videoTitle,
                'url' => $videoUrl
        ];
 
        $i++;
}
 
var_dump($videos);
*/

【问题讨论】:

  • 网站有时会使用 javascript 向网站填充内容。
  • 你必须为此使用php吗?
  • 不,我不需要使用 php,我只是最喜欢它,但如果它可以更好地工作,我也可以使用其他东西。谢谢
  • 这种东西我一般用PhantomJs,它会抓取整个页面,你可以用jquery选择你想要的元素并返回给php,
  • 报告空白页或“一些错误”不是有用问题的一部分。在您的问题中提供实际的错误消息。

标签: php html web-scraping


【解决方案1】:

您无法获取它,因为 javascript 会呈现它,因此它在您的库获取的原始 html 中不可用。

使用phantomjs(会执行javascript);

  1. 下载 phantomjs 并将可执行文件放在 PHP 二进制文件可以访问的路径中。

  2. 将以下2个文件放在同一目录下:

get-website.php

<?php

    $phantom_script= dirname(__FILE__). '/get-website.js';     

    $response =  exec ('phantomjs ' . $phantom_script);

    echo  htmlspecialchars($response);
?>

get-website.js

    var webPage = require('webpage');
    var page = webPage.create();    

    page.open('https://www.cheaptickets.com/events/tickets/firefly-music-festival-4-day-pass-2867495', function(status) {
        if (status === "success") {
            page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', function() {          
                var myElem = $('p#price-selected-label.filters-selected-label');
                console.log(myElem);
            });

            phantom.exit();         
        }
    });
  1. 浏览到get-website.php 和目标站点,https://www.cheaptickets.com/events/tickets/firefly-music-festival-4-day-pass-2867495 内容将在执行内联javascript 后返回。您也可以使用 php /path/to/get-website.php 从命令行调用它。

【讨论】:

  • 谢谢你的信息,去试试看!
猜你喜欢
  • 2017-06-08
  • 2014-09-04
  • 1970-01-01
  • 2018-05-23
  • 2011-06-09
  • 1970-01-01
  • 1970-01-01
  • 2012-08-12
  • 2012-04-08
相关资源
最近更新 更多