Вот отличный пример как парсить урлы сайтов и доставать из них всё,что нам нужно в качестве примера выступает парсинг title and descriptions сайтов.Естественно по аналогии можно использовать и других случаях.
Code:
<?php
ob_implicit_flush();
$urls = file("file.txt");
$file_titles = fopen("titles.txt", "a");
$file_descs = fopen("descriptions.txt", "a");
$pattern1 = "/<title>(.*?)<\/title>/is";
$pattern2 = "/<meta name=(\"|')description(\"|') content=(\"|')(.*?)(\"|')(.*?)>/is";
foreach ($urls as $one_url)
{
$html = file_get_contents(trim($one_url));
preg_match($pattern1, $html, $matches);
fwrite($file_titles, $matches[1]."\n");
preg_match($pattern2, $html, $matches);
fwrite($file_descs, $matches[4]."\n");
echo $one_url . " done.<br />";
}
fclose($file_titles);
fclose($file_descs);
?>
Code:
<?php
ob_implicit_flush();
$urls = file("file.txt");
$file_titles = fopen("titles.txt", "a");
$file_descs = fopen("descriptions.txt", "a");
$pattern1 = "/<title>(.*?)<\/title>/is";
$pattern2 = "/<meta name=(\"|')description(\"|') content=(\"|')(.*?)(\"|')(.*?)>/is";
foreach ($urls as $one_url)
{
$html = file_get_contents(trim($one_url));
preg_match($pattern1, $html, $matches);
fwrite($file_titles, $matches[1]."\n");
preg_match($pattern2, $html, $matches);
fwrite($file_descs, $matches[4]."\n");
echo $one_url . " done.<br />";
}
fclose($file_titles);
fclose($file_descs);
?>
Комментариев нет:
Отправить комментарий