Ben bağlantıları bir grup ile bir web sayfası var. Ben yerel bir dosyada bu bağlantıları bulunan tüm veri dökümü olacak bir senaryo yazmak istiyorum.
Herkes PHP ile bu yapılır mı? Genel kurallar ve gotchas bir cevap olarak yeterli olacaktır.
Meh. Yapmak değil parse HTML with regexes.
İşte Tatu esinlenerek bir DOM versiyonu:
<?php
function crawl_page($url, $depth = 5)
{
static $seen = array();
if (isset($seen[$url]) || $depth === 0) {
return;
}
$seen[$url] = true;
$dom = new DOMDocument('1.0');
@$dom->loadHTMLFile($url);
$anchors = $dom->getElementsByTagName('a');
foreach ($anchors as $element) {
$href = $element->getAttribute('href');
if (0 !== strpos($href, 'http')) {
$path = '/' . ltrim($href, '/');
if (extension_loaded('http')) {
$href = http_build_url($url, array('path' => $path));
} else {
$parts = parse_url($url);
$href = $parts['scheme'] . '://';
if (isset($parts['user']) && isset($parts['pass'])) {
$href .= $parts['user'] . ':' . $parts['pass'] . '@';
}
$href .= $parts['host'];
if (isset($parts['port'])) {
$href .= ':' . $parts['port'];
}
$href .= $path;
}
}
crawl_page($href, $depth - 1);
}
echo "URL:",$url,PHP_EOL,"CONTENT:",PHP_EOL,$dom->saveHTML(),PHP_EOL,PHP_EOL;
}
crawl_page("http://hobodave.com", 2);
Edit: I (şimdi göreli URL'ler ile çalışır) Tatunun sürümünden bazı hatalar düzeltildi.
Edit: Ben iki kez aynı URL aşağıdaki engelleyen yeni bir işlevsellik biraz ekledi.
Edit: böylece istediğiniz her dosya için yönlendirebilirsiniz şimdi STDOUT çıktı yankılanan
Edit: Sabit bir hata onun cevabını George tarafından işaret. Bağıl URL'ler artık url yolun sonuna eklemek, ama üzerine yazılır. Bunun için George sayesinde. Https, kullanıcı geçmek veya bağlantı noktası: George cevabı herhangi bir hesap olmadığını unutmayın. Eğer http PECL uzantısı bu yüklü varsa oldukça basit http_build_url kullanılarak yapılır. Aksi takdirde, ben elle parse_url kullanılarak birbirine tutkal var. Tekrar teşekkürler George.
Eğer wget, örneğin kullanabilirsiniz zaman neden, bunun için PHP kullanmak
wget -r -l 1 http://www.example.com
, Içeriğini ayrıştırmak Best Methods to parse HTML görmek ve için arama işlevini kullanmak için nasıl examples. Nasıl HTML ayrıştırmak için önce birden çok kez yanıtlandı.
Belirtildiği gibi, paletli çerçeveler tüm orada özelleştirme için hazır vardır, ama ne yaptığınızı size belirtildiği gibi basit ise, bunu oldukça kolay sıfırdan yapabilir.
Bağlantıları Kazıma: http://www.phpro.org/examples/Get-Links-With-DOM.html
Sonuçları bir dosyaya damping: http://www.tizag.com/phpT/filewrite.php
hobodave's kodunuzda bazı küçük değişiklikler ile, burada sayfaları taramasını için kullanabileceğiniz bir codesnippet olduğunu. Bu sunucu etkin olması kıvırmak uzantısı gerekiyor.
<?php
//set_time_limit (0);
function crawl_page($url, $depth = 5){
$seen = array();
if(($depth == 0) or (in_array($url, $seen))){
return;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec ($ch);
curl_close ($ch);
if( $result ){
$stripped_file = strip_tags($result, "<a>");
preg_match_all("/<a[\s]+[^>]*?href[\s]?=[\s\"\']+"."(.*?)[\"\']+.*?>"."([^<]+|.*?)?<\/a>/", $stripped_file, $matches, PREG_SET_ORDER );
foreach($matches as $match){
$href = $match[1];
if (0 !== strpos($href, 'http')) {
$path = '/' . ltrim($href, '/');
if (extension_loaded('http')) {
$href = http_build_url($href , array('path' => $path));
} else {
$parts = parse_url($href);
$href = $parts['scheme'] . '://';
if (isset($parts['user']) && isset($parts['pass'])) {
$href .= $parts['user'] . ':' . $parts['pass'] . '@';
}
$href .= $parts['host'];
if (isset($parts['port'])) {
$href .= ':' . $parts['port'];
}
$href .= $path;
}
}
crawl_page($href, $depth - 1);
}
}
echo "Crawled {$href}";
}
crawl_page("http://www.sitename.com/",3);
?>
Ben bu crawler script tutorial Bu öğretici açıkladım
Bu basit formu bulunuyor:
function crawl_page($url, $depth = 5) {
if($depth > 0) {
$html = file_get_contents($url);
preg_match_all('~<a.*?href="(.*?)".*?>~', $html, $matches);
foreach($matches[1] as $newurl) {
crawl_page($newurl, $depth - 1);
}
file_put_contents('results.txt', $newurl."\n\n".$html."\n\n", FILE_APPEND);
}
}
crawl_page('http://www.domain.com/index.php', 5);
Bu işlev daha sonra bulunan tüm bağlantıları tarama ve 'Results.txt' olarak içeriğini kaydetmek, bir sayfa içeriğini alacak. Fonksiyonlar bağlantıları takip edilmelidir ne kadar tanımlayan bir ikinci parametre, derinlik, kabul eder. Eğer verilen sayfadan yalnızca bağlantıları ayrıştırmak istiyorsanız orada 1 geçmektedir.
Eğer çok yakın Hobodave. Ben değişti tek şey bulundu çapa etiketinin href niteliği 'http' ile başlayan olmadığını görmek için denetler eğer deyimi içindedir. Bunun yerine sadece sizin geçildi sayfasını içerecek $ url değişkeni ekleyerek ilk parse_url php fonksiyonu kullanılarak yapılabilir konağa aşağı şerit gerekir.
<?php
function crawl_page($url, $depth = 5)
{
static $seen = array();
if (isset($seen[$url]) || $depth === 0) {
return;
}
$seen[$url] = true;
$dom = new DOMDocument('1.0');
@$dom->loadHTMLFile($url);
$anchors = $dom->getElementsByTagName('a');
foreach ($anchors as $element) {
$href = $element->getAttribute('href');
if (0 !== strpos($href, 'http')) {
/* this is where I changed hobodave's code */
$host = "http://".parse_url($url,PHP_URL_HOST);
$href = $host. '/' . ltrim($href, '/');
}
crawl_page($href, $depth - 1);
}
echo "New Page:<br /> ";
echo "URL:",$url,PHP_EOL,"<br />","CONTENT:",PHP_EOL,$dom->saveHTML(),PHP_EOL,PHP_EOL," <br /><br />";
}
crawl_page("http://hobodave.com/", 5);
?>
PHPCrawl, çok iyi ve iyi düşünülmüş paletli çerçevedir.
Soru ajax çağrıları kaynak kodunu almak için nasıl? Bu nasıl böyle bir linke resim tarama, örneğin, sürünerek değil mi? http://www.tiendeo.nl/Catalogi/amsterdam/16558&subori=web_sliders&buscar=Boni&sw=1366