day 18 - 2 正则与 re 模块练习
1、爬虫的例子
#爬虫的例子(方法一)
import re
import urllib,request import urlopen def getPage(url):
response = urlopen(url)
return response.read().decode('utf-8') def parsePage(s):
ret = re.findall(
'<div class="item">.*?<div class="pic">.*?<em .*?>(?P<id>\d+).*?<span class="title">(?P<title>.*?)</span>'
'.*?<span class="rating_num" .*?>(?P<rating_num>.*?)</span>.*?<span>(?P<comment_num>.*?)评价</span>',s,re.S)
return ret def main(num):
url = 'https://movie.douban.com/top250?start=%s&filter=' % num
response_html = getPage(url)
ret = parsePage(response_html)
print(ret) count = 0
for i in range(10): # 10页
main(count)
count += 25 # url 从网页上把代码搞下来
# bytes decode ——> utf-8 网页内容就是我的待匹配字符串
# ret = re.findall(正则,带匹配的字符串) #ret是所有匹配到的内容组成的列表
#爬虫的例子(方法一)
import requests import re
import json def getPage(url): response=requests.get(url)
return response.text def parsePage(s): com=re.compile('<div class="item">.*?<div class="pic">.*?<em .*?>(?P<id>\d+).*?<span class="title">(?P<title>.*?)</span>'
'.*?<span class="rating_num" .*?>(?P<rating_num>.*?)</span>.*?<span>(?P<comment_num>.*?)评价</span>',re.S) ret=com.finditer(s)
for i in ret:
yield {
"id":i.group("id"),
"title":i.group("title"),
"rating_num":i.group("rating_num"),
"comment_num":i.group("comment_num"),
} def main(num): url='https://movie.douban.com/top250?start=%s&filter='%num
response_html=getPage(url)
ret=parsePage(response_html)
print(ret)
f=open("move_info7","a",encoding="utf8") for obj in ret:
print(obj)
data=json.dumps(obj,ensure_ascii=False)
f.write(data+"\n") if __name__ == '__main__':
count=0
for i in range(10):
main(count)
count+=25
1、计算器
#计算下面式子
a = '1 - 2 * ( ( 6 0 -3 0 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )' import re def format(new_equation):
new_equation = new_equation.replace('+-','-')
new_equation = new_equation.replace('--', '+')
return new_equation def cal(val_son):
'''加减乘除的计算'''
#print(new_val)
if '/' in val_son:
a,b = val_son.split('/')
return str(float(a)/float(b))
elif '*' in val_son:
a,b = val_son.split('*')
return str(float(a)*float(b)) def no_brackets(val):
'''去括号'''
new_val = val.strip('()')
while True:
ret = re.search('\d+\.?\d*[*/]-?\d+\.?\d*',new_val) #匹配第一个乘除
if ret: #说明 表达式中海油乘除法
val_son = ret.group() #子表达式
ret = cal(val_son)
new_val = new_val.replace(val_son,ret)
new_val = format(new_val)
else:
ret = re.findall('-?\d+\.?\d*',new_val)
sum =
for i in ret:
sum += float(i)
return str(sum) def func(new_equation):
while True:
val = re.search('\([^()]+\)',new_equation)
if val:
val = val.group()
ret = no_brackets(val)
new_equation = new_equation.replace(val,ret)
new_equation = format(new_equation)
else:
return no_brackets(new_equation) a = input("请输入要计算的式子>>>")
new_equation = a.replace(' ','')
print(func(new_equation))
day 18 - 2 正则与 re 模块练习的更多相关文章
- day 18 - 1 正则与 re 模块
正则表达式 官方定义:正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个 “规则字符串”,这个 “规则字符串” 用来表达对字符串的一种过滤逻辑. 我 ...
- day19 正则,re模块
http://www.cnblogs.com/Eva-J/articles/7228075.html 所有常用模块的用法 正则的规则: 在一个字符组里面枚举合法的所有字符,字符组里面的任意一个字符和 ...
- 正则,re模块
一.正则表达式(精准匹配) 匹配字符串内容的一种规则 二.字符组 在同一个位置可能出现的各种字符组成了一个字符组,在正则表达式中用[]表示 常见字符组格式如下:[0123456789],[0-9],[ ...
- python正则以及collections模块
正则 一.认识模块 什么是模块:一个模块就是一个包含了python定义和声明的文件,文件名就是加上.py的后缀,但其实import加载的模块分为四个通用类别 : 1.使用python编写的代码(.p ...
- Python 正则处理_re模块
正则表达式 动机 文本处理成为计算机常见工作之一 对文本内容搜索,定位,提取是逻辑比较复杂的工作 为了快速方便的解决上述问题,产生了正则表达式技术 定义 文本的高级匹配模式, 提供搜索, 替换, 本质 ...
- Learning-Python【18】:Python常用模块(1)—— time、datetime、randrom
time 模块:与时间相关的功能的模块 在 Python 中,时间分为三种: 1.时间戳:是一个时间的表示,根据不同的语言,可以是整数或浮点数,是从1970年1月1日0时0分0秒到现在经历的秒数 2. ...
- day23 正则,re模块
一. 简谈正则表达式 元字符 . 除了换行符外任意字符. \w 数字.字母.下划线 \s 空白符 \b 单词的末尾 \d 数字 \n 匹配换行符 \t 匹配制表符 \W 除了数字. 字母 下划线 \D ...
- python 基础之第十二天(re正则,socket模块)
In [14]: 'hello-wold.tar.gz'.split('.') Out[14]: ['hello-wold', 'tar', 'gz'] In [15]: import re In [ ...
- Python正则、re模块
正则的概念 findall match search 方法 元字符的用法和作用 正则表达式概念 正则表达式是对字符串操作的一种逻辑公式,就是对字符串的一种过滤 可以判断是 ...
随机推荐
- Linux新手随手笔记1.1
ifconfig 查询网卡信息 分别是网卡名称,物理IP地址,MAC地址,RX收到数据包大小,TX发送数据包大小 # uname # uname -a 查看内核版本号 # hostname 查看主 ...
- 11 Django RESTful framework 实现缓存
01-安装 pip install drf-extensions 02-导入 from rest_framework_extensions.cache.mixins import CacheRespo ...
- PVLAN 简介
PVLAN(Private VLAN),即私有 VLAN.采⽤两层 VLAN 隔离技术,上层VLAN 全局可见,下层VLAN 相互隔离.PVLAN 通常用于企业内部网,用来防止连接到某些接⼝或接口组的 ...
- C# NetStream
标题:NetStream 关注点:Read.Write 正文: int size = Read(buf, 0, buf.length); 这里一次会读入length个字节,如果小于这个数量,后面的就是 ...
- kettle变量(var变量)
设置变量/set varibale 1.定义变量(子转换): 原始数据 设置获取变量:点击获取字段,自动获取变量名称和字段名称 引用变量: 输出: kettle.properties 文件存储在.ke ...
- 你不知道的CSS
white-space: pre-line;//P标签自动换行
- Singleton多种实现方式的在多线程情况下的优缺点
一.饿汉式 缺点:不能懒加载 // 不能懒加载 public class SingletonObject1 { private static final SingletonObject1 instan ...
- [ffmpeg] h.264解码所用的主要缓冲区介绍
在进行h264解码过程中,有两个最重要的结构体,分别为H264Picture.H264SliceContext. H264Picture H264Picture用于维护一帧图像以及与该图像相关的语法元 ...
- Spring MVC 使用介绍(十六)数据验证 (三)分组、自定义、跨参数、其他
一.概述 除了依赖注入.方法参数,Bean Validation 1.1定义的功能还包括: 1.分组验证 2.自定义验证规则 3.类级别验证 4.跨参数验证 5.组合多个验证注解 6.其他 二.分组验 ...
- Tornado实现多进程/多线程的HTTP服务
用tornado web服务的基本流程 实现处理请求的Handler,该类继承自tornado.web.RequestHandler,实现用于处理请求的对应方法如:get.post等.返回内容用sel ...