吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4165|回复: 4
收起左侧

[其他转载] [Rust]Wallheaven壁纸拉取代码附exe

[复制链接]
cOldpure 发表于 2019-12-25 21:06
本帖最后由 cOldpure 于 2019-12-25 21:24 编辑

初学 Rust 再写个壁纸爬取的工具
学的不精 可能不够健壮
网站在国外 延迟肯定是有的 timeout也很随机

[package]
name = "wall_heaven_cc"
version = "0.1.0"
authors = ["coldpure9ev"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
reqwest = {version = "0.10.0-alpha.2", features = ["blocking", "json"]}
select = {version = "0.4.3"}
extern crate select;

use select::document::Document;
use select::predicate::{Predicate, Attr, Class, Name};
use std::ops::Deref;
use std::io::Write;

fn get_html_code(api: &str) -> Result<String, Box<dyn std::error::Error>> {
    let client = reqwest::blocking::Client::new();
    let res = client.get(api).send()?.text()?;
    Ok(res)
}

fn download_image(pic_url: &str) -> Result<String, Box<dyn std::error::Error>> {
    let client = reqwest::blocking::Client::new();
    let mut buf: Vec<u8> = vec![];
    let res = client.get(pic_url).send()?.copy_to(&mut buf)?;
    let filename = pic_url.split("/").collect::<Vec<&str>>();
    println!("{:?}", filename);
    let mut file = std::fs::File::create( "wallhaven/".to_string() + filename[filename.len() - 1]).unwrap();
    println!("文件打开成功:{:?}",file);
    file.write_all(buf.as_slice()).expect("write failed");
    Ok(format!("文件下载成功 {0}", pic_url))
}

fn wall_heaven(index: i32) {
    let api = format!("https://wallhaven.cc/search?categories=111&purity=100&resolutions=1920x1080&topRange=1M&sorting=toplist-beta&order=desc&page={}", index);
    match get_html_code(&api) {
        Ok(res) => {
            let document = Document::from(res.deref());
            let mut img_list = Vec::new();
            println!("# Start");
            for node in document.find(Class("thumb-listing-page")) {
                for img_node in node.find(Name("img")) {
                    img_list.push(img_node.attr("data-src").unwrap().get(33..39).unwrap())
                }
            }
            for pic in img_list {
                let pic_html_url = format!("https://wallhaven.cc/w/{0}", pic);
                // let pic_html = get_html_code(&pic_html_url).unwrap();
                match get_html_code(&pic_html_url) {
                    Ok(n) => {
                        let document_pic = Document::from(n.deref());
                        let pic_url = document_pic.find(Attr("id", "wallpaper")).next().unwrap().attr("src").unwrap();
                        match download_image(pic_url) {
                            Ok(n) => {
                                println!("下载成功! {}", n)
                            },
                            Err(e) => {
                                println!("Dowload_image Error! {}", e);
                            }
                        }
                    },
                    Err(e) => {
                        println!("get_html_code Error! {}", e);
                    }
                };
            }
        },
        Err(e) => {
            println!("get_html_code Error! {}", e);
        }
    }
}

fn main() {
    print!("开始拉取wallheavn 壁纸.......");
    for n in 0..101 {
        println!("当前抓取页数: {}", n);
        wall_heaven(n)
    }
    println!("拉取完毕!")
}



https://www.lanzouj.com/i89l8ri
使用的时候在同目录新建一个wallhaven文件夹即可 不然可能报错文件目录找不到

免费评分

参与人数 2吾爱币 +2 热心值 +2 收起 理由
Nemoris丶 + 1 + 1 我很赞同!
FleTime + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

cherrypi 发表于 2019-12-25 21:48
不错不错,已经在用了。
裴冰夏 发表于 2019-12-25 22:40
风二中 发表于 2019-12-27 09:29
有时候关掉,都要从第一页开始下载

自己用py写了一个
NaiveBlue 发表于 2020-1-5 01:06
这壁纸的清晰度太低了哇
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-9 12:14

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表