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

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 9188|回复: 116
收起左侧

[其他转载] C#简单爬虫,爬取小姐姐合集照(懂得都懂,斜眼笑)带源码

  [复制链接]
小白1324 发表于 2021-3-9 13:09
源码在下面;控制台小程序,附件里有软件。话不多说自己慢慢研究吧(我自己也是小白,自己边学边做的)不好勿喷。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace _38.半次元
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("半次元");

            //存正则提取出来的链接
            List<string> zhuye = new List<string>();
            zhuye.Clear();//毫无卵用

            //存下一页的链接
            List<string> xiayiye = new List<string>();
            xiayiye.Clear();//毫无卵用

            //请求网页链接的类
            WebClient lianjie1 = new WebClient();

            //指定下载网页的编码,防止出现乱码(网页是哪个编码就哪个编码)
            lianjie1.Encoding = System.Text.Encoding.UTF8;

            //经过查看源码得知网页翻页只有单个字符改变,(页数)代码如下,只有数字改变
            //<a href="/acg/cos/index_18.html">18</a>
            int a = 0;
            //所以,我们将1-末尾页20页链接拼接好存入集合中
            for (int i = 2; i <= 20; i++)
            {
                //起始页与其他页面不同
                if (a == 0)
                {
                    xiayiye.Add(@"https://t2cy.com/acg/cos/index.html");
                }
                a++;
                xiayiye.Add(@"https://t2cy.com/acg/cos/index_" + i + ".html");
            }

            for (int i = 0; i < xiayiye.Count; i++)//循环输出一下看看页面是否正确
            {
                Console.WriteLine(xiayiye[i]);
            }
            Console.WriteLine("当前网页共有{0}个页面", xiayiye.Count);
            Console.WriteLine("按下回车继续!");
            Console.ReadKey();

            //存好以后将下一页的链接循环一遍,保存主页cos链接
            for (int i = 0; i < xiayiye.Count; i++)
            {
                string sw = lianjie1.DownloadString(xiayiye[i]);
                //<a href=""(.+?)"".+?block;"></a>
                MatchCollection tiqu = Regex.Matches(sw, @"<a href=""(.+?)"".+?");
                foreach (Match tiqu1 in tiqu)
                {
                    //将杂项链接去除
                    if (tiqu1.Groups[1].Value.Length < 36)
                    {

                    }
                    else
                    {
                        if (zhuye.Contains(@"https://t2cy.com" + tiqu1.Groups[1].Value))
                        {

                        }
                        else
                        {
                            zhuye.Add(@"https://t2cy.com" + tiqu1.Groups[1].Value);
                        }

                    }
                }

            }
            //输出查看提取的主页链接
            for (int i = 0; i < zhuye.Count; i++)
            {
                Console.WriteLine(zhuye[i]);
            }
            Console.WriteLine(zhuye.Count());
            Console.WriteLine("按下回车继续");
            Console.ReadKey();

            //存好了合集后我们将合集里面的图片一个个下载下来
            for (int i = 0; i < zhuye.Count; i++)
            {
                List<string> tp = new List<string>();
                tp.Clear();
                List<string> mz = new List<string>();
                mz.Clear();
                string swsa = lianjie1.DownloadString(zhuye[i]);//下载页面
                //^<p><img src=""(.+?)"" alt="".+?cos""/></p>$
                MatchCollection jihe34 = Regex.Matches(swsa, @".+?<img src=""(.+?)"" alt="".+?cos"".+?");//提取页面里图片链接
                //int n = 0;
                //循环集合提取图片li链接
                foreach (Match abcd in jihe34)
                {
                    //n++;
                    if (true)
                    {
                        tp.Add(@"https://t2cy.com" + abcd.Groups[1].Value);
                    }
                    else
                    {

                    }
                }
                //提取另一种图片链接
                MatchCollection jihe35 = Regex.Matches(swsa, @".+?data-loadsrc=""(.+?)"" alt=""([^【】]+?)"".+?");
                foreach (Match it in jihe35)
                {
                    string s = it.Groups[2].Value;
                    if (it.Groups[1].Value.Length >75)
                    {

                    }
                    else
                    {
                        if (it.Groups[2].Value.Length==s.Length)
                        {
                            tp.Add(@"https://t2cy.com" + it.Groups[1].Value);
                            mz.Add(it.Groups[2].Value);//保存图片名字
                        }
                    }
                }
                if (i>0)
                {
                    Console.WriteLine("为了防止服务器请求频繁,下载一个合集就等几秒钟吧");
                    Console.WriteLine("按下回车继续");
                    Console.ReadKey();
                }
                string swwaw = @"D:\图片下载\" + mz[1].ToString();//创建一个文件夹,将循环里保存的名字作为文件夹名字
                Directory.CreateDirectory(swwaw);
                int jiusan = 0;
                //图片链接和文件名字都保存好了,那么开始循环下载
                for (int j = 0; j < tp.Count; j++)
                {

                    try
                    {
                        Console.WriteLine(tp[j]);//正在下载的链接
                        Console.WriteLine("开始下载{0}", tp[i]);
                        lianjie1.DownloadFile(tp[j], @swwaw + "\\" + mz[1] + j + ".jpg");//将图片下载到指定的文件夹内,并添加后缀名
                        Console.WriteLine("下载中.........");
                        Console.WriteLine("下载完成");
                        Console.WriteLine();
                    }
                    catch
                    {

                    }
                }

            }
            Console.WriteLine("程序运行结束");
            Console.ReadKey();
        }
    }
}
image.png

