Skip to content

Instantly share code, notes, and snippets.

@maskaravivek
Created March 28, 2014 15:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maskaravivek/9835359 to your computer and use it in GitHub Desktop.
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.
<?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("&rdquo;","",trim(substr($a,0,strpos($a,'<br>')),"&ldquo;"));
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