• 你能猜出这是什么代码吗


    import datetime                            # 导入日期时间模块
    import time                            # 导入时间模块
    import requests                            # 导入网络请求模块
    from bs4 import BeautifulSoup                    # 导入解析HTML代码的模块


    def implement():
        # 头部信息
        headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                                 'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'}
        response = requests.get('http://www.waduanzi.com/', headers=headers)
        response.encoding = 'utf-8'                    # 设置编码方式
        html = BeautifulSoup(response.text, "html.parser")        # 解析html代码
        info_all = html.find_all('div', class_='item-detail')    # 获取所有段子信息
        index = 1                            # 文字换行标记
        for i in info_all:                        # 遍历段子信息
            print('\n《', i.h2.a.text, '》\n')                # 标题内容
            # 获取段子文字信息,并标记20字(包括符号)左右换行
            for text in i.find('div', class_='item-content').text:
                index += 1                # 递增文字换行标记
                if index == 20:            # 达到标记要求
                    print(text, '\n')            # 执行换行
                    index = 0                # 初始化文字换行标记
                else:
                    print(text, end='')        # 去除默认的换行
            print('\n')

    def timing(hour, minute):
        while True:
            now = datetime.datetime.now()
            if now.hour == hour and now.minute == minute:
                implement()        # 启动执行的程序
                break            # 程序执行完成后自动跳出循环结束任务
            time.sleep(60)        # 每1分钟检测一次

    if __name__ == '__main__':
        timing(10,24)            # 设置启动时间,时间为24小时制,白天10:24,夜间为22:24

  • 相关阅读:
    量化系统交易者想要取得长远的成功需要具备什么条件呢?
    java毕业设计大学生社团管理系统Mybatis+系统+数据库+调试部署
    免费IP类api接口:含ip查询、ip应用场景查询、ip代理识别、IP行业查询...
    1、C语言(语法结构、数据变量、变量与常量)
    使用pytest和allure框架实现自动化测试报告优化
    rust 开发入门
    PyTorch(一)安装与环境配置
    通过多进程并发方式(fork)实现服务器(注意要回收子进程)
    三更Blog项目总结(p1~p40)
    算法进阶系列1 空间搜索 GeoHash 算法
  • 原文地址:https://blog.csdn.net/s13596191285/article/details/125514282