python小练习
以一些脚本实战练习,逐渐熟悉python的各种模块和语法
~VEGETABLEBIRD~
这个作者很懒,什么都没留下…
展开
-
Python练习014
题目:爬取诗词名句网的全本三国演义。import requestsfrom bs4 import BeautifulSoupimport osif not os.path.exists("三国演义"): os.mkdir("三国演义")url = "https://www.shicimingju.com/book/sanguoyanyi.html"headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) App原创 2021-01-24 14:11:31 · 160 阅读 · 0 评论 -
Python练习013
题目:使用正则表达式、BeautifulSoup、Xpath三种方法爬取糗图百科的热门图片。正则表达式:import requestsimport reimport osif not os.path.exists("qiutu"): os.mkdir("qiutu")url = "https://www.qiushibaike.com/imgrank/"headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64)原创 2021-01-23 12:22:44 · 129 阅读 · 0 评论 -
Python练习012
题目:爬取药监总局化妆品公司信息import requestsurl = 'http://scxk.nmpa.gov.cn:81/xk/itownet/portalAction.do?method=getXkzsList'headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 \ (KHTML, like Gecko) Chrome/87.0.4280原创 2020-12-19 16:47:00 · 110 阅读 · 1 评论 -
Python练习011
题目:爬取肯德基指定地点的餐厅具体信息代码:import requestsimport argparseheaders = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64)\ AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280\ .88 Safari/537.36 Edg/87.0.664.60'}url = 'http://ww原创 2020-12-18 22:28:51 · 133 阅读 · 1 评论 -
Python练习010
题目:利用ajax请求,爬取豆瓣电影喜剧排行榜的前100名电影代码:import requestsimport argparseheaders = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64)\ AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280\ .88 Safari/537.36 Edg/87.0.664.60'}url原创 2020-12-18 22:22:06 · 144 阅读 · 1 评论 -
Python练习009
题目:利用ajax请求破解百度翻译代码:import requestsimport argparseheaders = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64)\ AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280\ .88 Safari/537.36 Edg/87.0.664.60'}url = 'https://fan原创 2020-12-18 22:14:46 · 96 阅读 · 1 评论 -
Python练习008
题目:利用pgzero模块,绘制一个不断移动的圆。(注意要先安装pgzero模块)import pgzrunW = 800 H = 600x = W/2y = H/2speed_x = 3speed_y = 4r =30def draw(): screen.fill('white') screen.draw.filled_circle((x, y), r, 'red') def update(): global x, y, speed_x, speed原创 2020-12-08 22:06:49 · 97 阅读 · 0 评论 -
Python练习007
题目:实现图片转字符串,并通过命令行输入参数图片:代码:from PIL import Imageimport argparseimport string# 创建句柄parser = argparse.ArgumentParser()# 添加参数parser.add_argument('--input','-i')parser.add_argument('--output', '-o', type = str, default = 'result.txt')parser.add_原创 2020-12-07 22:26:35 · 90 阅读 · 0 评论 -
Python练习006
题目:生成邀请码。注意邀请码由大小写字母以及数字随机组成,每个邀请码的长度任意,最后写入到codes.txt文件中。代码:import string, randomdef generateCode(num, length): # 等同于'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' code = string.digits + string.ascii_letters with open('codes原创 2020-12-04 21:58:08 · 102 阅读 · 0 评论 -
Python练习005
题目:选择一张照片,在照片某一位置添加一个数字。如:代码:from PIL import Image, ImageDraw, ImageFontdef add_num(img): draw_num = ImageDraw.Draw(img) # 设置字体 font =ImageFont.truetype(r'C:/windows/fonts/arial.ttf',size = 100) width, height = img.size draw_num.t原创 2020-12-04 21:54:35 · 66 阅读 · 0 评论 -
Python练习004
题目:编写一个程序,用户输入关键字,查找当前文件夹内(如果当前文件夹内包含文件夹,则进入文件夹继续搜索)所有含有该关键字的文本文件(.txt后缀),要求显示该文件所在的位置以及关键字在文件中的具体位置(第几行第几个字符)代码:import osdef pos_print(cnt, result, file): file_path = os.getcwd() + os.sep + file print("{}当前文件下含有{}有:".format(file_path, keywords)原创 2020-12-01 22:10:31 · 163 阅读 · 0 评论 -
Python练习003
题目:编写一个程序,用户输入开始搜索的路径,查找该路径下(包含子文件夹内)所有的视频格式文件(要求查找mp4,mp3,txt的格式即可),并把创建一个文件(list.txt)存放所有找到的文件的路径。代码:import ostarget = ['.mp4', '.mp3', '.txt']def search(start_dir): os.chdir(start_dir) print(os.listdir(os.curdir)) for each_file in os.lis原创 2020-12-01 19:22:42 · 144 阅读 · 0 评论 -
Python练习002
题目:编写一个程序,用户输入文件名以及开始搜索的路径,搜索该文件是否存在。如遇到文件夹,则进入文件夹继续搜索。代码:path = []all_files = []def search(start_path, target_file): os.chdir(start_path) for each_file in os.listdir(os.curdir): if each_file == target_file: path.append(os.g原创 2020-12-01 19:05:46 · 275 阅读 · 0 评论 -
Python练习001
题目:编写一个程序,统计当前目录下每个文件类型的文件数。代码:import osall_files = os.listdir(os.curdir)_dict={}for each_file in all_files: if os.path.isdir(each_file): _dict.setdefault('文件夹', 0) _dict['文件夹'] += 1 else: suffix = os.path.splitext(原创 2020-12-01 16:54:24 · 61 阅读 · 0 评论 -
Python练习000
题目:编写一个程序,计算当前文件夹下所有文件的大小。代码:import os# 获取当前目录下的所有文件名all_files = os.listdir(os.curdir)_dict = {}for each_file in all_files: # 如果这是一个文件夹 if os.path.isdir(each_file): size = os.path.getsize(each_file) _dict.setdefault(each_file,原创 2020-12-01 16:53:01 · 77 阅读 · 0 评论