как нажать на ссылку, используя cURL.? - PullRequest
5 голосов
/ 30 июля 2010
Например,

, на веб-странице дано много ссылок.

forward  backward

принимает эти две ссылки как две.Я хочу сначала загрузить эту страницу, которая содержит эти ссылки и нажать на любую из этих ссылок.ПРИМЕЧАНИЕ [Я не знаю URL, который будет загружаться после того, как я нажму на него, так как он случайно изменяется]

Ответы [ 2 ]

4 голосов
/ 16 апреля 2012

Это старый пост, но для тех, кто ищет ответ, у меня была похожая проблема, и я смог ее решить.Я использовал PHP с cUrl.

Код для перехода по ссылке через cUrl очень прост.

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

3 голосов
/ 30 июля 2010

Вам придется проанализировать HTML-код, возвращенный cUrl, и найти ссылки, а затем перенаправить их через новый запрос cUrl.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...