gpt4 book ai didi

curl - 如何使用cURL单击链接?

转载 作者:行者123 更新时间:2023-12-04 18:16:00 26 4
gpt4 key购买 nike

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

forward  backward
把这两个当作两个链接。我想首先加载包含这些链接的页面,然后单击任何这些链接。注意[我不知道要单击的URL,因为它随便更改了]

最佳答案

这是一篇过时的文章,但是对于任何寻找答案的人,我遇到了类似的问题并能够解决。我将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

关于curl - 如何使用cURL单击链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3372909/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com