博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
简单的正则解析
阅读量:4478 次
发布时间:2019-06-08

本文共 2789 字,大约阅读时间需要 9 分钟。

简单的正则

代码如下:

1 import re 2 #提取出python 3 key="javapythonc++php" 4 re.findall('python',key)[0] 5 # ##################################################################### 6 #提取出hello world 7 key="

hello world

" 8 re.findall('

(.*)

',key)[0] 9 # #####################################################################10 #提取17011 string = '我喜欢身高为170的女孩'12 re.findall('\d+',string)13 # #####################################################################14 #提取出http://和https://15 key='http://www.baidu.com and https://boob.com'16 re.findall('https?://',key)17 # #####################################################################18 #提取出hello19 key='lalalahellohahah' #输出hello20 re.findall('<[hH][tT][mM][lL]>(.*)
',key)21 # #####################################################################22 #提取出hit. 23 key='bobo@hit.edu.com'#想要匹配到hit.24 re.findall('h.*?\.',key)25 # #####################################################################26 # #匹配sas和saas27 # key='saas and sas and saaas'28 # #####################################################################29 # #匹配出i开头的行30 string = '''fall in love with you31 i love you very much32 i love she33 i love her'''34 35 re.findall('^i.*',string,re.M)36 # #####################################################################37 # #匹配全部行38 string1 = """
细思极恐39 你的队友在看书40 你的敌人在磨刀41 你的闺蜜在减肥42 隔壁老王在练腰43
"""44 45 re.findall('.*',string1,re.S)

简单的正则匹配数据

糗事百科糗图爬取

代码如下

1 import requests 2 from urllib import request 3 import re 4 import os 5 import random 6 #代理列表 7 proxies = [ 8     {
'https':'51.15.76.205:3128'}, 9 {
'https':'110.74.221.229:30838'},10 {
'https':'222.248.243.103:8118'},11 {
'https':'103.91.206.146:8080'},12 13 ]14 url = 'https://www.qiushibaike.com/pic/page/%d/?s=5148302'15 16 headers = {17 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36'18 }19 #创建一个文件夹20 if not os.path.exists('./qiutu'):21 os.mkdir('./qiutu')22 23 for pageNum in range(1,3):24 new_url = format(url%pageNum)25 #获取某一个页面对应的页面数据26 page_text = requests.get(url=new_url,headers=headers,proxies=random.choice(proxies)).text27 #获取解析到页面中所有图片的连接28 imgSrc_list = re.findall('
.*?
',page_text,re.S)29 for src in imgSrc_list:30 img_url = 'https:'+src31 img_name = src.split('/')[-1]32 img_path = './qiutu/'+img_name33 #request.urlretrieve(url=img_url,filename=img_path)34 img_data = requests.get(url=img_url,headers=headers,proxies=random.choice(proxies)).content35 with open(img_path,'wb') as fp:36 fp.write(img_data)37 print(img_name+'下载成功')38
糗图爬取

 

转载于:https://www.cnblogs.com/duanhaoxin/p/10110852.html

你可能感兴趣的文章
使用Android NDK以及JNI编写应用
查看>>
学习笔记之-php数组数据结构
查看>>
初学者--bootstrap(六)组件中的下拉菜单----在路上(10)
查看>>
QMetaObject::connectSlotsByName 总结
查看>>
Android 微信支付步骤
查看>>
js操作table
查看>>
JQuery学习系列篇(一)
查看>>
Centos7 minimal 系列之rabbitmq安装(八)
查看>>
英语语法(2)----点破主谓宾系表三大句型
查看>>
html如何与cgi数据交换,HTML网页与CGI之间通信的 实例分析
查看>>
html如何调用flash插件,htmlflash播放器插件如何播放 网页播放器flash插件怎么解决...
查看>>
mysql数据在html上面显示不出来的,HTML表格不能正确显示MySQL数据
查看>>
数据包和html,数据包和数据报有何区别?
查看>>
jq 异步调用一个html,聊聊如何将jQuery的$.ajax()用于异步HTTP请求
查看>>
android 7.0宽度432,全球最小的4G手机,比手掌还小,安卓7.0
查看>>
android fragmentstatepageradapter框架,Android FragmentStatePagerAdapter
查看>>
html自适应meta标签,自适应布局meta标签中viewport、content、width、initial-scale、minimum-scale、maximum-scale总结...
查看>>
html怎么加入编辑器,HTML 编辑器
查看>>
python发挥程度_你为什么用 Python?
查看>>
file 选择的文件胖多有多大_「HTML5 进阶」FileAPI 文件操作实战,内附详细案例,建议收藏...
查看>>