我正在写一个网站,在那里我加载这个链接(https://source.unsplash.com/random/1920x1080/?wallpaper,landscape)来获得一张随机的照片。链接后面的照片定期变化,当它加载到网站时,我只看到这个链接https://source.unsplash.com/random/1920x1080/?wallpaper,landscape,你认为有什么方法可以像这样获得真正的源代码:"https://images.unsplash.com/photo-1458571037713-913d8b481dc6?crop=entropy&cs=tinysrgb&fit=crop&fm=jpg&h=1080&ixid=eyJhcHBfaWQiOjF9&ixlib=rb-1.2.1&q=80&w=1920“。当我打开chrome devtools时,我可以在应用程序选项卡中看到源代码,但是我如何访问它们呢?
发布于 2019-10-11 04:47:00
您可以使用axios库来获取镜像并获取响应url:
let randomURL = 'https://source.unsplash.com/random/1920x1080/?wallpaper,landscape';
axios.get(randomURL).then( data => {
// the url of the random img
console.log(data.request.responseURL);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.0/axios.min.js" integrity="sha256-S1J4GVHHDMiirir9qsXWc8ZWw74PHHafpsHp5PXtjTs=" crossorigin="anonymous"></script>
和vanilla JS:
fetch("https://source.unsplash.com/random/1920x1080/?wallpaper,landscape").then( data => {
console.log(data.url);
});
如果你想支持旧的浏览器:
request = new XMLHttpRequest();
request.open("GET", "https://source.unsplash.com/random/1920x1080/?wallpaper,landscape", true);
request.send(null);
request.onreadystatechange = function() {
if (request.readyState === 4) {
if (request.status === 200) {
console.log(request.responseURL);
}
}
}
https://stackoverflow.com/questions/58330708
复制相似问题