- 第三方模块的下载与使用
- 网络爬虫模块之requests模块
- 网络爬虫实战之爬取链家二手房数据
- 自动化办公领域之openpyxl模块
今日内容详细
作业讲解
| """ |
| 网络爬虫没有我们现在接触的那么简单 |
| 有时候页面数据无法直接拷贝获取 |
| 有时候页面还存在防爬机制 弄得不好ip会被短暂拉黑 |
| """ |
| 1.直接拷贝页面数据到本地文件 |
| 2.读取文件内容当做字符串处理 |
| 3.编写正则筛选内容 |
| import re |
| |
| |
| with open(r'redbull.html', 'r', encoding='utf8') as f: |
| |
| data = f.read() |
| |
| """ |
| 思路1: |
| 一次性获取每个公司全部的数据 |
| 分部分挨个获取最后统一整合 |
| """ |
| |
| |
| |
| comp_title_list = re.findall('<h2>(.*?)</h2>', data) |
| |
| comp_address_list = re.findall("<p class='mapIco'>(.*?)</p>", data) |
| |
| comp_email_list = re.findall("<p class='mailIco'>(.*?)</p>", data) |
| |
| comp_phone_list = re.findall("<p class='telIco'>(.*?)</p>", data) |
| |
| res = zip(comp_title_list, comp_address_list, comp_email_list, comp_phone_list) |
| |
| with open(r'comp_info.txt', 'w', encoding='utf8') as f: |
| for data_tuple in res: |
| print( |
| """ |
| 公司名称:%s |
| 公司地址:%s |
| 公司邮编:%s |
| 公司电话:%s |
| """ % data_tuple) |
| f.write( """ |
| 公司名称:%s |
| 公司地址:%s |
| 公司邮编:%s |
| 公司电话:%s\n |
| """ % data_tuple) |
第三方模块的下载与使用
| 第三方模块:别人写的模块 一般情况下功能都特别强大 |
| |
| 我们如果想使用第三方模块 第一次必须先下载后面才可以反复使用(等同于内置模块) |
| |
| 下载第三方模块的方式 |
| 1.pip工具 |
| 注意每个解释器都有pip工具 如果我们的电脑上有多个版本的解释器那么我们在使用pip的时候一定要注意到底用的是哪一个 否则极其任意出现使用的是A版本解释器然后用B版本的pip下载模块 |
| 为了避免pip冲突 我们在使用的时候可以添加对应的版本号 |
| python27 pip2.7 |
| python36 pip3.6 |
| python38 pip3.8 |
| 下载第三方模块的句式 |
| pip install 模块名 |
| 下载第三方模块临时切换仓库 |
| pip install 模块名 -i 仓库地址 |
| 下载第三方模块指定版本(不指定默认是最新版) |
| pip install 模块名==版本号 -i 仓库地址 |
| 2.pycharm提供快捷方式 |
| 群内截图 |
| |
| """ |
| 下载第三方模块可能会出现的问题 |
| 1.报错并有警告信息 |
| WARNING: You are using pip version 20.2.1; |
| 原因在于pip版本过低 只需要拷贝后面的命令执行更新操作即可 |
| d:\python38\python.exe -m pip install --upgrade pip |
| 更新完成后再次执行下载第三方模块的命令即可 |
| 2.报错并含有Timeout关键字 |
| 说明当前计算机网络不稳定 只需要换网或者重新执行几次即可 |
| 3.报错并没有关键字 |
| 面向百度搜索 |
| pip下载XXX报错:拷贝错误信息 |
| 通常都是需要用户提前准备好一些环境才可以顺利下载 |
| 4.下载速度很慢 |
| pip默认下载的仓库地址是国外的 python.org |
| 我们可以切换下载的地址 |
| pip install 模块名 -i 仓库地址 |
| pip的仓库地址有很多 百度查询即可 |
| 清华大学 :https://pypi.tuna.tsinghua.edu.cn/simple/ |
| 阿里云:http://mirrors.aliyun.com/pypi/simple/ |
| 中国科学技术大学 :http://pypi.mirrors.ustc.edu.cn/simple/ |
| 华中科技大学:http://pypi.hustunique.com/ |
| 豆瓣源:http://pypi.douban.com/simple/ |
| 腾讯源:http://mirrors.cloud.tencent.com/pypi/simple |
| 华为镜像源:https://repo.huaweicloud.com/repository/pypi/simple/ |
| """ |
网络爬虫模块之requests模块
| requests模块能够模拟浏览器发送网络请求 |
| |
| import requests |
| |
| |
| |
| |
| |
| |
| |
网络爬虫实战之爬取链家二手房数据
| import requests |
| import re |
| |
| res = requests.get('https://sh.lianjia.com/ershoufang/pudong/') |
| |
| data = res.text |
| |
| home_title_list = re.findall( |
| '<a class="" href=".*?" target="_blank" data-log_index=".*?" data-el="ershoufang" data-housecode=".*?" data-is_focus="" data-sl="">(.*?)</a>', |
| data) |
| |
| home_name_list = re.findall('<a href=".*?" target="_blank" data-log_index=".*?" data-el="region">(.*?) </a>', data) |
| |
| home_street_list = re.findall( |
| '<div class="positionInfo"><span class="positionIcon"></span><a href=".*?" target="_blank" data-log_index=".*?" data-el="region">.*? </a> - <a href=".*?" target="_blank">(.*?)</a> </div>', |
| data) |
| |
| home_info_list = re.findall('<div class="houseInfo"><span class="houseIcon"></span>(.*?)</div>', data) |
| |
| home_watch_list = re.findall('<div class="followInfo"><span class="starIcon"></span>(.*?)</div>', data) |
| |
| home_total_price_list = re.findall( |
| '<div class="totalPrice totalPrice2"><i> </i><span class="">(.*?)</span><i>万</i></div>', data) |
| |
| home_unit_price_list = re.findall( |
| '<div class="unitPrice" data-hid=".*?" data-rid=".*?" data-price=".*?"><span>(.*?)</span></div>', data) |
| |
| home_data = zip(home_title_list, home_name_list, home_street_list, home_info_list, home_watch_list, |
| home_total_price_list, home_unit_price_list) |
| with open(r'home_data.txt','w',encoding='utf8') as f: |
| for data in home_data: |
| print( |
| """ |
| 房屋标题:%s |
| 小区名称:%s |
| 街道名称:%s |
| 详细信息:%s |
| 关注程度:%s |
| 房屋总价:%s |
| 房屋单价:%s |
| """%data |
| ) |
| f.write(""" |
| 房屋标题:%s |
| 小区名称:%s |
| 街道名称:%s |
| 详细信息:%s |
| 关注程度:%s |
| 房屋总价:%s |
| 房屋单价:%s\n |
| """%data) |
自动化办公领域之openpyxl模块
| 1.excel文件的后缀名问题 |
| 03版本之前 |
| .xls |
| 03版本之后 |
| .xlsx |
| |
| 2.操作excel表格的第三方模块 |
| xlwt往表格中写入数据、wlrd从表格中读取数据 |
| 兼容所有版本的excel文件 |
| openpyxl最近几年比较火热的操作excel表格的模块 |
| 03版本之前的兼容性较差 |
| ps:还有很多操作excel表格的模块 甚至涵盖了上述的模块>>>:pandas |
| |
| 3.openpyxl操作 |
| '''学会看官方文档!!!''' |
| from openpyxl import Workbook |
| |
| wb = Workbook() |
| |
| wb1 = wb.create_sheet('学生名单') |
| wb2 = wb.create_sheet('舔狗名单') |
| wb3 = wb.create_sheet('海王名单') |
| |
| wb4 = wb.create_sheet('富婆名单', 0) |
| |
| wb4.title = '高富帅名单' |
| wb4.sheet_properties.tabColor = "1072BA" |
| |
| |
| |
| |
| |
| |
| wb4.append(['编号', '姓名', '年龄', '爱好']) |
| wb4.append([1, 'jason', 18, 'read']) |
| wb4.append([2, 'kevin', 28, 'music']) |
| wb4.append([3, 'tony', 58, 'play']) |
| wb4.append([4, 'oscar', 38, 'ball']) |
| wb4.append([5, 'jerry', 'ball']) |
| wb4.append([6, 'tom', 88,'ball','哈哈哈']) |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| wb.save(r'111.xlsx') |
| |
| """ |
| openpyxl主要用于数据的写入 至于后续的表单操作它并不是很擅长 如果想做需要更高级的模块pandas |
| |
| import pandas |
| |
| data_dict = { |
| "公司名称": comp_title_list, |
| "公司地址": comp_address_list, |
| "公司邮编": comp_email_list, |
| "公司电话": comp_phone_list |
| } |
| # 将字典转换成pandas里面的DataFrame数据结构 |
| df = pandas.DataFrame(data_dict) |
| # 直接保存成excel文件 |
| df.to_excel(r'pd_comp_info.xlsx') |
| |
| |
| excel软件正常可以打开操作的数据集在10万左右 一旦数据集过大 软件操作几乎无效 |
| 需要使用代码操作>>>:pandas模块 |
| """ |
hashlib加密模块
| 1.何为加密 |
| 将明文数据处理成密文数据 让人无法看懂 |
| 2.为什么加密 |
| 保证数据的安全 |
| 3.如何判断数据是否是加密的 |
| 一串没有规律的字符串(数字、字母、符号) |
| 4.密文的长短有何讲究 |
| 密文越长表示使用的加密算法(数据的处理过程)越复杂 |
| 5.常见的加密算法有哪些 |
| md5 base64 hmac sha系列 |
| 6.加密算法基本操作 |
| import hashlib |
| |
| md5 = hashlib.md5() |
| |
| md5.update(b'hello') |
| |
| res = md5.hexdigest() |
| print(res) |
加密补充说明
| 1.加密算法不同 内容如果相同 那么结果肯定相同 |
| md5.update(b'hello~world~python~666') |
| |
| md5.update(b'hello') |
| md5.update(b'~world') |
| md5.update(b'~python~666') |
| 2. 加密之后的结果是无法反解密的 |
| 只能从明文到密文正向推导 无法从密文到明文反向推导 |
| 常见的解密过程其实是提前猜测了很多结果 |
| 123 密文 |
| 321 密文 |
| 222 密文 |
| 3.加盐处理 |
| 在明文里面添加一些额外的干扰项 |
| |
| md5 = hashlib.md5() |
| |
| md5.update('公司设置的干扰项'.encode('utf8')) |
| md5.update(b'hello python') |
| |
| res = md5.hexdigest() |
| print(res) |
| |
| 4.动态加盐 |
| 干扰项是随机变化的 |
| eg:当前时间、用户名部分... |
| 5.加密实战操作 |
| 1.用户密码加密 |
| 2.文件安全性校验 |
| 3.文件内容一致性校验 |
| 4.大文件内容加密 |
| 截取部分内容加密即可 |
subporcess模块
| 模拟操作系统终端 执行命令并获取结果 |
| |
| import subprocess |
| |
| res = subprocess.Popen( |
| 'asdas', |
| shell=True, |
| stdin=subprocess.PIPE, |
| stdout=subprocess.PIPE, |
| ) |
| print('正确结果', res.stdout.read().decode('gbk')) |
| print('错误结果', res.stderr) |
logging日志模块
| 1.如何理解日志 |
| 简单的理解是记录行为举止的操作 |
| 2.日志的级别 |
| 五种级别 |
| 3.日志模块要求 |
| 代码无需掌握 但是要会cv修改 |
| |
| import logging |
| |
| |
| |
| |
| |
| file_handler = logging.FileHandler(filename='x1.log', mode='a', encoding='utf8',) |
| logging.basicConfig( |
| format='%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s', |
| datefmt='%Y-%m-%d %H:%M:%S %p', |
| handlers=[file_handler,], |
| level=logging.ERROR |
| ) |
| |
| logging.error('你好') |
日志的组成
| 1.产生日志 |
| 2.过滤日志 |
| 基本不用 因为在日志产生的阶段就可以控制自己想要的日志内容 |
| 3.输出日志 |
| 4.日志格式 |
| |
| import logging |
| |
| |
| logger = logging.getLogger('购物车记录') |
| |
| |
| hd1 = logging.FileHandler('a1.log', encoding='utf-8') |
| hd2 = logging.FileHandler('a2.log', encoding='utf-8') |
| hd3 = logging.StreamHandler() |
| |
| fm1 = logging.Formatter( |
| fmt='%(asctime)s - %(name)s - %(levelname)s -%(module)s: %(message)s', |
| datefmt='%Y-%m-%d %H:%M:%S %p', |
| ) |
| fm2 = logging.Formatter( |
| fmt='%(asctime)s - %(name)s: %(message)s', |
| datefmt='%Y-%m-%d', |
| ) |
| |
| logger.addHandler(hd1) |
| logger.addHandler(hd2) |
| logger.addHandler(hd3) |
| |
| hd1.setFormatter(fm1) |
| hd2.setFormatter(fm2) |
| hd3.setFormatter(fm1) |
| |
| logger.setLevel(10) |
| |
| logger.debug('写了半天 好累啊 好热啊') |
日志配置字典
| import logging |
| import logging.config |
| |
| standard_format = '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]' \ |
| '[%(levelname)s][%(message)s]' |
| simple_format = '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s' |
| |
| logfile_path = 'a3.log' |
| LOGGING_DIC = { |
| 'version': 1, |
| 'disable_existing_loggers': False, |
| 'formatters': { |
| 'standard': { |
| 'format': standard_format |
| }, |
| 'simple': { |
| 'format': simple_format |
| }, |
| }, |
| 'filters': {}, |
| 'handlers': { |
| |
| 'console': { |
| 'level': 'DEBUG', |
| 'class': 'logging.StreamHandler', |
| 'formatter': 'simple' |
| }, |
| |
| 'default': { |
| 'level': 'DEBUG', |
| 'class': 'logging.handlers.RotatingFileHandler', |
| 'formatter': 'standard', |
| 'filename': logfile_path, |
| 'maxBytes': 1024 * 1024 * 5, |
| 'backupCount': 5, |
| 'encoding': 'utf-8', |
| }, |
| }, |
| 'loggers': { |
| |
| '': { |
| 'handlers': ['default', 'console'], |
| 'level': 'DEBUG', |
| 'propagate': True, |
| }, |
| |
| |
| |
| |
| |
| }, |
| } |
| logging.config.dictConfig(LOGGING_DIC) |
| |
| |
| |
| |
| logger1 = logging.getLogger('红浪漫顾客消费记录') |
| logger1.debug('慢男 猛男 骚男') |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构