2010-07-30 13 views
5

उदाहरण के लिए, एक वेब पेज में कई लिंक दिए जाते हैं।curl का उपयोग कर लिंक पर क्लिक कैसे करें।?

forward backward 

इन दोनों को दो लिंक के रूप में लें। मैं पहले इस पृष्ठ को लोड करना चाहता हूं, जिसमें इस लिंक शामिल हैं और इनमें से किसी भी लिंक पर क्लिक करें। नोट [मैं उस यूआरएल को नहीं जानता जो इसे लोड करने के बाद लोड हो रहा है क्योंकि यह यादृच्छिक रूप से बदलता है]

उत्तर

3

आपको HTML को पार्स करना होगा कि crrl वापस लौटाए और लिंक ढूंढें, फिर उन्हें एक नए cUrl अनुरोध के माध्यम से खींचें।

+0

तुम मुझे एक उदाहरण के साथ privide कर सकते हैं :) कृपया –

3

यह एक पुरानी पोस्ट है लेकिन किसी के उत्तर देने के लिए, मेरे पास एक समान समस्या थी और इसे हल करने में सक्षम था। मैंने सीयूआरएल के साथ PHP का इस्तेमाल किया।

सीयूआरएल के माध्यम से एक लिंक का पालन करने के लिए कोड बहुत आसान है।

// 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! 

यह एक उत्कृष्ट ट्यूटोरियल माध्यम से पालन करने के लिए है: php curl tutorial

+0

शानदार! धन्यवाद। – adamj

संबंधित मुद्दे