38.半次元.rar

19.14 KB, 下载次数: 574, 下载积分: 吾爱币 -1 CB

免费评分

参与人数 17吾爱币 +16 热心值 +17 收起 理由
chiyaoss + 1 + 1 我很赞同!
xkoon + 1 + 1 用心讨论,共获提升!
jiujiuliualong + 1 + 1 谢谢@Thanks!
树先生诶 + 1 + 1 用心讨论,共获提升!
onlylonely + 1 + 1 谢谢@Thanks!
Rubyao + 1 + 1 谢谢@Thanks!
朝闻道夕死可矣 + 1 我很赞同!
GGsimida + 1 + 1 谢谢@Thanks!
椰果Cc + 1 + 1 用心讨论,共获提升!
腰围两尺99 + 1 + 1 用心讨论,共获提升!
endlesschy + 1 + 1 谢谢@Thanks!
Junter + 1 + 1 用心讨论,共获提升!
王星星 + 1 + 1 谢谢@Thanks!
阿傑 + 1 + 1 网站收藏了
Leon_Woodson + 1 + 1 谢谢@Thanks!
boy666 + 1 + 1 网站不错,收藏了!!
稻草人DCR + 1 + 1 谢谢@Thanks!

查看全部评分

本帖被以下淘专辑推荐:

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

mysy1108 发表于 2021-3-9 14:06
为了了解年轻人的爱好,跟上时代发展的节奏,老夫也用软件试试效果
Fripside1983 发表于 2021-3-9 13:31
Fxhlt 发表于 2021-3-9 13:31
满脸牛肉 发表于 2021-3-9 13:35
潜力巨大哦楼主
jiuzhou 发表于 2021-3-9 13:38
萌新问一下怎么用
红客联盟红哥 发表于 2021-3-9 13:52
如何使用可否讲明
beigu 发表于 2021-3-9 13:57
感谢分享!
feelsoright 发表于 2021-3-9 13:58
来学习一下,C#的爬虫
soyadokio 发表于 2021-3-9 14:14
C#爬虫很少见噢,支持
您需要登录后才可以回帖 登录 | 注册[Register]

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

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

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

GMT+8, 2024-5-6 16:46

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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