Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.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/HTML-多页屏幕抓取,导出为.txt,日期和值之间带有逗号_Php_Html_Parsing_Scrape - Fatal编程技术网

PHP/HTML-多页屏幕抓取,导出为.txt,日期和值之间带有逗号

PHP/HTML-多页屏幕抓取,导出为.txt,日期和值之间带有逗号,php,html,parsing,scrape,Php,Html,Parsing,Scrape,我正试图从网页(见代码)以及那些追溯到时间的网页(你可以在网页上看到日期“20110509”)中找到简单的数字字符串。我似乎无法通过多次尝试和错误(我是编程新手)了解如何解析表中所需的特定数据。我一直在尝试使用简单的PHP/HTML而不使用curl或其他类似的东西。这可能吗?我想我的主要问题是 使用从源代码获取数据所需的分隔符 我想让程序从第一页开始,比如说“20050101”,然后扫描每一页直到当前日期,获取特定数据,例如“最新关闭”(列)、“关闭臂”(行),并将相应日期的值导出到单个.txt

我正试图从网页(见代码)以及那些追溯到时间的网页(你可以在网页上看到日期“20110509”)中找到简单的数字字符串。我似乎无法通过多次尝试和错误(我是编程新手)了解如何解析表中所需的特定数据。我一直在尝试使用简单的PHP/HTML而不使用curl或其他类似的东西。这可能吗?我想我的主要问题是 使用从源代码获取数据所需的分隔符

我想让程序从第一页开始,比如说“20050101”,然后扫描每一页直到当前日期,获取特定数据,例如“最新关闭”(列)、“关闭臂”(行),并将相应日期的值导出到单个.txt文件,日期与值之间用逗号分隔。每次运行程序时,都应将日期/值附加到现有文本文件中

我知道下面的许多代码都是垃圾代码,这是我学习过程的一部分

<html>
<title>HTML with PHP</title>
<body>

<?php

$rawdata = file_get_contents('http://online.wsj.com/mdc/public/page/2_3021-tradingdiary2-20110509.html?mod=mdc_pastcalendar');
//$data = substr(' ', $data);
//$begindate = '20050101';
//$newlines = array("\t","\n","\r","\x20\x20","\0","\x0B"); 
//if (preg_match(' <td class="text"> ' , $data , $content)) {
//$content = str_replace($newlines

echo $rawdata;
///file_put_contents( 'NYSETRIN.html' , $content , FILE_APPEND);

?>

<b>some more html</b>

<?php
?>

</body>
</html>

HTML与PHP

我建议使用,它是一个HTML解析器,非常方便在HTML文档中查找特定内容。

我建议使用,它是一个HTML解析器,非常方便在HTML文档中查找特定内容。

好的,让我们这样做吧。我们将首先将数据加载到HTML解析器中,然后从中创建XPath解析器。XPath将帮助我们轻松浏览HTML。因此:

$date = "20110509";
$data = file_get_contents("http://online.wsj.com/mdc/public/page/2_3021-tradingdiary2-{$date}.html?mod=mdc_pastcalendar");

$doc = new DOMDocument();
@$doc->loadHTML($data);

$xpath = new DOMXpath($doc);
现在我们需要获取一些数据。首先,让我们获取所有的数据表。查看源代码,这些表由一类
mdcTable
表示:

$result = $xpath->query("//table[@class='mdcTable']");
echo "Tables found: {$result->length}\n";
到目前为止:

$ php test.php
Tables found: 5
好了,我们有桌子了。现在我们需要得到特定的列。让我们使用你提到的最新专栏:

$result = $xpath->query("//table[@class='mdcTable']/*/td[contains(.,'Latest close')]");
foreach($result as $td) {
  echo "Column contains: {$td->nodeValue}\n";
}
到目前为止的结果是:

$ php test.php
Column contains: Latest close
Column contains: Latest close
Column contains: Latest close
... etc ...
$ php test.php
Returned 4 row(s)
现在我们需要列索引来获取特定行的特定列。我们通过计算所有以前的同级元素,然后添加一个来实现这一点。这是因为元素索引选择器是1索引的,而不是0索引的:

$result = $xpath->query("//table[@class='mdcTable']/*/td[contains(.,'Latest close')]");
$column_position = count($xpath->query('preceding::*', $result->item(0))) + 1;
echo "Position is: $column_position\n";
结果是:

$ php test.php
Position is: 2
$ php test.php
20110509,1.26
20110509,1.40
20110509,0.32
20110509,1.01
现在我们需要得到具体的行:

$data_row = $xpath->query("//table[@class='mdcTable']/*/td[starts-with(.,'Closing Arms')]");
echo "Returned {$data_row->length} row(s)\n";
这里我们使用以
开头的
,因为行标签中有一个utf-8符号。这使它更容易。迄今为止的结果:

$ php test.php
Column contains: Latest close
Column contains: Latest close
Column contains: Latest close
... etc ...
$ php test.php
Returned 4 row(s)
现在我们需要使用列索引来获取所需的数据:

$data_row = $xpath->query("//table[@class='mdcTable']/*/td[starts-with(.,'Closing Arms')]/../*[$column_position]");
foreach($data_row as $row) {
  echo "{$date},{$row->nodeValue}\n";
}
结果是:

$ php test.php
Position is: 2
$ php test.php
20110509,1.26
20110509,1.40
20110509,0.32
20110509,1.01
现在可以写入文件。现在,我们没有这些适用的市场,所以让我们继续抓住这些:

$headings = array();
$market_headings = $xpath->query("//table[@class='mdcTable']/*/td[@class='colhead'][1]");
foreach($market_headings as $market_heading) {
  $headings[] = $market_heading->nodeValue;
}
现在,我们可以使用计数器来参考我们所在的市场:

