鱼C论坛

 找回密码
 立即注册
查看: 1531|回复: 9

[技术交流] rust/c++ 多线程

[复制链接]
发表于 2020-5-30 22:51:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 赚小钱 于 2020-7-25 16:00 编辑

.
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-5-30 22:54:10 | 显示全部楼层
本帖最后由 赚小钱 于 2020-7-25 16:01 编辑

.
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-5-30 23:10:05 | 显示全部楼层
c++写爬虫太艹
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-30 23:42:27 | 显示全部楼层
  1. use futures::StreamExt;
  2. use hyper_tls::HttpsConnector;
  3. use std::io::Write;
  4. use hyper::body::HttpBody as _;

  5. #[tokio::main]
  6. async fn main() {
  7.     let u = "https://wallhaven.cc";
  8.     let cookie = "";

  9.     crawl(&u, &cookie).await;
  10. }

  11. async fn crawl(base: &str, cookie: &str) {
  12.     let mut next_pager = Some(format!("{}&page=1", base));
  13.     loop {
  14.         if next_pager.is_none() {
  15.             return;
  16.         }
  17.         next_pager = process_one(next_pager.as_ref().unwrap(), &cookie).await;
  18.     }
  19. }

  20. async fn process_one(u: &str, cookie: &str) -> Option<String> {
  21.     let next_pager_url = get_next_page_url(&u, &cookie).await;
  22.     let body = do_request(&u, &cookie).await;
  23.     let urls = parse_image_url(&body).await;
  24.     println!("next: {:?}", next_pager_url);
  25.     println!("urls: {:?}", urls);
  26.     for url in urls {
  27.         if download(&url, &cookie).await {
  28.             std::thread::sleep(std::time::Duration::from_millis(500));
  29.         }
  30.     }
  31.     next_pager_url
  32. }

  33. async fn get_next_page_url(base: &str, cookie: &str) -> Option<String> {
  34.     let body = do_request(base, cookie).await;
  35.     /// class selector:
  36.     /// grammar: .class_name .thumb-listing-page-num
  37.     let document = scraper::Html::parse_document(&body);
  38.     // let overlay_close_selector = scraper::Selector::parse("#overlay-close").unwrap();
  39.     // document.select(&overlay_close_selector).next().map(|overlay| {
  40.     //     println!("overlay close title: {:?}", overlay.value().attr("title"))
  41.     // });
  42.     let next_pager_selector = scraper::Selector::parse(".next").unwrap();
  43.     document.select(&next_pager_selector).next().map(|next_pager| {
  44.         // println!("page_num_element: {:?}", next_pager.inner_html());
  45.         String::from(next_pager.value().attr("href").unwrap())
  46.     })
  47. }

  48. async fn do_request(u: &str, cookie: &str) -> String {
  49.     let https = HttpsConnector::new();
  50.     let client = hyper::Client::builder().build::<_, hyper::Body>(https);
  51.     let request = hyper::Request::get(u).
  52.         header("Cookie", cookie).
  53.         body(hyper::Body::empty()).unwrap();
  54.     let mut response = match client.request(request).await {
  55.         Ok(response) => response,
  56.         Err(e) => {
  57.             println!("do request error: {:?}", e);
  58.             panic!("");
  59.         }
  60.     };

  61.     let body = hyper::body::to_bytes(response.into_body()).await.unwrap();
  62.     let str_body: String = unsafe { String::from_utf8_unchecked(body.to_vec()) };
  63.     str_body
  64. }

  65. async fn parse_image_url(body: &str) -> Vec<String> {
  66.     let document = scraper::Html::parse_document(&body);
  67.     let preview_selector = scraper::Selector::parse(".preview").unwrap();
  68.     let urls: Vec<String> = document.select(&preview_selector).filter(|element| {
  69.         element.value().attr("href").is_some()
  70.     }).map(|element| {
  71.         String::from(element.value().attr("href").unwrap())
  72.     }).collect();
  73.     urls
  74. }


  75. async fn download(address: &str, cookie: &str) -> bool {
  76.     println!("address: {}", address);
  77.     let body = do_request(&address, &cookie).await;
  78.     let selector = r#"id="wallpaper""#;
  79.     if let Some(index) = body.find(&selector) {
  80.         let body = &body[index + selector.len()..];
  81.         if let Some(src_index) = body.find("src") {
  82.             let body = &body[src_index + 5..];
  83.             if let Some(end_index) = body.find(r#"""#) {
  84.                 let src = &body[..end_index];
  85.                 println!("wallpaper src: {}", src);
  86.                 let slash_index = match address.rfind('/') {
  87.                     Some(index) => index,
  88.                     None => return false,
  89.                 };
  90.                 let name = &address[slash_index + 1..];
  91.                 let name = format!("./output/{}", name);
  92.                 let path = std::path::Path::new(&name);
  93.                 if path.exists() {
  94.                     return false;
  95.                 }
  96.                 let image = do_request(&src, &cookie).await;
  97.                 save(&name, image.as_bytes()).await;
  98.                 return true;
  99.             }
  100.         }
  101.         return false;
  102.     }
  103.     return false;
  104. }

  105. async fn save(name: &str, body: &[u8]) {
  106.     let mut file = std::fs::File::create(name).unwrap();
  107.     file.write_all(body);
  108. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-30 23:43:28 | 显示全部楼层

没有,爬虫是 rust 写的。刚把代码补上。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-13 10:49:04 | 显示全部楼层
赚小钱 发表于 2020-5-30 23:43
没有,爬虫是 rust 写的。刚把代码补上。

问问,这个rust好学吗?c太难了。编译总是逻辑错误。不学编程,不知道自己逻辑不行。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-13 22:32:15 From FishC Mobile | 显示全部楼层
mmld32 发表于 2021-3-13 10:49
问问,这个rust好学吗?c太难了。编译总是逻辑错误。不学编程,不知道自己逻辑不行。

相同难度等级。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-17 01:27:39 | 显示全部楼层

好吧.我还是用最笨的方法,学习python吧.听说python最简单了.只要不是数学不及格,都能过过.我数学刚刚好61.应该不会有问题吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-17 21:59:22 From FishC Mobile | 显示全部楼层
mmld32 发表于 2021-3-17 01:27
好吧.我还是用最笨的方法,学习python吧.听说python最简单了.只要不是数学不及格,都能过过.我数学刚刚好61 ...

放轻松,没那么难。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2021-3-18 00:03:31 | 显示全部楼层
赚小钱 发表于 2021-3-17 21:59
放轻松,没那么难。

其实吧.写代码本身对于我来说并不是特别难的事情.只要记住格式,基本都能用得了.就算是一些硬是要合成一段的代码,也可以分开拆解,成比较好理解的格式.
就编程语言里面,我个人感觉c是属于比较逻辑清晰的
python最大的问题是我不会用那些库,如果是单纯用原始的python总有一些不清不楚的数学问题,要自己思考的.高中数学,大学数学微积分,这些转化为编程的数学时候,很吃力.
算法这块,我说感觉,没什么希望了.就说需要一些功能性的东西,应用在实际工作中就行.
毕竟现在的计算机技术,就硬件这块,应该说对比当年的c的时代,还是宽裕太多了.内存不够用的问题,基本不存在,唯一比较,让我感觉难搞 的是,c里面对于内存溢出的问题.
还是先学python吧.
思路上,编程应该是都相通的,实现过程不一样而已.
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-30 18:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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