<?php
require_once __DIR__ . '/vendor/autoload.php';
use PHPHtmlParser\Dom;
use PHPHtmlParser\Options;
$options = new Options();
$options->setEnforceEncoding('utf8');
$db = new SQLite3('13dl.db');
$url1 = 'https://13dl.me/list/popular/';
function scrape($counter,$options,&$element_a,$db){
$urls = array();
$titles = array();
$next = "";
foreach($element_a as $line){
if(preg_match('/title/',"$line")) {
$div = $line->find('div');
if(preg_match('/div/',"$div")) {
$href = 'href';
$urls[] = $line->$href . "\n";
$titles[] = $div->text;
}else{
if(preg_match('/Next/',"$line")){
$attr = 'href';
$next = $line->$attr;
}
}
}else{
;
}
}
foreach($titles as $index=>$xxxx){
$counter ++;
$title = rtrim($xxxx,"\n");
$url = $urls[$index];
$u = rtrim($url,"\n");
$query = <<<eof
INSERT INTO data (id,title,addr) VALUES ('$counter','$title','$u')
eof;
$result = $db->exec($query);
echo "\n";
echo $counter . " " . $urls[$index] . " title :" . $xxxx . "\n";
}
unset($titles);
return array("$next",$counter);
}
$counter = 0;
while($url1 != ''){
if(preg_match('/http/',"$url1")) {
try {
$dom = new Dom();
$dom->loadFromUrl($url1, $options);
$element_a = $dom->find('a');
list($url_1,$counter) = scrape($counter,$options,$element_a,$db);
unset($element_a);
unset($dom);
} catch (Exception $e) {
$e->getMessage();
}
}else{
die($url1);
}
}
$db->close();
die('End');
?>