【问题标题】:php download all images of webpage from linkphp从链接下载网页的所有图像
【发布时间】:2020-10-09 14:46:12
【问题描述】:

当我使用 curl 或 file_get_contents 下载 html 时,我没有得到 https://www.tumbex.com/memes.tumblr/posts?page=2

和代码(第一次尝试)

$html = file_get_contents('https://www.tumbex.com/memes.tumblr/posts?page=2');

和代码(第二次尝试)

$html = get_dataa('https://www.tumbex.com/memes.tumblr/posts?page=2');

    echo($html);

function get_dataa($url) {
  $ch = curl_init();
  $timeout = 5;
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
  curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}

【问题讨论】:

  • 该页面似乎正在运行 JavaScript 以更新 UI。检查浏览器调试工具中的网络选项卡,查看它发出的所有请求。对其进行逆向工程以抓取其数据,这将比仅抓取初始 HTML 涉及更多内容。
  • 要查看该页面的卷曲效果,请禁用 JavaScript 并重新加载该页面。要解决这个问题,您可能需要阅读并理解以下内容:Tumblr API

标签: php curl


【解决方案1】:

只需在您的 chrome 上按 F12,然后转到“网络”标签。
在那里找到tumbex的api url,然后将其与请求头一起复制。

如果完成,
您可以使用 curl 到该 url (api url) 来获得响应..
这是我的代码

<?php
$page = 1; //change number of page here
$url = "https://api.1.tumbex.com/api/tumblr/posts?tumblr=memes&type=posts&page=$page&tag=";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = array(
    'Accept: */*',
    'Authorization: Bearer 0fae0f237b33e781a6884295b39c6e903484ef1ee3190bd51f07dd9881bdccbd',
    'content-type: application/json; charset=UTF-8',
    'Referer: https://www.tumbex.com/',
    'Accept: application/json, text/plain, */*',
    'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36',
    'x-csrf-token: MVMyb2hLTWtQdEJEYjJ0SER1dEwvZz09',
    'x-requested-with: XMLHttpRequest'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$res = curl_exec($ch); //result is json

$json = json_decode($res, true);
$edan = $json['response']['posts'];

for($i=0; $i<count($edan); $i++){

    $get_post       = $edan[$i];
    $type           = $get_post['detected_type']; //get type

    //$get_photo = $get_post['blocks'][0]['content'][0]['hd'];  -> get image url
    //$get_video = $get_post['blocks'][0]['content'][0]['media']['url']; -> get video url
    //$get_text1 = $get_post['blocks'][0]['content'][0]['text']; -> get text 1
    //$get_text2 = $get_post['blocks'][0]['content'][1]['text']; -> get text 2

    if($type == 'photo'){
        $get_photo  = $get_post['blocks'][0]['content'][0]['hd'];
        echo "<img src='".$get_photo."' height='120' width='160'><br>";
    }

}

你要知道,bearer tokenx-csrf-token总是在变化
如果结果为空,那就是Bearer token 和 x-csrf-token 已过期
但是,您可以手动解决该问题或使用其他 curl 自动获取 承载令牌 x-csrf-token..

【讨论】:

  • 感谢您的回答。但我试过了,它不起作用。 $res 是空的,你是怎么找到那个链接的?
  • @thepepp 再次阅读,“你必须知道,如果结果为空,承载令牌和 x-csrf-token 总是在变化,这意味着承载令牌和 x-csrf-token 已过期但是,您可以手动解决该问题或使用其他 curl 自动获取不记名令牌和 x-csrf-token.."
  • 对不起,我是菜鸟。不记名令牌和 x-csrf-token 存储在哪里?而且我还是不知道你是怎么找到链接的api.1.tumbex.com/api/tumblr/…
猜你喜欢
  • 2019-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多