0

I have this long list of courses on a html page done in tables i.e.

<tr class="navyLightgrey">
<td>3ADC7S1</td><td>SOFTWARE PROJECT MANAGEMENT (IIT Sri Lanka)</td><td align="Center">2009/0</td><td align="Center">Y</td><td align="Center">

<a id="dgModules__ctl2_lnkModule" href="http://example.com">View</a>

             </td>

            </tr>

<tr class="navyLightgrey">
<td>3ADC7S2</td><td>SOFTWARE ARCHITECTURE (IIT Sri Lanka)</td><td align="Center">2009/0</td><td align="Center">Y</td><td align="Center">

<a id="dgModules__ctl3_lnkModule" href="http://example.com" target="_self">View</a>

           </td>

             </tr>

It's done in this format. I want to get the value of the second td where it says the course name i.e. SOFTWARE ARCHITECTURE (IIT Sri Lanka) and SOFTWARE PROJECT MANAGEMENT (IIT Sri Lanka) for each tr item. I want to do a while loop through the html page and get each value and echo it. Thanks

2
  • 1
    Doesn't the site you're stealing data from have an RSS feed that you can interface with, rather than scraping HTML? Commented Mar 31, 2011 at 9:59
  • (related) Best Methods to parse HTML Commented Mar 31, 2011 at 10:14

3 Answers 3

2
$html = 'your html';
$dom = new DOMDocument();
$dom->loadHTML($html); // or loadHTMLFile

$xpath = new DOMXPath($dom);
$arrNodes = $xpath->query('//tr/td[2]/text()');

foreach($arrNodes as $node)
    echo $node->nodeValue . '<br />';
Sign up to request clarification or add additional context in comments.

Comments

1

Use DOMDocument::loadHTML, then search for the correct element within the DOMDocument.

Comments

1

That is done with "HTML DOM Parser". You can use, for example this one.

2 Comments

thanks for this, when i find the <tr> in the html page, how can i tell it to access the second <td> ? :)) +! from me
for that is "foreach" loop, it will loop through all matched elements. If you want to access "next TR after TR, where is some identifying stuff", you should add some "if matched, set variable found=1" and add in loop "if found = 1 then this is ne one TR I need".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.