首页 > 编程语言 >Python基础21

Python基础21

时间:2022-10-26 17:48:26浏览次数:49  
标签:21 Python 基础 excel list 模块 pip home data

今日内容概要

  • 第三方模块的下载与使用
  • 网络爬虫模块值requests模块
  • 网络爬虫实战之爬取链家二手房数据
  • 自动化办公领域之openpyxl模块

今日内容详细

第三方模块的下载与使用

第三方模块就是别人写的模块 一般情况下功能都特别强大
如果想使用第三方模块 第一次必须先下载之后才可以反复使用 等同于内置模块

1.pip工具下载
	每个计时器都有pip工具 如果电脑上有多个版本的解释器 那么在使用pip工具下载的时候一定要注意到底使用的是哪一个 否则机器容易出现使用的是A版本解释器 然后用的是B版本的pip工具下载的模块 
	为了避免pip冲突 在使用的时候可以添加对应的版本号
	python27   pip2.7
	python36   pip3.6
	python38	pip3.8	
下载第三方模块的句式
	pip install 模块名
下载第三方模块时临时切换仓库
	pip install 模块名 -i 仓库地址
下载第三方模块时指定版本(不指定默认下载最新版)
	pip install 模块名==版本号
    
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 仓库地址
清华大学 :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

res = requests.get()  
# 括号内填写网址 朝指定网址发送请求获取页面数据(等价于浏览器地址栏输入网址回车访问)
res.content # 获取bytes类型的网页数据(二进制)
res.encoding = 'utf8'  # 指定编码
res.text  # 获取字符串类型的网页数据(默认按照utf8)

网络爬虫实战之爬取链家二手房数据

import requests
import re

res = requests.get('https://sh.lianjia.com/ershoufang/pudong/')
# print(res.text)
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)
# print(home_title_list)
home_name_list = re.findall('<a href=".*?" target="_blank" data-log_index=".*?" data-el="region">(.*?) </a>', data)
# print(home_name_list)
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)
# print(home_street_list)
home_info_list = re.findall('<div class="houseInfo"><span class="houseIcon"></span>(.*?)</div>', data)
# print(home_info_list)
home_watch_list = re.findall('<div class="followInfo"><span class="starIcon"></span>(.*?)</div>', data)
# print(home_watch_list)
home_total_price_list = re.findall(
    '<div class="totalPrice totalPrice2"><i> </i><span class="">(.*?)</span><i>万</i></div>', data)
# print(home_total_price_list)
home_unit_price_list = re.findall(
    '<div class="unitPrice" data-hid=".*?" data-rid=".*?" data-price=".*?"><span>(.*?)</span></div>', data)
# print(home_unit_price_list)
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表格的第三方模块
	zlwt往表格中写入数据 xlrd 从表格中读取数据
	兼容所有版本的excel文件
	openpyxl最近几年比较火热的操作excel表格的模块
	不兼容03版本之前的excel文件 兼容性狡差
ps还有很多操作excel表格的模块 甚至涵盖了上述的模块>>>:pandas

openpyxl操作

'''学会看官方文档
百度模块名 纯英文的就是官网
'''
from openpyxl import Workbook

wb = Workbook()   # 创建一个excel文件
wb1 = wb.create_sheet('高富帅名单')  
# 在excel文件内创建工作簿 括号内填工作簿名
wb2 = wb.create_sheet('富婆名单', 0)
# 还可以修改默认工作簿位置
wb2.title = '富婆通讯录'  # 二次修改工作簿名称
wb2.sheet_properties.tabColor = "1072BA"  # 修改工作簿颜色
# 添加数据方式1
wb1['A1'] = 'jason'
# 添加数据方式2
wb1.cell(row=2, column=1, value='18')
# 添加数据方式3
wb1.append(['编号', '姓名', '年龄', '爱好'])
wb1.append([1, 'jason', '18', 'read'])

# 填写数学公式
wb2.cell(row=1, column=1, value=111)
wb2.cell(row=2, column=1, value=222)
wb2.cell(row=3, column=1, value=333)
wb2.cell(row=4, column=1, value=444)
wb2['A5'] = '=sum(A1:A4)'
wb2.cell(row=8, column=3, value='=sum(A1:A4)')

# 保存excel文件
wb.save(r'a.xlsx')

'''
	openpyxl主要用于数据的写入 至于后续的表单操作它并不是很擅长 如果想做需要更高级的模块pandas
	excel软件正常可以打开操作的数据集在10万左右 一旦数据集过大 软件操作几乎无效 需要使用代码操作>>>:pandas模块
'''

标签:21,Python,基础,excel,list,模块,pip,home,data
From: https://www.cnblogs.com/lzjjjj/p/16829268.html

相关文章

  • 转 python 自动监控表空间,并自动添加数据文件
    侯志清-江西南昌 python自动监控表空间,并自动添加数据文件#!/usr/bin/pythonimportosimporttimeimportlinecache#定义记录日志文件defrlog(log)......
  • python进程
    1、概念进程是一个执行中的程序,资源分配的最小单位。每个进程都拥有自己的地址空间、内存、数据栈以及其他用于跟踪执行的辅助数据。在单核CPU系统中的多进程,内存中可以有......
  • 摒弃“短板”——数据中心基础设施运维管理建议书
    数据中心是数字基础设施的重要组成部分,同时也是一整套复杂的设施。它不仅仅包括计算机系统和其它与之配套的设备(例如通信和存储系统),还包含冗余的数据通信连接、环境控制系统......
  • tekton基础
    task和taskrun示例[root@master02-tekton-basics]#cat01-task-hello.yamlapiVersion:tekton.dev/v1beta1kind:Taskmetadata:name:hellospec:steps:......
  • 每天三个Linux基础命令-008
    满招损,谦受益。内容不一定精彩,知识不一定你可以用得上,观点不一定准确,还望观者觉得有缺陷欢迎指正,觉得还好的感谢鼓励点赞。8.1man帮助命令主要用法:man后接需要查询的命......
  • python切片数组越界?
    1.在对list进行切片时,如x[9:12],若len(x)=10,只会返回x[9],而不会像其他语言直接数组越界错误。x=[iforiinrange(10)]print(x)foriinrange(0,10,3):print(x[i:i+3]......
  • 图像处理:opencv-python给图像加文字
    1.起因计算机视觉中常常会需要在图片上加文字,这样可以增强图像数据的可读性。2.工具opencv-python3.方法importmatplotlib.pyplotaspltfromPILimportImag......
  • python3 使用位图排序
    代码frombitmapimportBitMapa=[1,5,3,4,7,8,15,6,9]print(a)bm=BitMap(max(a))#print(dir(bm))print(bm.tostring())foriina:bm.set(i)print(bm......
  • 哈夫曼树及python实现
    3.1基本概念路径和路径长度:树中一个结点到另一个结点之间的分支构成这两个结点之间的路径;路径上的分枝数目称作路径长度,它等于路径上的结点数减1.结点的权和带权路径长度......
  • python的优雅退出
    #!/usr/bin/envpython#-*-coding:utf-8-*-importosimportsignalimportsysfromconcurrentimportfuturesimportloggingfromloguruimportloggerimpor......