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. SQLserver2008一对多,多行数据显示在一行

    现在有一个需求 我们有一张表employee EmpID EmpName ---------- ------------- 张山 张大山 张小山 李菲菲 李晓梅 Result I need in th ...

  2. ASP.NET MVC 下自定义模型绑定,去除字符串类型前后的空格

    直接贴代码了: SkyModelBinder.cs using System.ComponentModel; using System.Linq; using System.Web.Mvc; name ...

  3. EntityFramework Core 2.1重新梳理系列属性映射(一)

    前言 满血复活啦,大概有三个月的时间没更新博客了,关于EF Core最新进展这三个月也没怎么去看,不知现阶段有何变化没,本文将以EF Core 2.1稳定版本作为重新梳理系列,希望对看本文的你有所帮助 ...

  4. 64位ubuntu安装交叉编译工具链,显示找不到命令

    是因为Ubuntu64位版本已不支持ia32-libs的软件包,而是使用了lib32ncurses5.lib32z1软件包做为替代, 所以在Ubuntu16.04版本当中应该安装执行: sudo ap ...

  5. python读写excel文件

    '''xlrd和xlwt处理的是xls文件,单个sheet最大行数是65535,如果有更大需要的,建议使用openpyxl函数,最大行数达到1048576.'''import openpyxl ''' ...

  6. RedisCache 缓存

    /// <summary> /// 这是包装过公用的,用于本站而已. /// </summary> /// <author>rixiao</author> ...

  7. ArrayList如何扩容?

    1.调用ArrayList的参构造方法,此时集合内部是一个空数组 transient Object[] elementData; private static final Object[] DEFAU ...

  8. ubuntu14.04按照mysql5.7

    1.安装mysql5.5 https://www.cnblogs.com/zhuyp1015/p/3561470.html https://www.cnblogs.com/ruofengzhishan ...

  9. Java 删除ArrayList中重复元素,保持顺序

    // 删除ArrayList中重复元素,保持顺序          public static List<Map<String, Object>> removeDuplicat ...

  10. 20165223《网络对抗技术》Exp0 Kali的安装

    (1)安装步骤 Kali官网,下载Kali Linux 64 bit版本,按照网上安装教程进行即可 我是直接拷贝了Kali的.vmx文件(Linux 64bit),在上学期已经安装好的VMware中可 ...