首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在本地工作的PHP脚本,但当放置在when服务器上时不起作用

在本地工作的PHP脚本,但当放置在when服务器上时不起作用
EN

Stack Overflow用户
提问于 2015-07-08 23:07:53
回答 1查看 138关注 0票数 0

下面的代码从给定的网页中抓取链接列表,然后将它们放到另一个脚本中,从给定的链接中抓取文本并将数据放入csv文档中。代码在localhost (wampserver5.5php)上运行得很好,但如果放置在域上,则会严重失败。

您可以在http://miskai.tk/ANOFM/csv.php上查看脚本的功能。另外,文件get html和curl都在服务器上启用。

代码语言:javascript
复制
<?php
header('Content-Type: application/excel');
header('Content-Disposition: attachment; filename="Mehedinti.csv"');
include_once 'simple_html_dom.php';

include_once 'csv.php';
$urls = scrape_main_page();

function scraping($url) {
    // create HTML DOM

    $html = file_get_html($url);

    // get article block
    if ($html && is_object($html) && isset($html->nodes)) {

        foreach ($html->find('/html/body/table') as $article) {
            // get title

            $item['titlu'] = trim($article->find('/tbody/tr[1]/td/div', 0)->plaintext);

            // get body
            $item['tr2'] = trim($article->find('/tbody/tr[2]/td[2]', 0)->plaintext);
            $item['tr3'] = trim($article->find('/tbody/tr[3]/td[2]', 0)->plaintext);
            $item['tr4'] = trim($article->find('/tbody/tr[4]/td[2]', 0)->plaintext);
            $item['tr5'] = trim($article->find('/tbody/tr[5]/td[2]', 0)->plaintext);
            $item['tr6'] = trim($article->find('/tbody/tr[6]/td[2]', 0)->plaintext);
            $item['tr7'] = trim($article->find('/tbody/tr[7]/td[2]', 0)->plaintext);
            $item['tr8'] = trim($article->find('/tbody/tr[8]/td[2]', 0)->plaintext);
            $item['tr9'] = trim($article->find('/tbody/tr[9]/td[2]', 0)->plaintext);
            $item['tr10'] = trim($article->find('/tbody/tr[10]/td[2]', 0)->plaintext);
            $item['tr11'] = trim($article->find('/tbody/tr[11]/td[2]', 0)->plaintext);
            $item['tr12'] = trim($article->find('/tbody/tr[12]/td/div/]', 0)->plaintext);
            $ret[] = $item;
        }

        // clean up memory
        $html->clear();
        unset($html);

        return $ret;}
}



$output = fopen("php://output", "w");

foreach ($urls as $url) {

    $ret = scraping($url);

    foreach($ret as $v){
        fputcsv($output, $v);}

}

fclose($output);
exit();

第二档案

代码语言:javascript
复制
<?php

function get_contents($url) {
    // We could just use file_get_contents but using curl makes it more future-proof (setting a timeout for example)
    $ch = curl_init($url);

    curl_setopt_array($ch, array(CURLOPT_RETURNTRANSFER => true,));

    $content = curl_exec($ch);
    curl_close($ch);

    return $content;
}

function scrape_main_page() {
    set_time_limit(300);
    libxml_use_internal_errors(true); // Prevent DOMDocument from spraying errors onto the page and hide those errors internally ;)
    $html = get_contents("http://lmvz.anofm.ro:8080/lmv/index2.jsp?judet=26");

    $dom = new DOMDocument();
    $dom->loadHTML($html);
    die(var_dump($html));
    $xpath = new DOMXPath($dom);
    $results = $xpath->query("//table[@width=\"645\"]/tr");

    $all = array();
    //var_dump($results);
    for($i = 1; $i < $results->length; $i++) {
        $tr = $results->item($i);

        $id = $tr->childNodes->item(0)->textContent;
        $requesturl = "http://lmvz.anofm.ro:8080/lmv/detalii.jsp?UNIQUEJVID=" . urlencode($id) .
            "&judet=26";
        $details = scrape_detail_page($requesturl);

        $newObj = new stdClass();
        $newObj = $id;
        $all[] = $newObj;
    }

    foreach($all as $xtr) {
        $urls[] = "http://lmvz.anofm.ro:8080/lmv/detalii.jsp?UNIQUEJVID=" . $xtr .
            "&judet=26";
    }
    return $urls;
}


scrape_main_page();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-09 00:29:57

是的,这里的问题是您的php.ini配置。确保服务器支持curl和fopen。如果没有启动您自己的linux服务器。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31305383

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档