Created
March 28, 2014 15:24
-
-
Save maskaravivek/9835359 to your computer and use it in GitHub Desktop.
This php code uses simple_html_dom to scrap quotes along with their authors from Goodreads to a MYSQL database.
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 | |
include('config.php'); | |
include('simple_html_dom.php'); | |
if(!empty($_POST['name']) && !empty($_POST['number'])) | |
{ | |
$cat=$_POST['name']; | |
$no=$_POST['number']; | |
for($i=1;$i<$no;$i++) | |
{ | |
$url="http://www.goodreads.com/quotes/tag/".$cat."?page=".$i; | |
$html= file_get_html($url); | |
foreach($html->find('.quote') as $quote) { | |
foreach($quote->find('.quoteText') as $text) | |
{ | |
$a=trim($text->innertext); | |
$quotetext= str_replace("”","",trim(substr($a,0,strpos($a,'<br>')),"“")); | |
foreach($text->find('a') as $authors) | |
{ | |
$author= $authors->plaintext; | |
} | |
} | |
foreach($quote->find('.greyText') as $tags) | |
{ | |
$tag= substr(trim($tags->plaintext),6); | |
$tag=preg_replace('/\s+/', '', $tag); | |
} | |
foreach($quote->find('.right') as $likes) | |
{ | |
$like=substr(trim($likes->plaintext),0,strlen(trim($likes->plaintext))-6); | |
} | |
mysql_query("INSERT INTO quotes(quote,author,tags,category,likes) VALUES('$quotetext','$author','$tag','$cat','$like')"); | |
} | |
} | |
} | |
else | |
{ | |
echo "Invalid command"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment