开发者

PHP/HTML - Multiple page screen scrape, export to .txt with commas between dates and values

I am attempting to scrape the web page (see code) - as well as those pages going back in time (you can see the date '20110509' in the page itself) - for simple numerical strings. I can't seem to figure out through much trial and error (I'm new to programming) how to parse the specific data in the table that I want. I have been trying to use simple PHP/HTML without curl or other such things. Is this possible? I think my main issue is using the delimiters that are necessary to get the data from the source code.

What I'd like is for the program to start at the very first page it can, say for example '20050101', and scan through each page till the current date, grabbing the specific data for example, the "latest close" (column), "closing arm" (row), and have that value for the corresponding date exported to a single .txt file, with the date being separated from the value with a comma. Each time the program is run, the date/value should be appended to the existing text file.

I am aware many lines of the code below are junk, it's part of my learning process.

<开发者_Python百科;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>


All right so let's do this. We're going to first load the data into an HTML parser, then create an XPath parser out of it. XPath will help us navigate around the HTML easily. So:

$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);

Now then we need to grab some data. First off let's get all the data tables. Looking at the source, these tables are indicated by a class of mdcTable:

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

So far:

$ php test.php
Tables found: 5

Okay so we have the tables. Now we need to get specific column. So let's use the latest close column you mentioned:

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

The result so far:

$ php test.php
Column contains: Latest close
Column contains: Latest close
Column contains: Latest close
... etc ...

Now we need the column index for getting the specific column for the specific row. We do this by counting all of the previous sibling elements, then adding one. This is because element index selectors are 1 indexed, not 0 indexed:

$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";

Result is:

$ php test.php
Position is: 2

Now we need to get our specific row:

$data_row = $xpath->query("//table[@class='mdcTable']/*/td[starts-with(.,'Closing Arms')]");
echo "Returned {$data_row->length} row(s)\n";

Here we use starts-with, since the row label has a utf-8 symbol in it. This makes it easier. Result so far:

$ php test.php
Returned 4 row(s)

Now we need to use the column index to get the data we want:

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

Result is:

$ php test.php
20110509,1.26
20110509,1.40
20110509,0.32
20110509,1.01

Which can now be written to a file. Now, we don't have the markets these apply to, so let's go ahead and grab those:

$headings = array();
$market_headings = $xpath->query("//table[@class='mdcTable']/*/td[@class='colhead'][1]");
foreach($market_headings as $market_heading) {
  $headings[] = $market_heading->nodeValue;
}

Now we can use a counter to reference which market we're on:

$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++;
}

The output being:

$ php test.php
20110509,NYSE,1.26
20110509,Nasdaq,1.40
20110509,NYSE Amex,0.32
20110509,NYSE Arca,1.01

Now for your part:

  • This can be made into a function that takes a date
  • You'll need code to write out the file. Check out the filesystem functions for hints
  • This can be made extendible to use different columns and different rows


I'd recommend using the HTML Agility Pack, its a HTML parser which is very handy for finding particular content within a HTML document.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