【问题标题】:How to crawl a site with server-generated content?如何使用服务器生成的内容抓取网站?
【发布时间】:2019-09-02 20:59:50
【问题描述】:

我正在编写一个简单的 php 爬虫,它从网站获取数据并将其插入到我的数据库中。我从一个预定义的 url 开始。然后我浏览页面的内容(来自 php 的 file_get_contents)并最终在该页面的链接上使用file_get_contents。当我回显它们然后从我的浏览器自己打开它们时,我从链接中获得的 url 很好。但是,当我使用file_get_contents 然后回显结果时,由于与从站点动态创建的服务器端数据相关的错误,该页面无法正确显示。回显的页面内容不包括我需要的服务器中列出的数据,因为它找不到该站点的必要资源。

似乎回显网页中的相对路径不允许生成所需的内容。

任何人都可以在这里指出正确的方向吗?

感谢任何帮助!

到目前为止,这是我的一些代码:

function crawl_all($url)
{
    $main_page = file_get_contents($url);

    while(strpos($main_page, '"fl"') > 0)
    {   
        $subj_start  = strpos($main_page, '"fl"');      // get start of subject row
        $main_page   = substr($main_page, $subj_start); // cut off everything before subject row
        $link_start  = strpos($main_page, 'href') + 6;  // get the start of the subject link
        $main_page   = substr($main_page, $link_start); // cut off everything before subject link
        $link_end    = strpos($main_page, '">') - 1;    // get the end of the subject link
        $link_length = $link_end + 1;             
        $link = substr($main_page, 0, $link_length);    // get the subject link

        crawl_courses('https://whatever.com' . $link);      
    }
}

/* Crawls all the courses for a subject. */
function crawl_courses($url)
{
    $subj_page = file_get_contents($url);
    echo $url;           // website looks fine when in opened in browser
    echo $subj_page;     // when echo'd, the page does not contain most of the server-side generated data i need

    while(strpos($subj_page, '<td><a href') > 0)
    {
        $course_start = strpos($subj_page, '<td><a href');
        $subj_page    = substr($subj_page, $course_start);
        $link_start   = strpos($subj_page, 'href') + 6;
        $subj_page    = substr($subj_page, $link_start);
        $link_end     = strpos($subj_page, '">') - 1;
        $link_length  = $link_end + 1;
        $link = substr($subj_page, 0, $link_length);

        //crawl_professors('https://whatever.com' . $link);
    }
}

【问题讨论】:

标签: php web-crawler


【解决方案1】:

试试高级 html dom 解析器。是这里.... http://sourceforge.net/projects/advancedhtmldom/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-14
    • 1970-01-01
    • 1970-01-01
    • 2015-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-27
    相关资源
    最近更新 更多