如何使用cURL单击链接。 [英] how to click a link using cURL.?

查看:159
本文介绍了如何使用cURL单击链接。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,在网页中给出了很多链接。

for example, in a web page many links are given.

forward  backward

将这两个链接作为两个链接。我想先加载此页面,其中包含此链接,并点击任何这些链接。注意[我不知道我点击它之后加载的网址,因为它随机更改了]

take this two as two links. i want to first to load this page, wich contains this links and click on any of those links. NOTE[i dont know the URL thats gonna load after i click it as it randomly changes]

推荐答案

对于任何人寻找答案,我有一个类似的问题,并能够解决它。我使用PHP和cUrl。

This is an old post but for anyone searching for an answer, I had a similar issue and was able to solve it. I used PHP with cUrl.

通过cUrl跟踪链接的代码非常简单。

The code to follow a link through cUrl is very 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!

这是一个很好的教程: php curl教程

This is an excellent tutorial to follow by the way: php curl tutorial

这篇关于如何使用cURL单击链接。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