从 URL - PHP cURL 加载下一组结果
Load next set results from URL - PHP cURL
寻求一些帮助,我正在使用 curl 从所述网站提取数据,在该网站上,他们在第一页上有 10 个结果,然后下一组 10 个结果在下一页上附加 ?page=2等等。
我确实尝试了一个循环,但它似乎没有用,任何我可以使用的建议,最好是一个滚动来加载更多但想先让他的卷曲部分正确。
下面是我用作示例的测试代码,完整版本包括附加到 URL 的 post 参数,但只需要下一个结果
<?php
// Main url but the next result will be on https://example.org/data/?page=2
$url = "https://example.org/data";
$result = get($url) ;
function get ($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.85 Safari/537.36');
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
preg_match_all('!<h1>(.*?)<\/h1>!',$result,$title);
for ($i = 0; $i < count($result[1]); $i++) {
echo '<h1>' . $title[1][$i] . '"</h1>';
}
对于像我一样阅读本文进行学习的所有人,一旦值匹配,上面的代码也适用于在任何给定 URL 上基本提取 H1 header,如果我能提供帮助的话对于新程序员的任何基本问题,我都会。
修改后的示例显示 URL 中第 1,2 页的示例。
<?php
for ($i = 1; $i <= 2; $i++) {
$url = "https://www.gamespot.com/search/?q=gta&page=". $i;
echo $url . "<br>";
}
$result = get($url) ;
function get ($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.85 Safari/537.36');
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
preg_match_all('!<h4 class="media-title" style="margin:0;padding-bottom:4px;">
<span style="font-weight:bold;"><a href=".*?">(.*?)<\/a><\/span>
<\/h4>!',$result,$title);
for ($i = 0; $i < count($title[1]); $i++) {
echo '<p>' . $title[1][$i] . '</p>';
}
好的,经过许多小时的研究和失败,我完成了以下工作,我希望与您分享。
我设置了一些变量,比如下面的值
// Get the value of $pg through the GET value of 'page'
$pg = $_GET['page'];
// Increase the $pg variable when clicking $next/$prev with +1 or -1
$next = $pg +1;
$prev = $pg -1;
// Append the $pg value to the CURL url
$url = "https://www.gamespot.com/search/?q=gta&page=".$pg;
// The next & previous
<?php
echo '<div class="btn-group special">';
// Added an IF statement so that this does not go to the -1 values
if ($prev >= 1) {
echo '<a href="results.php?page='.$prev.'" class="btn btn-info" role="button"><i class="fas fa-chevron-left"></i></a>';
} else {
// Nothing to display
}
echo '<a href="results.php?page='.$next.'" class="btn btn-info" role="button"><i class="fas fa-chevron-right"></i></a>';
echo '</div>'
?>
寻求一些帮助,我正在使用 curl 从所述网站提取数据,在该网站上,他们在第一页上有 10 个结果,然后下一组 10 个结果在下一页上附加 ?page=2等等。
我确实尝试了一个循环,但它似乎没有用,任何我可以使用的建议,最好是一个滚动来加载更多但想先让他的卷曲部分正确。
下面是我用作示例的测试代码,完整版本包括附加到 URL 的 post 参数,但只需要下一个结果
<?php
// Main url but the next result will be on https://example.org/data/?page=2
$url = "https://example.org/data";
$result = get($url) ;
function get ($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.85 Safari/537.36');
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
preg_match_all('!<h1>(.*?)<\/h1>!',$result,$title);
for ($i = 0; $i < count($result[1]); $i++) {
echo '<h1>' . $title[1][$i] . '"</h1>';
}
对于像我一样阅读本文进行学习的所有人,一旦值匹配,上面的代码也适用于在任何给定 URL 上基本提取 H1 header,如果我能提供帮助的话对于新程序员的任何基本问题,我都会。
修改后的示例显示 URL 中第 1,2 页的示例。
<?php
for ($i = 1; $i <= 2; $i++) {
$url = "https://www.gamespot.com/search/?q=gta&page=". $i;
echo $url . "<br>";
}
$result = get($url) ;
function get ($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.85 Safari/537.36');
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
preg_match_all('!<h4 class="media-title" style="margin:0;padding-bottom:4px;">
<span style="font-weight:bold;"><a href=".*?">(.*?)<\/a><\/span>
<\/h4>!',$result,$title);
for ($i = 0; $i < count($title[1]); $i++) {
echo '<p>' . $title[1][$i] . '</p>';
}
好的,经过许多小时的研究和失败,我完成了以下工作,我希望与您分享。
我设置了一些变量,比如下面的值
// Get the value of $pg through the GET value of 'page'
$pg = $_GET['page'];
// Increase the $pg variable when clicking $next/$prev with +1 or -1
$next = $pg +1;
$prev = $pg -1;
// Append the $pg value to the CURL url
$url = "https://www.gamespot.com/search/?q=gta&page=".$pg;
// The next & previous
<?php
echo '<div class="btn-group special">';
// Added an IF statement so that this does not go to the -1 values
if ($prev >= 1) {
echo '<a href="results.php?page='.$prev.'" class="btn btn-info" role="button"><i class="fas fa-chevron-left"></i></a>';
} else {
// Nothing to display
}
echo '<a href="results.php?page='.$next.'" class="btn btn-info" role="button"><i class="fas fa-chevron-right"></i></a>';
echo '</div>'
?>