Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5957819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:27:20+00:00 2026-05-22T18:27:20+00:00

I am attempting to scrape the web page (see code) – as well as

  • 0

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.

<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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:27:21+00:00Added an answer on May 22, 2026 at 6:27 pm

    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
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to scrape a web page that has the following structures within
Attempting to generics-ify some legacy code, I'm stuck. I have a ParentObject which wraps
Im attempting to Convert a Price (from an API (code below)). public class Price
Attempting to follow this Java tutorial . About 63 pages in, you are instructed
Attempting to learn CI and going through the docs to get a better understanding.
Attempting to follow the directions as specified on http://code.google.com/p/gears/wiki/BuildingGearsForWindows my attempts are failing at
Attempting to take the data in my gridview and convert it back into a
Attempting to get an answer to this question. How can I turn off the
Attempting the most basic example of Knockout.js possible on their documentation page: http://knockoutjs.com/documentation/observables.html Looks
Attempting regex on a HTML file with the following code: <body style=><p class=Normal style=direction:ltr;unicode-bidi:normal;><span

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.