您的位置 首页 php

正则表达式和 Cookie使用

1.原子概念

原子是正则表达式中最基本的组成单位

import re
#普通的字符作为原子
pattern = "baidu"
string = ""
result=re.search(pattern,string)
print(result)
1
2
3
4
5
6
import re
#非打印字符(.控制各式的符号)作为原子
pattern = "\n"#换行符
string = """
http:www.so.com"""
result=re.search(pattern,string)
print(result)
1
2
3
4
5
6
7
import re
#通用字符(一个原子代表一类字符)作为原子
pattern = "\w\dpython\w"#\w代表任意数字字母下划线,\d 代表任意的十进制数
string = """达达达达eeee313python_dadad"""
result=re.search(pattern,string)
print(result)
1
2
3
4
5
6
import re
#原子表
pattern1 = "\w\dpython[xyz]\w"#\w代表任意数字字母下划线,\d代表任意的十进制数,[xyz]表示可以匹配x,z,y其中任意一个
pattern2 = "\w\dpython[^xyz]\w"#\w代表任意数字字母下划线,\d代表任意的十进制数,[^xyz]表示可以匹配除了x,z,y的字符
pattern3 = "\w\dpython[xyz]\W"#\w代表任意数字字母下划线\W代表 除了任意数字字母下划线的字符,\d代表任意的十进制数,[xyz]表示可以匹配x,z,y其中任意一个
string = """达达达达eeee313pythony_dadad"""
result=re.search(pattern1,string)
print(result)
result=re.search(pattern2,string)
print(result)
result=re.search(pattern3,string)
print(result)
1
2
3
4
5
6
7
8
9
10
11
12
 

2.元字符概念

正则表达式中具备一些特殊意义的字符,本文只给出一般性用法,欲了解更多,请参考:

正则表达式30分钟入门教程

python 正则表达式指南

import re
#任意匹配元字符
pattern1 = ".python..."#.每个.都可以匹配除了换行符意外的任意一个内容
string = """达达达达eeee313pythony_dadad"""
result=re.search(pattern1,string)
print(result)
1
2
3
4
5
6
import re
#边界限制元字符
pattern1 = "^31"#.匹配以 31开头的
pattern2 = "^32"#.匹配以 32开头的
pattern3 = "da$"#.匹配以da 结尾的
pattern4 = "dr$"#.匹配以dr 结尾的
string = """313pythony_da"""
result=re.search(pattern1,string)
print(result)
result=re.search(pattern2,string)
print(result)
result=re.search(pattern3,string)
print(result)
result=re.search(pattern4,string)
print(result)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import re
#限定符
pattern1 = "py.*n"#py和 n之间有0次1次或者多次的除了换行符之外的任意字符
pattern2 = "cd{2}"#匹配 d 出现两次
pattern3 = "cd{2,}"#匹配 d 出现两次到多次
string = """313pythony_cdacddddc"""
result=re.search(pattern1,string)
print(result)
result=re.search(pattern2,string)
print(result)
result=re.search(pattern3,string)
print(result)
1
2
3
4
5
6
7
8
9
10
11
12
import re
#模式选择符
pattern1 = "python|php"#符合 python 或者 php 任一条件即可匹配成功
string = """31php3pythony_cdacddddc"""
result=re.search(pattern1,string)
print(result)
1
2
3
4
5
6
import re
#模式单元符,将小原子组合成大原子
pattern2 = "(cd){2}"#匹配 cd 这个大原子整体出现两次
pattern3 = "cd{2,}"#匹配 d 这个 小原子出现两次到多次
string = """313pythony_cdacdcdddc"""
result=re.search(pattern2,string)
print(result)
result=re.search(pattern3,string)
print(result)
1
2
3
4
5
6
7
8
9
 

3.模式修正

import re
#模式修正符
pattern2 = "python"#匹配 cd 这个大原子整体出现两次
pattern3 = "python"#匹配 d 这个 小原子出现两次到多次
string = """313Pythony_cdacdcdddc"""
result=re.search(pattern2,string)
print(result)
result=re.search(pattern3,string,re.I)# 匹配的时候忽略大小写
print(result)
1
2
3
4
5
6
7
8
9
 

3.贪婪模式和懒惰模式

import re
#模式单元符
pattern2 = "p.*c"#默认的时候都是贪婪模式,找到最后满足条件的
pattern3 = "p.*?c"#加个问号就转化为懒惰模式了,找到第一个满足条件的就会停下
string = """313Pythony_cdacdcdddc"""
result=re.search(pattern2,string, re.I)
print(result)
result=re.search(pattern3,string,re.I)# 匹配的时候忽略大小写
print(result)
1
2
3
4
5
6
7
8
9
 

4.正则表达式使用中常见的函数

re.match(),re.search(), 二者之间有什么区别呢?

match 是从开头位置开始匹配, search 是全文中检索匹配

import re
#match 是从开头位置开始匹配, search 是全文中检索匹配
pattern2 = ".python"#默认的时候都是贪婪模式,找到最后满足条件的
pattern3 = ".python"#加个问号就转化为懒惰模式了,找到第一个满足条件的就会停下
string = """313pythony_cdacdcdddc"""
result=re.match(pattern2,string, re.I)
print(result)
result=re.search(pattern3,string,re.I).span()# 匹配的时候忽略大小写,.span()过滤掉无关信息,留下位置信息
print(result)
1
2
3
4
5
6
7
8
9
 

上面的都是只能匹配一个, 预编译后使用findall()可以将符合条件的内容全部匹配出来

import re
#查找所有内容
pattern = re.compile(".python.")#对正则表达式进行预编译
string = """313pythony_cdacdcdddcpython666"""
result=pattern.findall(string)
print(result)
1
2
3
4
5
6
 

re.sub()实现符合条件的字符替换功能

import re
#match 是从开头位置开始匹配, search 是全文中检索匹配
pattern2 = "python"#
string = """313pythony_cdacdcddpythondc"""
result=re.sub(pattern2,"php",string)#全部替换
print(result)
result=re.sub(pattern2,"php",string,1)#最多替换一次
print(result)
1
2
3
4
5
6
7
8
 

5.使用 Cookie 进行登录

import urllib. request 
import urllib.parse
import http.cookiejar
url=""
postdata=urllib.parse.urlencode({
 "formhash":"b3702802","referer":"",
 "username":"zhangkun","password":"passwordzzz",
 "loginsubmit":"true","return_type":""}).encode('utf-8')
req=urllib.request.Request(url,postdata)
req.add_header("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (K html , like Gecko) Chrome/63.0.3239.132 Safari/537.36")
cjar=http.cookiejar.CookieJar()#创建了一个 CookieJar 的对象 cjar
 open er=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cjar))#以 cjar 为参数,使用 HTTPCookieProcessor 创建一个 Cookie 处理器,以该处理器为参数,通过 urllib.request.build_opener 创建一个 opener 对象
urllib.request.install_opener(opener)#创建全局默认的 opener 对象
result=opener.open(req)
data=result.read()
htmlfile=open("chinaunix.html","wb")
htmlfile.write(data)
htmlfile. close ()
url2=""
data2=urllib.request.urlopen(url2).read()
htmlfile2=open("chinaunix2.html","wb")
htmlfile2.write(data2)
htmlfile2.close()
 

文章来源:智云一二三科技

文章标题:正则表达式和 Cookie使用

文章地址:https://www.zhihuclub.com/151960.shtml

关于作者: 智云科技

热门文章

网站地图