gpt4 book ai didi

php - 如何抓取登录保护的页面?

转载 作者:搜寻专家 更新时间:2023-10-31 21:13:08 27 4
gpt4 key购买 nike

我想抓取站点搜索信息,我的脚本对普通页面工作正常,但如何抓取登录保护页面?我有登录信息 我如何使用 url 发送此信息?我的代码:

crawl_page("http://www.site.com/v3/search/results?start=1&sortCol=MyDefault");
function crawl_page($url) {
$html = file_get_contents($url);
preg_match_all('~<a.*?href="(.*?)".*?>~', $html, $matches);
$str = "http://www.site.com/v3/features/id?Profile=";
foreach($matches[1] as $newurl) {
if (strpos($newurl, $str) !== false) {
$my_file = 'link.txt';
$handle = fopen($my_file, 'a') or die('Cannot open file: '.$my_file);
$numberNewline = $newurl . PHP_EOL;
fwrite($handle, $numberNewline);
}
}
}

任何帮助谢谢。

最佳答案

这在很大程度上取决于所使用的身份验证方法。最简单的一种是 HTTP Basic Auth。对于该方法,您只需要像这样构建一个上下文:

$context = stream_context_create(array(
'http' => array(
'header' => "Authorization: Basic " . base64_encode("$username:$password")
)
));
$data = file_get_contents($url, false, $context);

这样,file_get_contents 将使用 HTTP 基本身份验证。

其他身份验证方法可能需要更多工作,例如通过 POST 将密码发送到登录页面和存储 session cookie。

关于php - 如何抓取登录保护的页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14731787/

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