加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

如何使用cURL单击链接.?

发布时间:2020-12-14 01:06:25 所属栏目:Linux 来源:网络整理
导读:例如,在网页中给出了许多链接. forward backward 把这两个作为两个链接.我想首先加载此页面,其中包含此链接并单击任何这些链接.注意[我不知道随机更改后点击它后会加载的URL] 解决方法 这是一个老帖子,但对于任何寻找答案的人来说,我遇到了类似的问题,并且能
例如,在网页中给出了许多链接.

forward  backward

把这两个作为两个链接.我想首先加载此页面,其中包含此链接并单击任何这些链接.注意[我不知道随机更改后点击它后会加载的URL]

解决方法

这是一个老帖子,但对于任何寻找答案的人来说,我遇到了类似的问题,并且能够解决它.我在cUrl中使用了PHP.

通过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,CURLOPT_AUTOREFERER,CURLOPT_RETURNTRANSFER,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,$target_url);

$html = curl_exec($ch);


// do what you want with your new link!

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读