$data_row = $xpath->query("//table[@class='mdcTable']/*/td[starts-with(.,'Closing Arms')]/../*[$column_position]");
$i = 0;
foreach($data_row as $row) {
  echo "{$date},{$headings[$i]},{$row->nodeValue}\n";
  $i++;
}
输出为:

$ php test.php
20110509,NYSE,1.26
20110509,Nasdaq,1.40
20110509,NYSE Amex,0.32
20110509,NYSE Arca,1.01
现在请注意:

  • 这可以做成一个带日期的函数
  • 您需要编写代码来写出该文件。请查看下面的提示
  • 这可以扩展为使用不同的列和行

    • 好的,让我们开始吧。我们将首先将数据加载到HTML解析器中,然后从中创建XPath解析器。XPath将帮助我们轻松浏览HTML。因此:

      $date = "20110509";
      $data = file_get_contents("http://online.wsj.com/mdc/public/page/2_3021-tradingdiary2-{$date}.html?mod=mdc_pastcalendar");
      
      $doc = new DOMDocument();
      @$doc->loadHTML($data);
      
      $xpath = new DOMXpath($doc);
      
      现在我们需要获取一些数据。首先,让我们获取所有的数据表。查看源代码,这些表由一类
      mdcTable
      表示:

      $result = $xpath->query("//table[@class='mdcTable']");
      echo "Tables found: {$result->length}\n";
      
      到目前为止:

      $ php test.php
      Tables found: 5
      
      好了,我们有桌子了。现在我们需要得到特定的列。让我们使用你提到的最新专栏:

      $result = $xpath->query("//table[@class='mdcTable']/*/td[contains(.,'Latest close')]");
      foreach($result as $td) {
        echo "Column contains: {$td->nodeValue}\n";
      }
      
      到目前为止的结果是:

      $ php test.php
      Column contains: Latest close
      Column contains: Latest close
      Column contains: Latest close
      ... etc ...
      
      $ php test.php
      Returned 4 row(s)
      
      现在我们需要列索引来获取特定行的特定列。我们通过计算所有以前的同级元素,然后添加一个来实现这一点。这是因为元素索引选择器是1索引的,而不是0索引的:

      $result = $xpath->query("//table[@class='mdcTable']/*/td[contains(.,'Latest close')]");
      $column_position = count($xpath->query('preceding::*', $result->item(0))) + 1;
      echo "Position is: $column_position\n";
      
      结果是:

      $ php test.php
      Position is: 2
      
      $ php test.php
      20110509,1.26
      20110509,1.40
      20110509,0.32
      20110509,1.01
      
      现在我们需要得到具体的行:

      $data_row = $xpath->query("//table[@class='mdcTable']/*/td[starts-with(.,'Closing Arms')]");
      echo "Returned {$data_row->length} row(s)\n";
      
      这里我们使用以
开头的
,因为行标签中有一个utf-8符号。这使它更容易。迄今为止的结果:

$ php test.php
Column contains: Latest close
Column contains: Latest close
Column contains: Latest close
... etc ...
$ php test.php
Returned 4 row(s)
现在我们需要使用列索引来获取所需的数据:

$data_row = $xpath->query("//table[@class='mdcTable']/*/td[starts-with(.,'Closing Arms')]/../*[$column_position]");
foreach($data_row as $row) {
  echo "{$date},{$row->nodeValue}\n";
}
结果是:

$ php test.php
Position is: 2
$ php test.php
20110509,1.26
20110509,1.40
20110509,0.32
20110509,1.01
现在可以写入文件。现在,我们没有这些适用的市场,所以让我们继续抓住这些:

$headings = array();
$market_headings = $xpath->query("//table[@class='mdcTable']/*/td[@class='colhead'][1]");
foreach($market_headings as $market_heading) {
  $headings[] = $market_heading->nodeValue;
}
现在,我们可以使用计数器来参考我们所在的市场:

$data_row = $xpath->query("//table[@class='mdcTable']/*/td[starts-with(.,'Closing Arms')]/../*[$column_position]");
$i = 0;
foreach($data_row as $row) {
  echo "{$date},{$headings[$i]},{$row->nodeValue}\n";
  $i++;
}
输出为:

$ php test.php
20110509,NYSE,1.26
20110509,Nasdaq,1.40
20110509,NYSE Amex,0.32
20110509,NYSE Arca,1.01
现在请注意:

  • 这可以做成一个带日期的函数
  • 您需要编写代码来写出该文件。请查看下面的提示
  • 这可以扩展为使用不同的列和行

您的意思是说您正在收集网页中的所有纯文本?不,不是所有纯文本,只是表格中显示的某些数据。我想我很清楚这一点,我在上面包括了一个我想解析的值的例子…我看到了你想解析的URL。我猜你想用股市数据。是吗?是的,我认为这很明显。你可以用其他的方法。我想我对处理股市数据有一些想法。在sujitagarwal上给我打电话。com@gmail.comYou意思是说你正在收集网页上的所有纯文本?不,不是所有的纯文本,只是表格中出现的某些数据。我想我很清楚这一点,我在上面包括了一个我想解析的值的例子…我看到了你想解析的URL。我猜你想用股市数据。是吗?是的,我认为这很明显。你可以用其他的方法。我想我对处理股市数据有一些想法。在sujitagarwal上给我打电话。com@gmail.comThank你举个例子,真的helpful@Adam我真的建议大家阅读XPath,它是解析HTML()的强大工具。然后,我建议查看PHP中所有可用的DOM函数:()是的,我已经计划好了,谢谢链接和帮助。我仍在努力理解基本php代码,并处理您编写的内容,以获得我想要的输出。我唯一想做的事情就是沿着类似的路线。我已经在DOM函数上打开了第二个链接,因为我一直在使用它