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 模块练习的更多相关文章

  1. day 18 - 1 正则与 re 模块

    正则表达式 官方定义:正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个 “规则字符串”,这个 “规则字符串” 用来表达对字符串的一种过滤逻辑. 我 ...

  2. day19 正则,re模块

    http://www.cnblogs.com/Eva-J/articles/7228075.html  所有常用模块的用法 正则的规则: 在一个字符组里面枚举合法的所有字符,字符组里面的任意一个字符和 ...

  3. 正则,re模块

    一.正则表达式(精准匹配) 匹配字符串内容的一种规则 二.字符组 在同一个位置可能出现的各种字符组成了一个字符组,在正则表达式中用[]表示 常见字符组格式如下:[0123456789],[0-9],[ ...

  4. python正则以及collections模块

    正则 一.认识模块  什么是模块:一个模块就是一个包含了python定义和声明的文件,文件名就是加上.py的后缀,但其实import加载的模块分为四个通用类别 : 1.使用python编写的代码(.p ...

  5. Python 正则处理_re模块

    正则表达式 动机 文本处理成为计算机常见工作之一 对文本内容搜索,定位,提取是逻辑比较复杂的工作 为了快速方便的解决上述问题,产生了正则表达式技术 定义 文本的高级匹配模式, 提供搜索, 替换, 本质 ...

  6. Learning-Python【18】:Python常用模块(1)—— time、datetime、randrom

    time 模块:与时间相关的功能的模块 在 Python 中,时间分为三种: 1.时间戳:是一个时间的表示,根据不同的语言,可以是整数或浮点数,是从1970年1月1日0时0分0秒到现在经历的秒数 2. ...

  7. day23 正则,re模块

    一. 简谈正则表达式 元字符 . 除了换行符外任意字符. \w 数字.字母.下划线 \s 空白符 \b 单词的末尾 \d 数字 \n 匹配换行符 \t 匹配制表符 \W 除了数字. 字母 下划线 \D ...

  8. python 基础之第十二天(re正则,socket模块)

    In [14]: 'hello-wold.tar.gz'.split('.') Out[14]: ['hello-wold', 'tar', 'gz'] In [15]: import re In [ ...

  9. Python正则、re模块

    正则的概念 findall        match        search  方法 元字符的用法和作用 正则表达式概念 正则表达式是对字符串操作的一种逻辑公式,就是对字符串的一种过滤 可以判断是 ...

随机推荐

  1. Mysql原理与优化

    原文:https://mp.weixin.qq.com/s__biz=MzI4NTA1MDEwNg==&mid=2650763421&idx=1&sn=2515421f09c1 ...

  2. 企业移动化?AppCan教你正确的打开方式

    七分选型.三分软件.的确,在过去的企业移动化进程中,由于选型失败导致信息系统实施失败的案例屡见不鲜.而在当今的移动互连和大数据时代,移动化已经是企业必然的选择. 那么,什么是企业移动化呢?怎样才是企业 ...

  3. 类 Calendar

    简介 Java.util.Calendar是日历类,在Date后出现,替换掉了许多Date的方法.该类将所有可能用到的时间信息封装为静态成员变量,方便获取.日历类就是方便获取各个时间属性的.注意Cal ...

  4. 三、Redis基础操作

    前言: Redi是key-value的NoSQL,我们用Redis提供的redis-cli就能操作String类型key和各种数据类型value.但是放入的不是特定类型数据,添加的都是一个一个Stri ...

  5. Python进阶8---面向对象基础1

    面向对象 语言的分类 Python的类 定义 class ClassName: pass class MyCalss: """A example class"& ...

  6. 【MySQL 读书笔记】全局锁 | 表锁 | 行锁

    全局锁 全局锁是针对数据库实例的直接加锁,MySQL 提供了一个加全局锁的方法, Flush tables with read lock 可以使用锁将整个表的增删改操作都锁上其中包括 ddl 语句,只 ...

  7. React Native & react-native-web-player & React Native for Web

    React Native & react-native-web-player & React Native for Web https://github.com/dabbott/rea ...

  8. Java代理模式之Cglib代理

    Cglib代理,也叫做子类代理.在内存中构建一个子类对象从而实现对目标对象功能的扩展. CGLIB包的底层是通过使用一个小而快的字节码处理框架ASM,来转换字节码并生成新的类.不鼓励直接使用ASM,因 ...

  9. git的安装(和远程仓库建立连接)

    安装完git 1.配置用户名和邮箱 $ git config --global user.name "My Name"  $ git config --global user.em ...

  10. ElasticSearch搜索(一)

    首先从ES的支持的字段说起,ES文档中字段有多种类型 官方文档. 这几个比较常用: text,keyword,integer,float,boolean,object,geo_point(地理坐标), ...