Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 脚本将部分内容写入csv文件_Php_Csv_Curl_Web Scraping_Simple Html Dom - Fatal编程技术网

Php 脚本将部分内容写入csv文件

Php 脚本将部分内容写入csv文件,php,csv,curl,web-scraping,simple-html-dom,Php,Csv,Curl,Web Scraping,Simple Html Dom,我已经用php编写了一个脚本,从网页中刮取标题及其链接,并将它们相应地写入csv文件。当我处理一个分页的站点时,只有最后一页的内容保留在csv文件中,其余的内容被覆盖。我试着用写作模式w。但是,当我使用append a执行相同操作时,我会在该csv文件中找到所有数据 由于添加和写入数据会导致csv文件多次打开和关闭,这可能是因为我的循环应用错误,因此脚本的效率和时间都会降低 我怎样才能高效地完成同样的工作,当然也要使用writingw模式 这是我迄今为止写的: <?php include

我已经用php编写了一个脚本,从网页中刮取标题及其链接,并将它们相应地写入csv文件。当我处理一个分页的站点时,只有最后一页的内容保留在csv文件中,其余的内容被覆盖。我试着用写作模式w。但是,当我使用append a执行相同操作时,我会在该csv文件中找到所有数据

由于添加和写入数据会导致csv文件多次打开和关闭,这可能是因为我的循环应用错误,因此脚本的效率和时间都会降低

我怎样才能高效地完成同样的工作,当然也要使用writingw模式

这是我迄今为止写的:

<?php
include "simple_html_dom.php";
$link = "https://stackoverflow.com/questions/tagged/web-scraping?page="; 

function get_content($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $htmlContent = curl_exec($ch);
        curl_close($ch);
        $dom = new simple_html_dom();
        $dom->load($htmlContent);
        $infile = fopen("itemfile.csv","a");
        foreach($dom->find('.question-summary') as $file){
            $itemTitle = $file->find('.question-hyperlink', 0)->innertext;
            $itemLink = $file->find('.question-hyperlink', 0)->href;
            echo "{$itemTitle},{$itemLink}<br>";
            fputcsv($infile,[$itemTitle,$itemLink]);
        }
        fclose($infile);
    }
for($i = 1; $i<10; $i++){
        get_content($link.$i);
    }
?>

如果不想多次打开和关闭文件,请在for循环之前移动打开脚本,然后在以下操作之后关闭:

function get_content($url, $inifile)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $htmlContent = curl_exec($ch);
    curl_close($ch);
    $dom = new simple_html_dom();
    $dom->load($htmlContent);
    foreach($dom->find('.question-summary') as $file){
        $itemTitle = $file->find('.question-hyperlink', 0)->innertext;
        $itemLink = $file->find('.question-hyperlink', 0)->href;
        echo "{$itemTitle},{$itemLink}<br>";
        fputcsv($infile,[$itemTitle,$itemLink]);
    }
}

$infile = fopen("itemfile.csv","w");

for($i = 1; $i<10; $i++) {
    get_content($link.$i, $inifile);
}

fclose($infile);
?>

我不考虑在GETX内容函数中对文件进行回响或写入结果。我会重写它,这样它只会得到内容,所以我可以用任何我喜欢的方式处理提取的数据。类似的内容请阅读代码注释:

<?php
include "simple_html_dom.php";
$link = "https://stackoverflow.com/questions/tagged/web-scraping?page="; 

// This function does not write data to a file or print it. It only extracts data
// and returns it as an array.
function get_content($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $htmlContent = curl_exec($ch);
        curl_close($ch);
        $dom = new simple_html_dom();
        $dom->load($htmlContent);
        // We don't need the following line anymore
        // $infile = fopen("itemfile.csv","a");
        // We will collect extracted data in an array
        $result = [];
        foreach($dom->find('.question-summary') as $file){
            $itemTitle = $file->find('.question-hyperlink', 0)->innertext;
            $itemLink = $file->find('.question-hyperlink', 0)->href;
            $result []= [$itemTitle, $itemLink];
            // echo "{$itemTitle},{$itemLink}<br>";
            // No need to write to file, so we don't need the following as well
            // fputcsv($infile,[$itemTitle,$itemLink]);
        }
        // No files opened, so the following line is no more required
        // fclose($infile);
        // Return extracted data from this specific URL
        return $result;
    }
// Merge all results (result for each url with different page parameter
// With a little refactoring, get_content() can handle this as well
$result = [];
for($page = 1; $page < 10; $page++){
    $result = array_merge($result, get_content($link.$page));
}
// Now do whatever you want with $result. Like writing its values to a file, or print it, etc.
// You might want to write a function for this
$outputFile = fopen("itemfile.csv","a");
foreach ($result as $row) {
    fputcsv($outputFile, $row);
}
fclose($outputFile);

?>

抱歉@Nima,我的反应太晚了。你的剧本很神奇。提供了一个加号。我需要研究您的代码,因为这种方法与我之前的方法略有不同。谢谢。