<!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 &lt; 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 &amp;&amp; xhr.status == 200) {
					var resp = xhr.responseText;
					var page = new DOMParser().parseFromString(resp, &quot;text/html&quot;);
					var urls = page.getElementsByClassName(&quot;preview&quot;);

					for (var j = 0; j &lt; urls.length; j++) {
						var href = urls[j].getAttribute(&quot;href&quot;);

						var xhr1 = new XMLHttpRequest();
						xhr1.onreadystatechange = function() {
							if (xhr1.readyState == 4 &amp;&amp; xhr1.status == 200) {
								var resp1 = xhr1.responseText;
								var page1 = new DOMParser().parseFromString(resp1, &quot;text/html&quot;);
								var img = page1.getElementById(&quot;wallpaper&quot;);
								var img_url = img.getAttribute(&quot;src&quot;);

								var xhr2 = new XMLHttpRequest();
								xhr2.onreadystatechange = function() {
									if (xhr2.readyState == 4 &amp;&amp; xhr2.status == 200) {
										var resp2 = xhr2.response;
										var blob = new Blob([resp2], {type: &quot;image/jpeg&quot;});
										var link = document.createElement(&quot;a&quot;);
										link.href = URL.createObjectURL(blob);
										link.download = &quot;bizhi/&quot; + ab + &quot;.jpg&quot;;
										link.click();
										ab++;
									}
								};
								xhr2.open(&quot;GET&quot;, img_url, true);
								xhr2.responseType = &quot;arraybuffer&quot;;
								xhr2.send();
							}
						};
						xhr1.open(&quot;GET&quot;, href, true);
						xhr1.send();
					}
				}
			};
			xhr.open(&quot;GET&quot;, url, true);
			xhr.setRequestHeader(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;);
			xhr.send(Object.keys(data).map(function(k) {
				return encodeURIComponent(k) + &quot;=&quot; + encodeURIComponent(data[k]);
			}).join(&quot;&amp;&quot;));
		}

		alert(&quot;爬取完毕&quot;);
	}

	crawlWallpapers();
&lt;/script&gt;
</code></pre>
</body>
</html>
把下面代码改为html代码import requestsosfrom bs4 import BeautifulSoupab = 1url = httpswallhavenccsearch# 如果不存在bizhi文件夹创建壁纸文件夹if not ospathexistsbizhi osmkdirbizhikeyword = input请输入你要爬取的壁纸关键词:start_page = in

原文地址: https://www.cveoy.top/t/topic/i2z3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录