【问题标题】:file_get_contents from html explode, write to cell of spreadsheethtml中的file_get_contents爆炸,写入电子表格的单元格
【发布时间】:2016-08-28 14:44:58
【问题描述】:

我试图实现的是通过 file_get_contents() 对来自 URL 源的特定内容进行微调,然后在该内容所在的位置爆炸() 标记,仅返回 HTML 格式的内容,然后编写它到电子表格或 CSV 的单个单元格。很简单,我想。

这就是我所拥有的:

<?php

//My .html

$url = 'http://spiderlearning.com/demo/ALG_SA_U1_L1.html';

//Get content

$content = file_get_contents($url);

//Get content sections

$lesson_name = explode( '<section id="nameField" class="editable" contenteditable="false">' , $content);

$section_title1 = explode( '<a onclick="goToByScroll(\'obj0\')" href="#">' , $content);

$challenge_q = explode( '<section id="redactor_content" class="editable" contenteditable="false">' , $content);

//Write content

$write1 = explode("</section>" , $lesson_name[1]);
$write2 = explode("</a>" , $section_title1[1]);
$write3 = explode("</section>" , $challenge_q[1]);

//Into arrays

$line1 = array($write1[0],$write2[0],$write3[0]);

$list = array($line1);

//Open .csv

$file = fopen("data/data.csv", "w");

//Write as line, delimitate with ";"

foreach ($list as $line) fputcsv($file, $line, ';');

//Close

fclose($file);

?>

返回:

CSV

Excel

我正在寻找的是:

CSV:

Unit 1 Lesson 1; 1. Challenge Questions; <p><img src="https://s3-eu-west-1.amazonaws.com/teacher-uploads.fishtree.com/SpiderLearning/1428953716a42b06b9-1ce1-4594-badd-4ab8c9b65ac0.jpeg" alt="" rel="float: left; width: 171px; height: 113.697826086957px; margin: 0px 10px 10px 0px;" style="float: left; width: 171px; height: 113.697826086957px; margin: 0px 10px 10px 0px;"></p><p>Before you begin this lesson, let's see what you already know about the topic. Take a moment to complete the three Challenge Questions that follow.</p>

在我看来,问题在于格式化内容中的回车。它也在返回的内容周围加上括号,但我不确定从哪里开始。有什么办法可以逃避这些吗?我过去将类似的函数放在一起没有任何问题,但这是我第一次将 file_get_contents() 转换为 CSV,几周后我终于碰壁了。

【问题讨论】:

  • 您的做法是可以理解的。如果html网页发生变化怎么办?那么你的“爆炸”方法将不再有效。您应该使用一些库通过类名、html 元素类型等从网页中提取数据,例如 github.com/paquettg/php-html-parser
  • 抱歉,对于上下文,我将针对存储在我机器上的本地文件(大约 4,000 个)运行此程序。我只主持了这个,所以你可以看到我正在使用什么。每个文件都以完全相同的方式设置,并且不会对其进行更改或更新。我只需要以一种干净的方式将这些特定信息电子表格,以便我可以通过查找/替换、复制编辑快速更新媒体链接等,然后通过我创建的 CSV 中的 API 将内容发送到 CMS,如果这使得感觉。

标签: php csv spreadsheet file-get-contents fputcsv


【解决方案1】:

首先要摆脱换行符,请执行以下操作: foreach ($list as $line) fputcsv($file, preg_replace( "/\r|\n/", "", $line), ';');

最好保留 fputcsv 引入的那些字段分隔符。原因是其中一个字段内的任何分号都会破坏上面的 CSV 你想要的 CSV 然后看起来像:

"Unit 1 Lesson 1";"1. Challenge Questions";"<p><img src=""https://s3-eu-west-1.amazonaws.com/teacher-uploads.fishtree.com/SpiderLearning/1428953716a42b06b9-1ce1-4594-badd-4ab8c9b65ac0.jpeg"" alt="""" rel=""float: left; width: 171px; height: 113.697826086957px; margin: 0px 10px 10px 0px;"" style=""float: left; width: 171px; height: 113.697826086957px; margin: 0px 10px 10px 0px;""></p><p>Before you begin this lesson, let's see what you already know about the topic. Take a moment to complete the three Challenge Questions that follow.</p>"

但在大多数情况下,您不能直接在 excel 中打开它(某处有全局设置)。您需要导入此数据,然后设置以下内容:

【讨论】:

  • 诺曼,这很好用,谢谢!一个简单问题的简单解决方案。
【解决方案2】:

这是一个基于 PHP 的 DOMDocument 类的替代解决方案:

$url = 'http://spiderlearning.com/demo/ALG_SA_U1_L1.html';
// Load HTML via DOMDocument class
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTMLFile($url);
// Extract the elements of interest
$xpath = new DOMXPath($doc);
$list = [
    [
        "lesson" => $doc->getElementById('nameField')->textContent,
        "section" => $xpath->query("//div[@class='activitySelect']//a")[0]->textContent,
        "challenge" => innerHTML($doc->getElementById('redactor_content'))
    ]
];
// Write CSV (unchanged code)
$file = fopen("php://output", "w");
foreach ($list as $line) fputcsv($file, $line, ';');
fclose($file);

// Utility function
function innerHTML($node) {
    return implode(array_map([$node->ownerDocument,"saveHTML"], 
                             iterator_to_array($node->childNodes)));
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    • 2018-08-24
    • 2021-04-28
    • 1970-01-01
    相关资源
    最近更新 更多