python模块之二

re模块

findall
# 在python中想要使用正则表达式re模块是其中选择之一

------------
import re
res = re.findall('d','adddsasfgfhdg')

 # 通过正则表达式筛选出所有符号条件的数据
print(res)  # ['d', 'd', 'd', 'd']

findall(参数1,参数二)
# 参数一是给出的条件 
# 参数二是文本数据


------------
分组()
res = re.findall('acd','acdacdacdacd')
print(res) #['acd', 'acd', 'acd', 'acd']
res = re.findall('a(c)d','acdacdacdacd')
print(res)  # ['c', 'c', 'c', 'c']

# 分组优先级展示

res = re.findall('a(?:c)d','acdacdacdacd')
print(res)  # ['acd', 'acd', 'acd', 'acd']

# (?:)取消分组


finditer
import re
res = re.finditer('d','adddsasfgfhdg')
print(res)
#<callable_iterator object at 0x000001BB03D9A278>
print(res.__next__())
# <_sre.SRE_Match object; span=(1, 2), match='d'>
print(res.__next__())
# <_sre.SRE_Match object; span=(2, 3), match='d'>

#返回的是一个迭代器 调用一次可以返回一个结果 目的是为了节省内存资源
import re
res = re.search('d','adddsasfgfhdg')
print(res)  # <_sre.SRE_Match object; span=(1, 2), match='d'>
print(res.group()) # d

# 筛选机制是只要有一个满足条件就不再继续往后面找数据
------------
res = re.search('l','adddsasfgfhdg')
print(res)   # None

#返回一个包含匹配信息的对象

#通过调用group()方法得到匹配的字符串,如果字符串没有匹配,则返回None。

------------
分组()
res = re.search('a(c)d','acdacdacdacd')
print(res.group())  # acd
# 加分组是没有改变的
res = re.search('a(c)d','acdacdacdacd')
print(res.group(0))  # acd
# 通过索引取值方式 0和没分组结果一样
res = re.search('a(c)d','acdacdacdacd')
print(res.group(1))  # c
# 索引1就是括号里的值 如想取d值 则需给d加括号 直接索引2就可以


------------
起别名 (?P<s>a) 
# 括号内填写要起的名字s  括号后面是被起名字的数据a
res = re.search('(?P<s>a)','adddsasfgfhdg')
print(res.group('s')) # a
# 也可以用索引取值
res = re.search('a(?P<s>d)d','adddsasfgfhdg')
print(res.group(1)) # d


match
import re
res = re.match('d','adddsasfgfhdg')
print(res) # None
------------
res = re.match('a','adddsasfgfhdg')
print(res) # <_sre.SRE_Match object; span=(0, 1), match='a'>
print(res.group()) # a

# 只返回文本的起始位置的数据 要是没有就返回None
compile
import re
res = re.compile('d')
print(re.findall(res,'adddsasfgfhdg')) #['d', 'd', 'd', 'd']
print(re.findall(res,'aaaaaaadaaaa')) #['d']

# compile() 能够提前准备好正则条件 之后可以反复使用 减少代码冗余

第三方模块

下载方法
1.第三方模块必须先下载才可以导入使用
2.python下载第三方模块需要借助于pip工具
3.下载命令
pip3.8 install 模块名

image

下载源地址
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/
	pip3.8 install 模块名 -i 源地址

# 模块也有版本
	pip3.8 install 模块名==版本号
	pip3.8 install django==1.11.11
		
pycharm提供第三方模块下载快捷方式

image

下载报错
1.pip工具版本过低 直接拷贝提示信息里面的更新命令即可
  python38 -m pip install --upgrade pip
  
2.网络波动 关键字是Read timed out
  只需要重新下载几次即可 或者切换一个网络稳定一点的

3.有些模块在下载使用之前需要提前配置指定的环境
  结合具体情况 百度搜索

eg:
image
eg:
image

python解释器换源
1.修改源码换源

