简单的正则解析

简单的正则

代码如下:

 1 import re
 2 #提取出python
 3 key="javapythonc++php"
 4 re.findall('python',key)[0]
 5 # #####################################################################
 6 #提取出hello world
 7 key="<html><h1>hello world<h1></html>"
 8 re.findall('<h1>(.*)<h1>',key)[0]
 9 # #####################################################################
10 #提取170
11 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 #提取出hello
19 key='lalala<hTml>hello</HtMl>hahah' #输出<hTml>hello</HtMl>
20 re.findall('<[hH][tT][mM][lL]>(.*)</[hH][tT][mM][lL]>',key)
21 # #####################################################################
22 #提取出hit. 
23 key='bobo@hit.edu.com'#想要匹配到hit.
24 re.findall('h.*?.',key)
25 # #####################################################################
26 # #匹配sas和saas
27 # key='saas and sas and saaas'
28 # #####################################################################
29 # #匹配出i开头的行
30 string = '''fall in love with you
31 i love you very much
32 i love she
33 i love her'''
34 
35 re.findall('^i.*',string,re.M)
36 # #####################################################################
37 # #匹配全部行
38 string1 = """<div>细思极恐
39 你的队友在看书
40 你的敌人在磨刀
41 你的闺蜜在减肥
42 隔壁老王在练腰
43 </div>"""
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)).text
27     #获取解析到页面中所有图片的连接
28     imgSrc_list = re.findall('<div class="thumb">.*?<img src="(.*?)".*?</div>',page_text,re.S)
29     for src in imgSrc_list:
30         img_url = 'https:'+src
31         img_name = src.split('/')[-1]
32         img_path = './qiutu/'+img_name
33         #request.urlretrieve(url=img_url,filename=img_path)
34         img_data = requests.get(url=img_url,headers=headers,proxies=random.choice(proxies)).content
35         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