day 18 - 2 正则与 re 模块练习
2024-10-19 03:27:14
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))
最新文章
- GroupBy(..)的四种声明方式的理解及调用
- springboot中swaggerUI的使用
- examine self thrice a day2017
- [deviceone开发]-数据绑定示例
- 改变Vim在iTerm2中的光标
- scrum站立会议学习
- LVS负载均衡集群服务搭建详解(一)
- 数组越界保护与消息传递black机制
- flex+java将数据库里的数据导出到指定目录下excel表里(poi)
- hdu3368之DFS
- C# 模拟提交带附件(input type=file)的表单
- 14.9.4 COMPACT and REDUNDANT Row Formats
- 使用jQuery筛选排除元素以修改指定标签的属性
- windows c++ 错误汇总
- Android插件化的兼容性(下):突破Android P中灰黑名单的限制
- Windows Server 2016 + SCO 2016 安装及配置介绍
- Confluence 6 整合到其他数据库
- Python 2.7 cython cythonize py 编译成 pyd 谈谈那些坑
- 云计算SPI(SaaS、PaaS、IaaS)
- 浙江工业大学校赛 小M和天平
热门文章
- windows下mysql 5.7以上版本安装及遇到的问题
- jenkins自动化部署vue
- In action ";Setting JDBC driver jar location unix [Set a variable]"; (screen ";Select a Database [Configurable banner form]";), property ";Script";:
- 02 Django REST Framework 序列化
- Python_if
- linux环境下vim创建java文件,并编译运行
- Windows 10 &; React Native &; Android
- Git学习指北
- linux shell通配符及if语句判断
- DRF 商城项目 - 购物( 购物车, 订单, 支付 )逻辑梳理