找到python安装目录下的:Libsite-packagespipmodelsindex.py文件,将PYPI的值改为你所需要的镜像源即可,例如改为豆瓣镜像源:
#PyPI = Index('https://pypi.python.org/')  
PyPI = Index('https://pypi.douban.com/')

------------
2.修改配置文件永久换源

1、文件管理器文件路径地址栏敲:%APPDATA% 回车,快速进入 C:Users电脑用户AppDataRoaming 文件夹中
2、新建 pip 文件夹并在文件夹中新建 pip.ini 配置文件
3、新增 pip.ini 配置文件内容

配置文件内容
[global]
index-url = http://pypi.douban.com/simple
[install]
use-mirrors =true
mirrors =http://pypi.douban.com/simple/
trusted-host =pypi.douban.com

eg:
image

eg:
image

eg:
image

openpyxl模块

版本介绍
# 主要用于操作excel表格 也是pandas底层操作表格的模块
在python中能够操作excel表格的模块有很多
	openpyxl属于近几年比较流行的模块
    	openpyxl针对03版本之前的excel文件兼容性不好
	xlwt、xlrd也可以操作excel表格
    	兼容所有版本的excel文件 但是使用方式没有openpyxl简单

1.excel版本问题
	03版本之前 excel文件的后缀名 .xls
 	03版本之后 excel文件的后缀名 .xlsx
    	如果是苹果电脑excel文件的后缀 .csv

2.下载模块
	pip  install openpyxl

实操
# 如果创建的文件在打开状态,再次运行代码的话会出现报错 :拒绝访问

from openpyxl import Workbook
# 创建excel文件
web = Workbook()
res = web.create_sheet('收入',0)
# 后面一个参数0 是把收入挪到第一位
res1 = web.create_sheet('支出')
res2 = web.create_sheet('购房基金')

# 二次修改
res.title = '攒钱买房'
# 写入的三种方式
res['A5'] = '90000'
res['A6'] = '100'

res1.cell(row=3,column=3,value='999')

# 批量写入
res2.append(['一月工资','二月工资','三月工资'])
res2.append([12233,22333,77788])
# append()方法括号内要传容器类参数

# 求和
res2['A3'] = '=sum(A2:C2)'

# 保存文件
web.save(r'a.xlsx')
# save()方法要放在所有的数据修改完成以后再用 save后面的代码不能运行

image

网络爬虫简介

爬虫的本质

 # 模拟计算机浏览器朝目标网址发送请求回去数据并筛选
  
 # 只要是浏览器可以访问到的数据网络爬虫理论上都可以
 
.上网的本质是基于互联网访问别人计算机上面的资源(有些计算机存在的意义就是让别人访问,这种类型的计算机我们也称之为服务器)

爬虫实操
import requests
import re

# 朝目标地址发送网络请求获取响应数据(相当于在浏览器地址栏中输入网址并回车)
res = requests.get('http://www.redbull.com.cn/about/branch')
print(res.content)  # 获取bytes类型的数据
print(res.text)  # 获取解码之后的数据
# 为了避免每次执行程序都要发送网络请求  也可以提前保存页面数据到文件
with open(r'hn.html', 'wb') as f:
    f.write(res.content)

with open(r'hn.html', 'r', encoding='utf8')as f:
    data = f.read()
name_list = re.findall('<h2>(.*?)</h2>', data)
addr_list = re.findall("<p class='mapIco'>(.*?)</p>", data)
email_liat = re.findall("<p class='mailIco'>(.*?)</p>", data)
phone_list = re.findall("</p>lass='telIco'>(.*?)</p>", data)

res = zip(name_list,addr_list,email_liat,phone_list)

for i in res:
    print(
        '''
        公司名称:%s,
        公司地址:%s,
        公司邮编:%s,
        公司电话:%s
        
        '''%i
    )

image

posted @   Hsummer  阅读(45)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示