zoukankan      html  css  js  c++  java
  • 用python爬点高质量的壁纸换换,每天保持心情愉悦!

    每天我的壁纸都是Windows自带的天蓝色,看的真的没意思,有意思吗,没意思~
    所以啊,当然的整一手高质量壁纸,没有别的意思,只为了心情愉悦~
     
    好了,不多哔哔,开启今天的高质量旅途~
     
     
    一、准备工作
     
    这些统统安排上
     
    软件
    • python 3.6
    • pycharm
    模块
    • requests
    • parsel
    二、爬虫流程
     
    关于数据来源查找:
     
    确定目标需求: 爬取高清壁纸图片 (彼岸)
    通过开发者工具(F12或者鼠标右键点击检查) 查找图片的url地址来源;
    请求 壁纸的详情页 获取它网页源代码 就可以获取图片url地址了 (一张);
    请求 列表页就可以获取 每个壁纸的详情页url 以及 标题;
     
    代码实现
     
    1、发送请求
    壁纸的列表页url: http://www.netbian.com/1920x1080/index.htm
    2、获取数据
    网页源代码/ response.text 网页文本数据
    3、解析数据
    css xpath bs4 re 壁纸详情页url:/desk/23397.htm 2.壁纸标题
    4、保存数据
    保存图片是二进制数据
     
    观众姥爷:就这就这?代码呢?代码都不放你几个意思?
     
     
    别慌,来了来了
     
    三、代码展示
     
    我就不一 一拆解了,注释加上第三步,相信聪明的你可以理解,实在不行最后我放视频讲解吧。
     
    import requests # 请求模块 第三方模块 pip install requests
    import parsel # 数据解析模块 第三方模块 pip install parsel
    import time # 时间模块 内置模块
    
    time_1 = time.time()
    # 要什么用模块 首先要知道模块有什么用
    for page in range(2, 12):
        print(f'====================正在爬取第{page}页的数据内容====================')
        url = f'http://www.netbian.com/1920x1080/index_{page}.htm'
        # 请求头: 把python代码伪装成浏览器对服务器发送请求
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36'
        }
        response = requests.get(url=url, headers=headers)
        # 出现乱码怎么办? 需要转码
        # html_data = response.content.decode('gbk')
        response.encoding = response.apparent_encoding # 自动转码
        # 获取源代码/获取网页文本数据 response.text
        # print(response.text)
        # 解析数据
        selector = parsel.Selector(response.text)
        # CSS选择器 就是根据网页标签内容提取数据
        # 第一次提取 提取所有的li标签内容
        lis = selector.css('.list li')
        for li in lis:
            # http://www.netbian.com/desk/23397.htm
            title = li.css('b::text').get()
            if title:
                href = 'http://www.netbian.com' + li.css('a::attr(href)').get()
                response_1 = requests.get(url=href, headers=headers)
                selector_1 = parsel.Selector(response_1.text)
                img_url = selector_1.css('.pic img::attr(src)').get()
    
                img_content = requests.get(url=img_url, headers=headers).content
                with open('img\\' + title + '.jpg', mode='wb') as f:
                    f.write(img_content)
                    print('正在保存: ', title)
    
    
    
    time_2 = time.time()
    use_time = int(time_2) - int(time_1)
    print(f'总计耗时{use_time}秒')

    我还给大家准备了这些资料,直接在这里免费领。

    # 一群:872937351 (群满了的话加二群)
    # 二群:924040232
    # python学习路线汇总
    # 精品Python学习书籍100本
    # Python入门视频合集
    # Python实战案例
    # Python面试题
    # Python相关软件工具/pycharm永久激活
    大家可以自己运行试试,记得三连哇~
     
  • 相关阅读:
    将内容重定向到剪切板(clip.exe)
    加速数组操作(Array)
    错误信息输出,重定向到文件
    格式化数字字符串
    PowerShell常用的.Net 、COM对象(New-Object、Assembly)、加载程序集
    计算文件夹大小、拷贝文件显示进度
    草稿-Hyper-V
    右下角显示提示窗口(New-Object,COM)
    《TCP/IP详解卷一:协议》数据链路层(一)
    tcpdump抓包命令
  • 原文地址:https://www.cnblogs.com/hahaa/p/15700074.html
Copyright © 2011-2022 走看看