把下面代码改为html代码import requestsosfrom bs4 import BeautifulSoupab = 1url = httpswallhavenccsearch# 如果不存在bizhi文件夹创建壁纸文件夹if not ospathexistsbizhi osmkdirbizhikeyword = input请输入你要爬取的壁纸关键词:start_page = in
<!DOCTYPE html>
<html>
<head>
<title>爬取壁纸</title>
</head>
<body>
<script>
function crawlWallpapers() {
var ab = 1;
var url = 'https://wallhaven.cc/search';
var keyword = prompt("请输入你要爬取的壁纸关键词:");
var start_page = parseInt(prompt("起始页码:"));
var end_page = parseInt(prompt("结束页码:")) + 1;
<pre><code> for (var i = start_page; i < end_page; i++) {
var data = {
'q': keyword,
'sorting': 'random',
'ref': 'fp',
'seed': 'pDRjMC',
'page': i,
};
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
var resp = xhr.responseText;
var page = new DOMParser().parseFromString(resp, "text/html");
var urls = page.getElementsByClassName("preview");
for (var j = 0; j < urls.length; j++) {
var href = urls[j].getAttribute("href");
var xhr1 = new XMLHttpRequest();
xhr1.onreadystatechange = function() {
if (xhr1.readyState == 4 && xhr1.status == 200) {
var resp1 = xhr1.responseText;
var page1 = new DOMParser().parseFromString(resp1, "text/html");
var img = page1.getElementById("wallpaper");
var img_url = img.getAttribute("src");
var xhr2 = new XMLHttpRequest();
xhr2.onreadystatechange = function() {
if (xhr2.readyState == 4 && xhr2.status == 200) {
var resp2 = xhr2.response;
var blob = new Blob([resp2], {type: "image/jpeg"});
var link = document.createElement("a");
link.href = URL.createObjectURL(blob);
link.download = "bizhi/" + ab + ".jpg";
link.click();
ab++;
}
};
xhr2.open("GET", img_url, true);
xhr2.responseType = "arraybuffer";
xhr2.send();
}
};
xhr1.open("GET", href, true);
xhr1.send();
}
}
};
xhr.open("GET", url, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(Object.keys(data).map(function(k) {
return encodeURIComponent(k) + "=" + encodeURIComponent(data[k]);
}).join("&"));
}
alert("爬取完毕");
}
crawlWallpapers();
</script>
</code></pre>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/i2z3 著作权归作者所有。请勿转载和采集!