# -*- coding: utf-8 -*-
import re # 内置库
from requests_html import HTMLSession
"""
1、re.compile 正则表达式的语法
2、re.search 找一个
3、re.match 从头找一个 没有返回None
4、re.findall 找所有 返回列表
. 匹配任意字符,除了换行符,re.DOTALL标记被指定时,则可以匹配包括换行符的任意字符
* 匹配0个或多个的表达式
+ 匹配1个或者多个的表达式
? 匹配0个或1个由前面的正则表达式定义的片段,非贪婪方式
[] 表示一组字符。
"|" A|B,创建一个正则,将匹配A或B。
\s 匹配任何空白字符
\d 匹配任何数字
"""
str1 = '<link rel="search" type="application/opensearchdescription+xml" href="/content-search.xml" title="百度搜索1" title="百度搜索2"/>'
result = re.findall('title="(.*?)"', str1) # 第一个参数正则,第二个屎字符串
print(result)
str2 = """
<html>
<body>
<p>python</p>
<div>
Line 1
</div>
</body>
</html>
"""
result1 = re.findall('<p>(.*?)</p>', str2) # 匹配p标签的内容
print(result1[0])
# re.S可以多行匹配 re的对象是一行一行匹配
result2 = re.findall('<div>(.*?)</div>', str2, re.S)[0].strip() # 得到列表,取下标0,得到字符串,通过strip()去除两边空白
print(result2)
爬虫之re匹配
最新推荐文章于 2024-12-05 09:18:12 发布