Created
July 11, 2011 12:48
-
-
Save aarvay/1075768 to your computer and use it in GitHub Desktop.
A Naive HN Crawler to get news about Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* A Naive HN Crawler to get news about Javascript | |
* @author Vignesh Rajagopalan <vignesh@campuspry.com> | |
* | |
* Motivated by Vijay Anand (@vijayanands) | |
*/ | |
require('phpQuery.php'); | |
//$fp = fopen("../jsdata.txt", 'w'); | |
$next = 'http://news.ycombinator.com/'; | |
$key=0; | |
$key1 = 'javascript'; | |
$key2 = 'js'; | |
for($i=0;$i<5;$i++) { | |
$html = file_get_contents($next); | |
$doc = phpQuery::newDocument($html); | |
pq('tr:first')->remove(); | |
pq('tr:empty')->remove(); | |
pq('td:empty')->remove(); | |
$rows = pq('table tr'); | |
$details = array(); | |
foreach($rows as $row) { | |
$title = strtolower(pq($row)->find('td.title')->text()); | |
//echo $title; | |
if (strpos($title, 'javascript') !== false) { | |
$details[$key]["title"] = $title; | |
$details[$key]["url"] = pq($row)->find('td.title')->find('a')->attr('href'); | |
$key++; | |
continue; | |
} | |
if($title == "more") { | |
$next = "http://news.ycombinator.com".pq($row)->find('a')->attr('href'); | |
break; | |
} | |
} | |
print_r($details).'<br /><br />'; | |
//fwrite($fp, json_encode($details)."<br /><br />"); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Crawls only the top 5 pages. Of course can be changed :-)