• 简单的正则解析


    简单的正则

    代码如下:

     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      
    糗图爬取
  • 相关阅读:
    word删除脚注的方法
    移动最小二乘
    word插入的对象和文字混合到了一起的解决办法
    插值
    中学小学学校学生德育量化管理系统_文明班评比量化系统_德育评价系统_德育量化考核系统_政教管理系统_政教考核系统
    需求的最初形式:12306ng的需求小说
    异步javascript的原理和实现
    需求与设计过程(1)用例
    项目管理沙龙第十二次会议纪要为没有共识的项目组定制敏捷方法
    项目进度控制的技术
  • 原文地址:https://www.cnblogs.com/duanhaoxin/p/10110852.html
Copyright © 2020-2023  润新知