re模块openpyxl模块
创建开发目录规范代码
def make_file():
import os
center_path = os.path.dirname(__file__)
bin_path = os.path.join(center_path, 'bin')
if not os.path.exists(bin_path):
os.mkdir(bin_path)
star_path = os.path.join(bin_path, 'star.py')
if not os.path.isfile(star_path):
with open(star_path, 'w', encoding='utf8') as f:
pass
cnof_path = os.path.join(center_path, 'conf')
if not os.path.exists(cnof_path):
os.mkdir(cnof_path)
settings_path = os.path.join(cnof_path, 'settings.py')
if not os.path.isfile(settings_path):
with open(settings_path, 'w', encoding='utf8')as f:
pass
core_path = os.path.join(center_path, 'core')
if not os.path.exists(core_path):
os.mkdir(core_path)
src_path = os.path.join(core_path, 'src.py')
if not os.path.isfile(src_path):
with open(src_path, 'w', encoding='utf8') as f:
pass
db_path = os.path.join(center_path, 'db')
if not os.path.exists(db_path):
os.mkdir(db_path)
user_path = os.path.join(db_path, 'userinfo.txt')
if not os.path.isfile(user_path):
with open(user_path, 'w', encoding='utf8') as f:
pass
lib_path = os.path.join(center_path, 'lib')
if not os.path.exists(lib_path):
os.mkdir(lib_path)
common_path = os.path.join(lib_path, 'common.py')
if not os.path.isfile(common_path):
with open(common_path, 'w', encoding='utf8') as f:
pass
log_path = os.path.join(center_path, 'log.log')
if not os.path.exists(log_path):
os.mkdir(log_path)
log_txt_path = os.path.join(log_path, 'log.txt')
if not os.path.isfile(log_txt_path):
with open(log_txt_path, 'w', encoding='utf8') as f:
pass
requirements_path = os.path.join(center_path, 'requirements.txt')
if not os.path.isfile(requirements_path):
with open(requirements_path, 'w', encoding='utf8') as f:
pass
make_file()
re模块
在python使用正则表达式,re模块是选择之一
1.re.findall()通过正则匹配所有复合的数据结果是列表['a', 'a']
import re
res = re.findall('a','jason tony kkavn') #括号内第一个参数是正则表达式,筛查的文本
for i in res:
print(i)
2.re.finditer()与上一致结果会处理成迭代对象<callable_iterator object at 0x105d92860>
import re
res = re.finditer('a','jason tony kkaaavn')
#匹配到一个就停止
print(res)
3.re.seach()通过正则匹配到一个就停止
import re
res = re.search('a','jason tony kkaaavn')
#匹配到一个就停止
print(res)
print(res.group())
4.re.match()通过正则匹配第一个不是就停止只匹配第一个字符
import re
res = re.match('a','jason tony kkaaavn')
res1 = re.match('a','adsdsdsdsd')
print(res)
print(str(res1))
5.re.compile('a')括号内写正则表达式用变量可代替表达式其实莫得什么黑纹用
import re
obj =re.compile('a') #可接收正则表达式
res = re.findall(obj,'jason tony kkaaavn') #用变量代替正则
print(obj,res)
re模块其他说明
用括号可以分组优先展示括号内的数据,取消分组在括号内添加?:即可
import re
res =re.findall('a(?:b)c','abcabcabc')
print(res) # 匹配正则abc 展示的时候优先展示括号中的结果
import re
res =re.findall('a(b)c','abcabcabc')
print(res) # 匹配正则abc 展示的时候优先展示括号中的结果
res1 =re.search('a(b)c','abcabcabc')
print(res1.group(0)) #匹配的就是abc对正则没有影响结果就是abc
print(res1.group(1)) # 索引1就是优先展示括号内的数据称为分组1
起别名 :(?P< id > b) 括号内问号大写p加尖括号id 主要用在serrch和match
import re
res =re.search('a(?P<id>b)c','abcabcabc')
print(res.group()) # 0 是整个正则表达式
print(res.group(1)) # 索引1就是分组内的数据
print(res.group('id')) #起别名
网络爬虫简介
1.什么是互联网
讲全世界计算机连接到一起组成的网络
2.互联网发明的目的是什么
将接入互联网的计算机上面的数据彼此共享
3.上网的本质是什么
基于互联网访问别人计算机上的资源(有些计算机存在的意义就是让别人访问的,这种类型的计算机称为服务器)
4.网络爬虫的本质
模拟计算机浏览器朝目标网址发送请求回去数据并筛选
浏览器可以访问到的数据网络爬虫理论上都可以
5.获取红牛分公司的所有数据
http://www.redbull.com.cn/about/branch
爬虫模块requests
import requests
import re
# res = requests.get('http://www.redbull.com.cn/about/branch')
# print(res.text)
# with open(r'hn.htm','wb') as f:
# f.write(res.content)
with open(r'hn.htm','r',encoding='utf8') as f:
data = f.read()
res = re.findall('<h2>(.*?)</h2>',data)
# print(res)
res1 = re.findall("class='mapIco'>(.*?)</p>",data)
# print(res1)
res2 = re.findall("class='mailIco'>(.*?)</p",data)
# print(res2)
res3 = re.findall("class='telIco'>(.*?)</p>",data)
# print(res3)
hnlist = zip(res,res1,res2,res3)
print(hnlist)
# with open('hn.txt','w',encoding='utf8') as f:
# f.write(hnlist)
for i in hnlist:
print('''
红牛公司:%s
红牛地址:%s
公司邮箱:%s
公司号码:%s
}'''%i)
i = str(i)
with open('hn.txt','a',encoding='utf8') as f:
f.write(i)
f.write('\n')
with open('hn.txt', 'r', encoding='utf8') as f:
for i in f:
res, res1, res2, res3 = i.split(',')
res = res.strip('(')
res1 = res1.strip()
res2 = res2.strip()
res3 = res3.strip()
res3 = res3.strip(')')
res = res.strip("'")
res1 = res1.strip("'")
res2 = res2.strip("'")
res3 = res3.strip("'")
print('''
红牛公司:%s
红牛地址:%s
公司邮箱:%s
公司号码:%s
''' % (res, res1, res2, res3))
第三方模块
1.第三方模块必须先下载才可以导入使用
2.python下载第三方模块需要借助于pip工具
3.下载命令
pip3.8 install 模块名
"""
1.下载速度很慢
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 源地址
pycharm提供第三方模块下载快捷方式
也可以直接修改python解释器源文件(课下自行查阅)
2.下载报错
1.pip工具版本过低 直接拷贝提示信息里面的更新命令即可
python38 -m pip install --upgrade pip
2.网络波动 关键字是Read timed out
只需要重新下载几次即可 或者切换一个网络稳定一点的
3.有些模块在下载使用之前需要提前配置指定的环境
结合具体情况 百度搜索
3.模块也有版本
pip3.8 install 模块名版本号
pip3.8 install django1.11.11 指明下载版本
"""
下载第三方模块更改版本号
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.下载模块
pip3.8 install openpyxl
1.创建excel文件
from openpyxl import Workbook # 导入模块
wb = Workbook() # 创建excel文件
wb1 = wb.create_sheet('成绩表')
wb2 = wb.create_sheet('财务表')
wb3 = wb.create_sheet('校花表', 0)
wb1.title = '舔狗表' # 支持二次修改
wb1.sheet_properties.tabColor = "1072BA" # 修改工作簿颜色
wb.save(r'111.xlsx') # 保存文件
2.写入数据
# 第一种写入方式
wb1['A1'] = '叙利亚悍匪'
wb1['D2'] = '慢男'
# 第二种写入方式
wb1.cell(row=3, column=2, value='老六慢走')
# 第三种写入方式(批量写入)
wb1.append(['username','password','age','gender','hobby'])
wb1.append(['jason1',123,18,'male','read'])
wb1.append(['jason2',123,18,'male','read'])
wb1.append(['jason3',123,18,'male','read'])
wb1.append(['jason4',123,18,'male','read'])
wb1.append(['jason4',123,18,'male',None])
wb1.append([None,123,18,'male',''])
wb1['F11'] = '=sum(B5:B10)'
作业爬红牛写进表格
import requests
import re
from openpyxl import Workbook
res = requests.get('http://www.redbull.com.cn/about/branch')
print(res.text)
with open(r'hn.htm','wb') as f:
f.write(res.content)
with open(r'hn.htm','r',encoding='utf8') as f:
data = f.read()
res = re.findall('<h2>(.*?)</h2>',data)
# print(res)
res1 = re.findall("class='mapIco'>(.*?)</p>",data)
# print(res1)
res2 = re.findall("class='mailIco'>(.*?)</p",data)
# print(res2)
res3 = re.findall("class='telIco'>(.*?)</p>",data)
# print(res3)
hnlist = zip(res,res1,res2,res3)
print(hnlist)
# with open('hn.txt','w',encoding='utf8') as f:
# f.write(hnlist)
for i in hnlist:
print('''
红牛公司:%s
红牛地址:%s
公司邮箱:%s
公司号码:%s
}'''%i)
i = str(i)
with open('hn.txt','a',encoding='utf8') as f:
f.write(i)
f.write('\n')
with open('hn.txt', 'r', encoding='utf8') as f:
for i in f:
res, res1, res2, res3 = i.split(',')
res = res.strip('(')
res1 = res1.strip()
res2 = res2.strip()
res3 = res3.strip()
res3 = res3.strip(')')
res = res.strip("'")
res1 = res1.strip("'")
res2 = res2.strip("'")
res3 = res3.strip("'")
print('''
红牛公司:%s
红牛地址:%s
公司邮箱:%s
公司号码:%s
''' % (res, res1, res2, res3))
wb = Workbook()
wb1 = wb.create_sheet('玩偶姐姐', 0)
wb1.append(['红牛公司', '公司地址', '公司邮编', '公司电话'])
with open('hn.txt', 'r', encoding='utf8') as f:
for i in f:
res, res1, res2, res3 = i.split(',')
res = res.strip('(')
res1 = res1.strip()
res2 = res2.strip()
res3 = res3.strip()
res3 = res3.strip(')')
res = res.strip("'")
res1 = res1.strip("'")
res2 = res2.strip("'")
res3 = res3.strip("'")
wb1.append([res, res1, res2, res3])
wb.save(r'111.xlsx')