博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
简单的正则解析
阅读量:4477 次
发布时间: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

你可能感兴趣的文章
深入研究java.lang.Runtime类
查看>>
10677 我们仍未知道那天所看见的花的名字
查看>>
ScanTailor-ScanTailor 自动矫正图像歪斜
查看>>
UVA GCD - Extreme (II)
查看>>
完成个人中心—导航标签
查看>>
【C++】C++中变量的声明与定义的区别
查看>>
前端性能优化
查看>>
static
查看>>
属性动画
查看>>
Swift 字符串
查看>>
Python 生成器 Generator 和迭代器 Iterator
查看>>
实现icon和文字垂直居中的两种方法-(vertical-align and line-height)
查看>>
[CareerCup] 3.6 Sort Stack 栈排序
查看>>
Beta版总结会议
查看>>
Cocos2d-x中使用的数据容器类
查看>>
创建ORACLE 查询用户
查看>>
jzoj3297. 【SDOI2013】逃考
查看>>
通过例子学python(2.1)
查看>>
高效率场景-内存映射
查看>>
Python基础——0前言
查看>>