2010-07-30 4 views
5

par exemple, dans une page Web de nombreux liens sont donnés.comment cliquer sur un lien en utilisant cURL.?

forward backward 

prendre ce deux comme deux liens. Je veux d'abord charger cette page, qui contient ces liens et cliquer sur un de ces liens. NOTE [Je ne connais pas l'URL qui va charger après je l'ai cliqué comme il change aléatoirement]

Répondre

3

Vous devrez analyser le code HTML que cUrl a retourné et trouver les liens, puis les tirer par le biais d'une nouvelle requête crl.

+0

pouvez-vous me privide un exemple :) s'il vous plaît –

3

Ceci est un ancien article, mais pour tous ceux qui cherchaient une réponse, j'ai eu un problème similaire et j'ai réussi à le résoudre. J'ai utilisé PHP avec cUrl.

Le code permettant de suivre un lien via cUrl est très simple.

// Create a user agent so websites don't block you 
$userAgent = 'Googlebot/2.1 (http://www.google.bot.com/bot.html)'; 

// Create the initial link you want. 
$target_url = "http://www.example.com/somepage"; 

// Initialize curl and following options 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); 
curl_setopt($ch, CURLOPT_URL,$target_url); 
curl_setopt($ch, CURLOPT_FAILONERROR, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_AUTOREFERER, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); 
curl_setopt($ch, CURLOPT_TIMEOUT, 10); 


// Grab the html from the page 
$html = curl_exec($ch); 

// Error handling 
if(!$html){ 
    handle error if page was not reachable, etc 
    exit(); 
} 


// Create a new DOM Document to handle scraping 
$dom = new DOMDocument(); 
@$dom->loadHTML($html); 


// get your element, you can do this numerous ways like getting by tag, id or using a DOMXPath object 
// This example gets elements with id forward-link which might be a div or ul or li, etc 
// It then gets all the a tags (links) within all those divs, uls, etc 
// Then it takes the first link in the array of links and then grabs the href from the link 
$search = $dom->getElementById('forward-link'); 
$forwardlink = $search->getElementsByTagName('a'); 
$forwardlink = $forwardlink->item(0); 
$forwardlink = $getNamedItem('href'); 
$href = $forwardlink->textContent; 


// Now that you have the link you want to follow/click to 
// Set the target_url for the cUrl to the new url 
curl_setopt($ch, CURLOPT_URL, $target_url); 

$html = curl_exec($ch); 


// do what you want with your new link! 

C'est un excellent tutoriel pour suivre le chemin: php curl tutorial

+0

brillant! Je vous remercie. – adamj

Questions connexes