Python第二阶段学习 - week6总结
一. 爬虫
1. 思维导图
2. 正则表达式
1)相关函数
函数 | 说明 |
---|---|
compile(pattern, flags=0) | 编译正则表达式返回正则表达式对象 |
match(pattern, string, flags=0) | 用正则表达式匹配字符串 成功返回匹配对象 否则返回None |
fullmatch(pattern, string, flags=0) | 检查字符串是否与正则表达式完全匹配 |
search(pattern, string, flags=0) | 搜索字符串中第⼀次出现正则表达式的模式 成功返回匹配对象 否则返回None |
findall(pattern, string,flags=0) | 查找字符串所有与正则表达式匹配的模式 返回字符串的列表 |
finditer(pattern, string,flags=0) | 查找字符串所有与正则表达式匹配的模式 返回⼀个迭代器 |
compile(pattern, flags=0) | 编译正则表达式返回正则表达式对象 |
sub(pattern, repl, string,count=0, flags=0) | 用指定的字符串替换原字符串中与正则表达式匹配的模式 可以用count指定替换的次数 |
re.I / re.IGNORECASE | 忽略大小写匹配标记 |
2)元字符
符号 | 解释 |
---|---|
^ | 字符串的开始 |
$ | 字符串的结束 |
\d | 数字 |
\D | 非数字 |
\w | 英文大小写字母、数字、下划线 |
\W | 不是英文大小写字母、数字、下划线 |
\s | 空白字符 |
\S | 不是空白字符 |
. | 匹配任意的字符 |
* | 通配符 —> 模糊匹配 |
*? | 惰性匹配(尽可能短的匹配) |
.*? | 表示匹配任意数量的重复,但是在能使整个匹配成功的前提下使用最少的重复 |
?= | 内容后面是指定内容 |
?<= | 内容前面是指定内容 |
?! | 前瞻(向后面看):内容后面不能是指定内容,例如:?=\d —> 后面必须是数字;?!\d —> 后面不能是数字 |
?<! | 回顾(向前面看):内容前面不能是指定内容,例如:?<=\d —> 前面必须是数字;?<!\d —> 前面不能是数字 |
(exp) | 匹配exp并捕获到自动命名的组中 |
([^]exp) | 匹配exp(不包含[^]里的内容)并捕获到自动命名的组中 |
+? | 惰性匹配(尽可能短的匹配),出现1次或任意多次 |
3)字符集
- [ ]:字符必须是字符集中的任意一个
[abc] —> abc任意取其一
[a-z0-9A-Z] —> 大小写字母、数字任意取其一
4)量词
符号 | 解释 |
---|---|
{N} | 前面的字符刚好出现N次 |
{N, M} | 最少N次,最多M次 |
{, M} | 最大M次,最少可以没有 |
* | 出现0次或任意多次 |
+ | 出现1次或任意多次 |
? | 出现0次或1次 |
import re
content = """小明的手机号是13011223344,不是15800224567,也不是110。
大华135的手机号已经停用了,请拨打13899887766这个号码。
大华的银行存款没有1350099887766元。"""
# 方法一:通过search函数
matcher = re.search(r'(?<!\d)1[3-9]\d{9}(?!\d)', content)
while matcher:
print(matcher.group()) # 将抽取的对象拿出来,提取与正则表达式匹配的内容
content = content[matcher.end():]
matcher = re.search(r'1[3-9]\d{9}', content)
# 方法二:通过findall函数
# findall:提取字符串中所有与正则表达式匹配的内容
items = re.findall(r'(?<!\d)1[3-9]\d{9}(?!\d)', content) # findall
for item in items:
print(item)
# 方法三:通过finditer函数
# finditer:提取字符串中所有与正则表达式匹配的内容
iter_obj = re.finditer(r'1[3-9]\d{9}', content)
for matcher in iter_obj:
print(matcher.group())
3. XPATH
XML(extensible markup language) - 可扩展标记语言
在Google浏览器使用Xpath扩展程序,可以查看
pip install lxml
1) 语法
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book>
<title lang="eng">Harry Potter</title>
<price>29.99</price>
</book>
<book>
<title lang="eng">Learning XML</title>
<price>39.95</price>
</book>
</bookstore>
路径表达式 | 结果 |
---|---|
/bookstore | 选取根元素 bookstore。注意:假如路径起始于正斜杠( / ),则此路径始终代表到某元素的绝对路径! |
//book | 选取所有 book ⼦元素,⽽不管它们在⽂档中的位置。 |
//@lang | 选取名为 lang 的所有属性。 |
/bookstore/book[1] | 选取属于 bookstore ⼦元素的第⼀个 book 元素。 |
/bookstore/book[last()] | 选取属于 bookstore ⼦元素的最后⼀个 book 元素。 |
/bookstore/book[last()-1] | 选取属于 bookstore ⼦元素的倒数第⼆个 book 元素。 |
/bookstore/book[position() < 3] | 选取最前⾯的两个属于 bookstore 元素的⼦元素的 book 元素。 |
//title[@lang] | 选取所有拥有名为 lang 的属性的 title 元素。 |
//title[@lang=‘eng’] | 选取所有 title 元素,且这些元素拥有值为 eng 的 lang 属性。 |
/bookstore/book[price>35.00] | 选取 bookstore 元素的所有 book 元素,且其中的 price 元素的值须⼤于 35.00。 |
/bookstore/book[price>35.00]/title | 选取 bookstore 元素中的 book 元素的所有 title 元素,且其中的 price 元素的值须⼤于 35.00。 |
/bookstore/* | 选取 bookstore 元素的所有⼦元素。 |
//* | 选取⽂档中的所有元素。 |
//title[@*] | 选取所有带有属性的 title 元素。 |
//book/title |//book/price | 选取 book 元素的所有 title 和 price 元素。 |
//title |//price | 选取⽂档中的所有 title 和 price 元素。 |
/bookstore/book/title |//price | 选取属于 bookstore 元素的 book 元素的所有 title 元素,以及⽂档中所有的 price 元素。 |
2) 运用(requests.get里的参数内容根据页面内容填写)
import requests
from lxml import etree
resp = requests.get(url='',
headers={'Accept-Language': '',
'User-Agent': '',
'Cookie': ''})
print(resp.status_code)
if resp.status_code == 200:
html_obj = etree.HTML(resp.text)
results = html_obj.xpath('//*[@id="content"]/div/div[1]/ol/li[1]/div/div[2]/div[1]/a/span[1]/text()')
print(results)
二. requests模块补充、数据的操作、并发编程
1. 思维导图
三. 迭代器和生成器
1. 迭代器
-
概念:实现了迭代器协议的对象
- 迭代器协议:两个魔术方法
- _ _ iter_ _ —> 返回迭代器对象
- _ _ next_ _ —> 从迭代器获取下一个迭代值
- 迭代器协议:两个魔术方法
-
案例:斐波那契数列: 1 1 2 3 5 8 13 21 34 55 …
class FibIter: def __init__(self, max_count): self.a, self.b = 0, 1 self.cur_count = 0 self.max_count = max_count def __iter__(self): return self def __next__(self): if self.cur_count < self.max_count: self.a, self.b = self.b, self.a + self.b self.cur_count += 1 return self.a raise StopIteration() obj = FibIter(20) for value in obj: print(value)
2. 生成器
-
概念:迭代器的语法升级简化版本
-
案例:斐波那契数列: 1 1 2 3 5 8 13 21 34 55 …
def fib(max_count): a, b = 0, 1 for _ in range(max_count): a, b = b, a + b yield a # 调用函数不是得到返回值而是创建了一个生成器对象 obj = fib(20) for value in obj: print(value)
四. 协程(co-routine)
1. 概念
- 相互协作的子程序
- 生成器经过预激活就可以成为协程(跟其他子程序进行协作 —> 协作时并发)
2. 案列
def calc_average():
total, counter = 0, 0
avg_value = None
while True:
curr_value = yield f'平均数是:{avg_value}'
total += curr_value
counter += 1
avg_value = total / counter
def main():
obj = calc_average()
# 预激活生成器 next(obj)
obj.send(None)
for _ in range(5):
print(obj.send(float(input('请输入一个数:'))))
if __name__ == '__main__':
main()