网站备案到哪里下载/专注于品牌营销服务

网站备案到哪里下载,专注于品牌营销服务,如何设计商务网站,bootstrap响应式网站开发实战电商利用 requests 可以获取网站页面数据,但是 requests 返回的数据中包含了一些冗余数据,我们需要在这些数据集中提取自己需要的信息。所以我们要学会在数据集中提取自己需要的数据。 需要掌握的知识点如下: json 数据提取 jsonpath 语法 静态…

利用 requests 可以获取网站页面数据,但是 requests 返回的数据中包含了一些冗余数据,我们需要在这些数据集中提取自己需要的信息。所以我们要学会在数据集中提取自己需要的数据。

需要掌握的知识点如下:

  • json 数据提取
    • jsonpath 语法
  • 静态页面数据提取
    • xpath语法
    • bs4 模块使用
  • 正则表达式的使用

1.数据提取的概念和数据分类

在爬虫爬取的数据中有很多不同类型的数据,我们需要了解数据的不同类型来有规律的提取和解析数据。

  • 结构化数据:json、xml
    • 处理方式:直接转化为 python 数据类型
  • 非结构化数据:HTML
    • 处理方式:正则表达式、xpath、bs4

1.1 结构化数据

json

xml

1.2 非结构化数据

2.结构化数据提取-json

什么是 json

JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,它使得人们很容易的进行阅读和编写,同时也方便了机器进行解析和生成,适用于进行数据交互的场景,比如网站前端和后端之间的数据交互。

 json 模块方法回顾

# json.dumps 实现python类型转化为json字符串
# indent实现缩进格式
# ensure_ascii=False实现让中文写入的时候保持为中文
json_str = json.dumps(mydict, indent=2, ensure_ascii=False)# json.loads 实现json字符串转化为python的数据类型
my_dict = json.loads(json_str)

 代码示例

import json
import requests# 网站地址: http://www.cninfo.com.cn/new/commonUrl?url=disclosure/list/notice#szse# 获取公告信息
url = 'http://www.cninfo.com.cn/new/disclosure'# 定义请求头,模拟浏览器
headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) ""AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
}# 定义表单数据
post_data = {'column': 'szse_latest','pageNum': 1,'pageSize': 30,'sortName': '','sortType': '','clusterFlag': 'true'
}# 请求json数据
r = requests.post(url, headers=headers, data=post_data)# 解码
json_str = r.content.decode()# 把json格式字符串转换成python对象
json_dict = json.loads(json_str)print(json_dict)print("\n\n")# 还可以使用json()方法, 免去了自己手动编解码的步骤
print(r.json())

3. xpath语法

3.1 什么是xpath

Xpath(XML Path Language) 即 XML路径语言,在最初时它主要在 xml 文档中查找需要的信息,而现在它也适用于 HTML 文档的搜索。

W3School 官方文档:

http://www.w3school.com.cn/xpath/index.asp

xpath 可以很轻松的选择出想要的数据,提供了非常简单明了的路径选择表达式,几乎想要任何定位功能,xpath 都可以很轻松的实现。

所以在之后的静态网站数据提取中会经常使用 xpath 语法完成。

3.2 xpath 节点

每个标签我们都称之为 节点,其中最顶层的节点称为 根节点

辅助工具

  • Chrome 浏览器插件: XPath Helper
  • Firefox 浏览器插件:XPath Finder 

注意:这些工具是用来学习xpath语法的,当熟练掌握 xpath 的语法后就可以直接在代码中编写 xpath 而不一定非要用此工具。

3.3 语法规则

Xpath 使用路径表达式来选取文档中的节点或者节点集。这些路径表达式和我们在常规的电脑文件系统中看到的表达式非常相似。

表达式描述
nodename选中该元素
/从根节点选取、或者是元素和元素间的过渡
//从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置
.选取当前节点
..选取当前节点的父节点
@选取属性
text()选取文本
路径表达式
路径表达式结果
/bookstore选取根元素 bookstore。注释:假如路径起始于斜杠(/),则此路径始终代表到某元素的绝对路径
bookstore/book选取属于 bookstore 之下的所有book元素
//book选取所有book子元素,而不管它们在文档中的位置
bookstore//book选择属于bookstore元素的后代的所有book元素,而不管它们位于bookstore之下的什么位置
//book/title/@lang选择所有的 book 下面的 title 中的 lang 属性的值
//book/title/text()选择所有的 book 下面的 title 的文本
查询特定节点
路径表达式结果
//title[@lang="eng"]选择 lang 属性值为 eng 的所有 title 元素
/bookstore/book[1]选取属于 bookstore 子元素的第1个book元素
/bookstore/book[last()]选取属于 bookstore 子元素的最后 1 个book元素
/bookstore/book[last()-1]选取属于 bookstore 子元素的倒数第 2 个book元素
/bookstore/book[position()>1]选择 bookstore 下面的 book 元素,从第 2 个开始选择
/bookstore/book[position()>1 and position()<4]选择 bookstore 下面的 book 元素,从第2个开始取到第 4 个元素
//book/title[text()='Harry Potter']选择所有 book 下的 title 元素,仅仅选择文本为 Harry Poter的 title 元素

注意点:在 xpath 中,第一个元素的位置是 1 ,最后一个元素的位置是 last(),倒数第二个是last()-1

语法练习

接下来对豆瓣2电影 top250 的页面来练习上述语法:

https://movie.douban.com/top250

  • 选择所有的 h1 下的文本
//h1/text()
  • 获取电影信息的href信息
//div[@class='item']/div[1]/a/@href
  • 获取电影的评价人数
//div[@class='star']/span[last()]/text()

总结

  1. xpath 的概述: Xpath(XML Path Language),解析查找提取信息的语言
  2. xml 是和服务器交互的数据格式和 json 的作用一致
  3. html 是浏览器解析标签数据显示给用户
  4. Xpath 的重点语法获取任意节点://
  5. Xpath 的重点语法根据数据获取节点:标签[@属性='值']
  6. Xpath 的获取节点属性值:@属性值
  7. Xpath 的获取节点文本值:text()

4.使用lxml模块中的xpath语法提取非结构化数据

前面学习的 xpath 知识主要的作用是:学会怎样通过 xpath 语法找到需要的数据,想要的代码中使用 xpath 进行处理,就需要学习另外一个新模块 lxml

4.1 模块的安装

pip install lxml -i https://pypi.tuna.tsinghua.edu.cn/simple

4.2 lxml 的使用

1.使用 lxml 转化为 Element 对象

from lxml import etreetext = ''' <div> <ul> <li class="item-1"><a href="link1.html">first item</a></li> <li class="item-1"><a href="link2.html">second item</a></li> <li class="item-inactive"><a href="link3.html">third item</a></li> <li class="item-1"><a href="link4.html">fourth item</a></li> <li class="item-0"><a href="link5.html">fifth item</a> </ul> </div> '''# 利用etree.HTML,将字符串转化为Element对象,Element对象具有Xpath的方法
html=etree.HTML(text)
print(type(html))#将Element对象转化为字符串
handled_html_str=etree.tostring(html).decode()
print(handled_html_str)

2.使用 lxml  中的 xpath 语法提取数据

提取 a标签 属性和文本

from lxml import etreetext = ''' <div> <ul> <li class="item-1"><a href="link1.html">first item</a></li> <li class="item-1"><a href="link2.html">second item</a></li> <li class="item-inactive"><a href="link3.html">third item</a></li> <li class="item-1"><a href="link4.html">fourth item</a></li> <li class="item-0"><a href="link5.html">fifth item</a> </ul> </div> '''html=etree.HTML(text)# 获取href的列表和title的列表
href_list=html.xpath("//li[@class='item-1']/a/@href")
title_list=html.xpath("//li[@class='item-1']/a/text()")for title,href in zip(title_list,href_list):item=dict()item['title']=titleitem['href']=hrefprint(item)

以上代码必须确保标签中的数据是一一对应的,如果有些标签中不存在指定的属性或文本则会匹配混乱。

输出结果为:

/Users/poppies/python_envs/base/bin/python3 /Users/poppies/Documents/spider_code/1.py
{'title': 'first item', 'href': 'link2.html'}
{'title': 'second item', 'href': 'link4.html'}

3. xpath 分次提取

前面我们取到属性,或者是文本的时候,返回字符串 但是如果我们取到的是一个节点,返回什么呢?

返回的是 element 对象,可以继续使用xpath方法

对此我们可以在后面的数据提取过程中:先根据某个xpath规则进行提取部分节点,然后再次使用xpath进行数据的提取

 示例如下:

from lxml import etreetext = ''' <div> <ul><li class="item-1"><a>first item</a></li><li class="item-1"><a href="link2.html">second item</a></li><li class="item-inactive"><a href="link3.html">third item</a></li><li class="item-1"><a href="link4.html">fourth item</a></li><li class="item-0"><a href="link5.html">fifth item</a></ul> </div> '''html = etree.HTML(text)li_list = html.xpath("//li[@class='item-1']")
print(li_list)# 在每一组中继续进行数据的提取
for li in li_list:item = dict()item["href"] = li.xpath("./a/@href")[0] if len(li.xpath("./a/@href")) > 0 else Noneitem["title"] = li.xpath("./a/text()")[0] if len(li.xpath("./a/text()")) > 0 else Noneprint(item)

总结:

  1.  lxml 库的安装:pip install lxml
  2.  lxml的导包:from lxml import etree
  3.  lxml转换解析类型的方法:etree.HTML(text)
  4.  lxml解析数据的方法:data.xpath("//div/text()")
  5.  需要注意 lxml 提取完毕数据的数据类型都是列表类型
  6.  如果数据比较复杂:先提取大节点,然后再进行小节点操作

5.案例:通过xpath提取豆瓣电影评论

爬取豆瓣电影的评论,地址链接:

https://movie.douban.com/subject/1292052/comments?status=P

代码示例:

import requests
from lxml import etree# 1.通过requests发送请求获取豆瓣返回的内容
url='https://movie.douban.com/subject/1292052/comments?status=P'headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36'}res=requests.get(url,headers=headers)# 2. 将返回的内容通过etree.HTML转换为Element对象
html=etree.HTML(res.text)# 3. 对Element对象使用XPath提取数据
comment=html.xpath("//span[@class='short']//text()")for i in comment:print(i)

6.jsonpath 模块

JsonPath 是一种可以快速解析 json 数据的方式,JsonPath 对于JSON来说,相当于 XPath 对于XML,JsonPath 用来解析多层嵌套的json数据。

官网:https://goessner.net/articles/JsonPath/

想要在Python 编程语音中使用 JsonPath 对 json 数据快速提前,需要安装jsonpath模块。

pip install jsonpath -i https://pypi.tuna.tsinghua.edu.cn/simple

6.1 jsonpath 常用语法

代码示例

import jsonpathinfo = {"error_code": 0,"stu_info": [{"id": 2059,"name": "小白","sex": "男","age": 28,"addr": "河南省济源市北海大道xx号","grade": "天蝎座","phone": "1837830xxxx","gold": 10896,"info": {"card": 12345678,"bank_name": '中国银行'}},{"id": 2067,"name": "小黑","sex": "男","age": 28,"addr": "河南省济源市北海大道xx号","grade": "天蝎座","phone": "87654321","gold": 100}]
}""" 未使用jsonpath时,提取dict时的方式"""
res=info['stu_info'][0]['name']
print(res)
res1=info['stu_info'][1]['name']
print(res1)print("--------分割线--------")"""使用jsonpath时,提取dict时的方式"""res2=jsonpath.jsonpath(info,"$.stu_info[0].name")# $表示最外层的{}, . 表示子节点的意思
print(res2)
res3=jsonpath.jsonpath(info,"$.stu_info[1].name")
print(res3)res4=jsonpath.jsonpath(info,'$..name') # 嵌套n层也能取得的所有学生姓名信息,$表示最外层的{},...表示模糊匹配
print(res4)res5=jsonpaht.jsonpath(info,'$..bank_name')
print(res5)

6.2 jsonpath 对比 xpath:

XpathJSONPath结果
/store/book/author$.store.book[*].author商店里所有书籍的作者
//author$..author        所有作者
/store/*$.store.*商店里的所有东西,都是一些书和一辆红色的自行车
/store//price$.store..price商店里一切的价格
//book[3]$..book[2]第三本书
//book[last()]$..book[(@.length-1)]最后一本书
//book[position()<3]

$..book[0,1]

$..book[:2]

前两本书
//book[isbn]$..book[?(@.isbn)]使用isbn number过滤所有书籍
//book[price<10]$..book[?(@.price<10)]过滤所有便宜10以上的书籍
//*$..*XML文档中的所有元素。JSON结构的所有成员

练习代码:

import jsonpathinfo = {"store": {"book": [{"category": "reference","author": "Nigel Rees","title": "Sayings of the Century","price": 8.95},{"category": "fiction","author": "Evelyn Waugh","title": "Sword of Honour","price": 12.99},{"category": "fiction","author": "Herman Melville","title": "Moby Dick","isbn": "0-553-21311-3","price": 8.99},{"category": "fiction","author": "J. R. R. Tolkien","title": "The Lord of the Rings","isbn": "0-395-19395-8","price": 22.99}],"bicycle": {"color": "red","price": 19.95}}
}# 1. 提取第1本书的title
print('提取第1本书的title')
ret1=jsonpath.jsonpath(info,"$.store.book[0].title")
print(ret1)ret1=jsonpath.jsonpath(info,"$['store']['book'][0]['title']")
print(ret1)# 2. 提取2、3、4本书的标题
print('提取2、3、4本书的标题')
ret2=jsonpath.jsonpath(info,"$.store.book[1,2,3].title")
print(ret2)
ret2=jsonpath.jsonpath(info,"$.store.book[1:4].title")
print(ret2)# 3. 提取1、3本书的标题
print('提取1、3本书的标题')
ret3=jsonpath.jsonpath(info,"$.store.book[0,2].title")
print(ret3)# 4. 提取最后一本书的标题
print('提取最后一本书的标题')
ret4=jsonpath.jsonpath(info,"$.store.book[-1:].title")
print(ret4)
ret4=jsonpath.jsonpath(info,"$.store.book[(@.length-1)].title")
print(ret4)# 5. 提取价格小于10的书的标题
print('提取价格小于10的书的标题')
ret5=jsonpath.jsonpath(info,"$.store.book[?(@.price<10)].title")
print(ret5)# 6. 提取价格小于或者等于20的所有商品的价格
print('提取价格小于或者等于20的所有商品的价格')
ret6=jsonpath.jsonpath(info,"$.store.book[?(@.price<=20)].title")
print(ret6)
ret6=jsonpath.jsonpath(info,"$..[?(@.price<=20)].title")
print(ret6)# 7. 获取所有书的作者
print('获取所有书的作者')
ret7=jsonpath.jsonpath(info,"$.store.book[*].author")
print(ret7)# 8. 获取所有作者
print('获取所有作者')
ret8=jsonpath.jsonpath(info,"$..author")
print(ret8)# 9. 获取在store中的所有商品(包括书、自行车)
print('获取在store中的所有商品(包括书、自行车)')
ret9=jsonpath.jsonpath(info,"$..store")
print(ret9)# 10. 获取所有商品(包括书、自行车)的价格
print('获取所有商品(包括书、自行车)的价格')
ret10=jsonpath.jsonpath(info,"$.store..price")
print(ret10)# 11. 获取带有isbn的书
print('获取带有isbn的书')
ret11=jsonpath.jsonpath(info,"$..book[?(@.isbn)]")
print(ret11)# 12. 获取不带有isbn的书
print('获取不带有isbn的书')
ret12=jsonpath.jsonpath(info,"$..book[?(!@.isbn)]")
print(ret12)# 13. 获取价格在5~10之间的书
print('获取价格在5~10之间的书')
ret13=jsonpath.jsonpath(info,"$..book[?(@.price>=5&&@.price<=10)]")
print(ret13)# 14. 获取价格不在5~10之间的书
print('获取价格不在5~10之间的书')
ret14=jsonpath.jsonpath(info,"$..book[?(@.price<5||@.price>10)]")
print(ret14)# 15. 获取所有的元素
print('获取所有的元素')
ret15=jsonpath.jsonpath(info,"$..")
for i in ret15:print(i)

7.非结构化数据提取-bs4

7.1 bs4介绍与安装

介绍

BeautifulSoup4 简称 BS4,和使用 lxml模块一样,Beautiful Soup 也是一个HTML/XML 的解析器,主要的功能也是解析和提取 HTML/XML 数据。

Beautiful Soup 是基于 HTML DOM 的,会载入整个文档,解析整个DOM树,因此时间和内存开销都会大很多,所以性能要低于 lxml 模块。

BeautifulSoup 用来解析 HTML  比较简单,API 非常人性化,支持CSS选择器、Python标准库中的HTML 解析器,也支持 lxml 模块 的XML解析器。

安装

pip install bs4 -i https://mirrors.aliyun.com/pypi/simple

官方文档:http://beautifulsoup.readthedocs.io/zh_CN/v4.4.0
 

抓取工具速度使用
正则最快困难
BeautifulSoup最简单
lxml简单

7.2 bs4 基本使用示例

from bs4 import BeautifulSouphtml = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""# 创建Beautiful Soup对象
soup=BeautifulSoup(html,'lxml')# 格式化输出html代码
print(soup.prettify())

7.3 find_all搜索文档树中标签、内容、属性

find_all 方法中的参数:

def find_all(self,name=None,attrs={},recursive=True,string=None,limit=None,**kwargs)...

1.name 参数:

当前参数可以传递标签名称字符串,根据传递的标签名称搜索对应标签

# 1.创建soup对象
soup=BeautifulSoup(html_obj,'lxml')#2.根据标签名称搜索标签
ret_1=soup.find_all('b')
ret_2=soup.find_all('a')print(ret_1,ret_2)

除了传递标签名称字符串之外也可传递正则表达式,如果传入正则表达式作为参数,Beautiful Soup 会通过正则表达式的 match()来匹配内容。下面例子中找出所有以 b 开头的标签。

soup=BeautifulSoup(html_obj,'lxml')
for tag in soup.find_all(re.compile('^b'));print(tag.name)

如果传递是一个列表,则 Beautiful Soup 会将与 列表中任一元素匹配的内容返回。

soup=BeautifulSoup(html_obj,'lxml')
ret=soup.find_all(['a','b'])
print(ret)

2.attrs参数:可以根据标签属性搜索对应标签

from bs4 import BeautifulSouphtml = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""soup=BeautifulSoup(html,'lxml')
ret_1=soup.find_all(attrs={'class':'sister'})
print(ret_1)print('-' *30)# 简写方式
ret_2=soup.find_all(class_='sister')
print(ret_2)print('-' *30)# 查询id属性为link2的标签
ret_3=soup.find_all(id='link2')
print(ret_3)

3.string参数:通过string参数可以搜索文档中的字符串内容,与name参数的可选值一样,string参数接受字符串、正则表达式、列表

import refrom bs4 import BeautifulSouphtml = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""soup=BeautifulSoup(html,'lxml')ret_1=soup.find_all(string='Elsie')
print(ret_1)ret_2=soup.find_all(string=['Tillie','Elisie','Lacie'])
print(ret_2)ret_3=soup.find_all(string=re.compile('Dormouse'))
print(ret_3)

find_all方法的使用:

import refrom bs4 import BeautifulSouphtml = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""# 创建Beautiful Soup对象
soup=BeautifulSoup(html,'lxml')# 格式化输出html代码
# print(soup.prettify())# 一、name参数
# 1.根据标签名搜索标签
ret1=soup.find_all('a')
print(ret1)# 2.传递正则表达式
for i in soup.find_all(re.compile('^b')):print(i.name)# 3.传递一个列表
ret3=soup.find_all(['a','b'])
print(ret3)# 二、attrs参数:可以根据标签属性搜索对应标签
ret4=soup.find_all(attrs={'class':'title'})
print(ret4)
ret4=soup.find_all(class_='sister')
print(ret4)# 查询id属性为link2的标签
ret5=soup.find_all(id='link2')
print(ret5)# 三、string参数:通过string参数可以搜索文档中的字符串内容,与name参数的可选值一样,string参数接受字符串、正则表达式、列表
ret6=soup.find_all(string='Elsie')
print(ret6)ret7=soup.find_all(string=['Tillie','Elisie','Lacie'])
print(ret7)ret8=soup.find_all(string=re.compile('Dormouse'))
print(ret8)

find 方法

find 的用法与 find_all 一样,区别在于find 返回第一个符合匹配结果,find_all则返回所有匹配结果的列表。

7.4 文档搜索树中的 css 选择器

另一种与 find_all 方法有异曲同工之妙的查找方法,也是返回所有匹配结果的列表。

css 选择器编写注意事项:

  • 标签名称不加任何修饰
  • 类名前加 .
  • id属性名称前加#

css 选择器编写方式与编写 css 样式表的语法大致相同。在bs4中可以直接使用 soup.select()方法进行筛选,返回值类型是一个列表。

css选择器的使用:

from bs4 import BeautifulSoup
import rehtml = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
soup=BeautifulSoup(html,'lxml')# 1.标签选择器
print(soup.select('title'))
print(soup.select('a'))
print(soup.select('b'))
print('-'*30)# 2.类选择器
print(soup.select('.sister'))
print('-'*30)# 3.id选择器
print(soup.select('#link1'))
print('-'*30)# 4.层选择器
print(soup.select('p #link1'))
print('-'*30)# 5.属性选择器
print(soup.select('a[class="sister"]'))
print('-'*30)
print(soup.select('a[href="http://example.com/elsie"]'))
print('-'*30)# 6.get_text()方法:获取文本内容
# select返回的是列表对象,需要使用for循环遍历列表元素再使用get_text方法获取文本数据
for title in soup.select('title'):print(title.get_text())# 7.get()方法:获取属性
for attr in soup.select('a'):print(attr.get('href'))

案例:使用bs4抓取搜狗微信下的所有文章标题

https://weixin.sogou.com/weixin?_sug_type_=1&type=2&query=python

import requests
from bs4 import BeautifulSoupurl='https://weixin.sogou.com/weixin?_sug_type_=1&type=2&query=python'
headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36'}
res=requests.get(url,headers=headers)soup=BeautifulSoup(res.text,'lxml')
title_list=soup.select('div[class="txt-box"] a')
for i in title_list:title=i.get_text()print(title)

8.正则表达式

正则表达式 Regular Expression ,通常缩写为 RegExp 或 Regex,是一种用于匹配、搜索和操作文本字符串的强大工具。它是由模式 pattern 和相关的匹配规则组成的表达式。

正则表达式的模式由一系列字符和特殊字符组成,用于描述所需匹配的文本模式。它可以用于各种编辑语言和文本编辑器中,例如Python、JavaScript、Perl等。

正则表达式提供了一种灵活的方式来匹配和处理字符串。它可以用以下情况:

  1. 搜索和替换:可以使用正则表达式来搜索文本中符合特定模式的字符串,并进行替换或其他操作。
  2. 验证数据:可以使用正则表达式来验证用户输入的数据是否符合特定的格式要求,例如验证电子邮件地址、电话号码、日期等
  3. 提取信息:可以使用正则表达式从文本中提取特定的信息,例如提取URL、提取网页中的所有链接等。

正则表达式中的特殊字符和语法规则很多,包括字符类 character class、量词 quantifier、分组grouping、转义字符 escape character等。这些特殊字符和规则可以组合使用,构成复杂的匹配模式。

正则表达式在线验证工具:https://regexr-cn.com

在这个工具中我们可以快速验证自己编写的正则表达式是否存在语法错误。

8.1 正则表达式常用函数

1.re.match(pattern,string)

此函数用于检测字符串的开头位置是否与指定的模式 pattern 相匹配。如果匹配成功,则返回一个匹配对象;否则返回 None

import restring ='Hello,World!'
pattern=r'Hello'
result=re.match(pattern,string)
if result:print('匹配成功:',result.group())
else:print('匹配失败')
2.re.search(pattern,string)

该函数会在整个字符串内查找并返回第一个成功匹配模式 pattern 的结果。若匹配成功,返回一个匹配对象;若未找到匹配项,则返回 None。

import re string1='Hello,World!'
pattern1=r'World'
result1=re.search(pattern1,string1)
if result1:print('匹配成功:',result1.group())
else:print('匹配失败')
3.re.findall(pattern,string)

此函数会返回字符串中所有匹配模式 pattern 的结果列表。如果没有找到匹配项,则返回一个空列表。

import restring2="ab12cd34ef56"
pattern2=r'\d+'
result2=re.findall(pattern2,string2)
print('匹配结果:',result2)
4.re.sub(pattern,repl,string)

该函数会把字符串中所有匹配模式 pattern 的部分用指定的字符串 repl 进行替换,并返回替换后的新字符串。

import restring3='Hello,World!'
pattern3=r'World'
repl='Python'
result3=re.sub(pattern,repl,string3)
print('替换后的字符串:',result3)
5. re.split(pattern,string)

此函数根据正则表达式 pattern 对字符串进行分割,并将分割后的所有字符串放在一个列表中返回。

import restring4='apple,banana;cherry grape'
pattern4=r'[;, ]'
result4=re.split(pattern4,string4)
print('分割后的列表:',result4)

8.2 正则表达式的元字符

元字符由特殊符号组成,是正则表达式强大的关键所在。元字符可以定义字符集合、子组匹配以及模式重复次数,使得正则表达式不仅能匹配单个字符串,还能匹配字符串集合。

1.字符匹配

英文句号.

匹配除换行符 \n 之外的任意一个字符。

import restring_1='abc\nn123'
pattern_1=r'a.c'
result_1=re.findall(pattern_1,string_1)
print('匹配结果:',result_1)# 输出的结果
# 匹配结果: ['abc']

中括号 [ ]

匹配包含在中括号内部的任意一个字符。

import restring_2='abc123'
pattern_2=r'[abc]'
result_2=re.findall(pattern_2,string_2)
print('匹配结果:',result_2)# 输出结果:
#匹配结果: ['a', 'b', 'c']

管道符 |

用于在两个正则表达式之间进行或操作,只要满足其中一个模式即可匹配

import restring_3='apple banana cherry'
pattern_3=r'apple|cheery'
result_3=re.findall(pattern_3,string_3)
print('匹配结果:',result_3)# 输出结果:
# 匹配结果: ['apple']

乘方符号^

匹配字符串的起始内容

import restring_4='Hello,World!'
pattern_4=r'^Hello'
result_4=re.findall(pattern_4,string_4)
print('匹配结果:',result_4)# 输出结果:
# 匹配结果: ['Hello']

货币符号 $

匹配字符串的结束位置的内容

import restring_5='Hello,World!'
pattern_5=r'World!$'
result_5=re.findall(pattern_5,string_5)
print('匹配结果:',result_5)# 输出结果:
# 匹配结果: ['World!']

 量化符号 ?、*、+、{n}、{n,}、{m,n}
?:前面的元素是可选的,并且最多能匹配1次

import res1='color colour'
p1=r'colou?r'
r1=re.findall(p1,s1)
print('匹配结果:',r1)# 输出结果
# 匹配结果: ['color', 'colour']

*:前面的元素会被匹配0次或多次

import res2='ab abb abbb'
p2=r'ab*'
r2=re.findall(p2,s2)
print('匹配结果:',r2)# 输出结果:
# 匹配结果: ['ab', 'abb', 'abbb']

+:前面的元素会被匹配1次或多次

import res3='ab abb abbb'
p3=r'ab+'
r3=re.findall(p3,s3)
print('匹配结果:',r3)# 输出结果:
# 匹配结果: ['ab', 'abb', 'abbb']

{n}:前面的元素会正好匹配n次

import res4='123 1234 12345'
p4=r'\d{3}'
r4=re.findall(p4,s4)
print('匹配结果:',r4)# 输出结果:
# 匹配结果: ['123', '123', '123']

{n,}:前面的元素至少会被匹配n次

import res5='123 1234 12345'
p5=r'\d{3,}'
r5=re.findall(p5,s5)
print('匹配结果:',r5)# 输出结果:
# 匹配结果: ['123', '1234', '12345']

{m,n}:前面的元素至少匹配n次

import res6='12 123 1234 12345'
p6=r'\d{2,4}'
r6=re.findall(p6,s6)
print('匹配结果:',r6)# 输出结果:
# 匹配结果: ['12', '123', '1234', '1234']
2.转义字符

\w:匹配字母、数字和下划线

s_1='abc123_!@#'
p_1=r'\w'
r_1=re.findall(p_1,s_1)
print('匹配结果:',r_1)# 输出结果:
# 匹配结果: ['a', 'b', 'c', '1', '2', '3', '_']

\W:与\w相反,匹配非字母、数字和下划线的字符。

import res_2='abc123_!@#'
p_2=r'\W'
r_2=re.findall(p_2,s_2)
print('匹配结果:',r_2)# 输出结果:
# 匹配结果: ['!', '@', '#']

\s:匹配空白字符,如空格、制表符、换行符等。

import res_3='Hello World\n'
p_3=r'\s'
r_3=re.findall(p_3,s_3)
print('匹配结果:',r_3)# 输出结果:
# 匹配结果: [' ', '\n']

\S:匹配非空白字符。

import res_4='Hello World\n'
p_4=r'\S'
r_4=re.findall(p_4,s_4)
print('匹配结果:',r_4)# 输出结果:
# 匹配结果: ['H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd']

\d:匹配数字。

import res_5='abc123'
p_5=r'\d'
r_5=re.findall(p_5,s_5)
print('匹配结果:',r_5)# 输出结果:
# 匹配结果: ['1', '2', '3']

\D:匹配非数字字符

import res_6='abc123'
p_6=r'\D'
r_6=re.findall(p_6,s_6)
print('匹配结果:',r_6)# 输出结果:
# 匹配结果: ['a', 'b', 'c']

\b:匹配单词边界

import res_7='hello world'
p_7=r'\bhello\b'
r_7=re.findall(p_7,s_7)
print('匹配结果:',r_7)# 输出结果:
# 匹配结果: ['hello']

\B:匹配非单词边界

import res_8='helloworld'
p_8=r'\Bworld\B'
r_8=re.findall(p_8,s_8)
print('匹配结果:',r_8)# 输出结果:
# 匹配结果: []

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/72209.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

前端学习——HTML

VSCode常用快捷键 代码格式化&#xff1a;ShiftAltF 向上或向下移动一行&#xff1a;AltUp或AltDown 快速复制一行代码&#xff1a;ShiftAltUp或者ShiftAltDown 快速替换&#xff1a;CtrlH HTML标签 文本标签 定义着重文字 定义粗体文字 定义斜体文字 加重语气 删除字 无特…

Hadoop之02:MR-图解

1、不是所有的MR都适合combine 1.1、map端统计出了不同班级的每个学生的年龄 如&#xff1a;(class1, 14)表示class1班的一个学生的年龄是14岁。 第一个map任务&#xff1a; class1 14 class1 15 class1 16 class2 10第二个map任务&#xff1a; class1 16 class2 10 class…

Redis的持久化-RDBAOF

文章目录 一、 RDB1. 触发机制2. 流程说明3. RDB 文件的处理4. RDB 的优缺点 二、AOF1. 使用 AOF2. 命令写⼊3. 文件同步4. 重写机制5 启动时数据恢复 一、 RDB RDB 持久化是把当前进程数据生成快照保存到硬盘的过程&#xff0c;触发 RDB 持久化过程分为手动触发和自动触发。 …

CCF-CSP认证 202104-1灰度直方图

题目描述 思路 首先输入矩阵长度、矩阵宽度和灰度范围&#xff0c;结果数组长度可固定&#xff0c;其中的元素要初始化为0。在输入灰度值的时候&#xff0c;结果数组中以该灰度值为索引的元素值1&#xff0c;即可统计每个灰度值的数量。 代码 C版&#xff1a; #include <…

水果识别系统 | BP神经网络水果识别系统,含GUI界面(Matlab)

使用说明 代码下载&#xff1a;BP神经网络水果识别系统&#xff0c;含GUI界面&#xff08;Matlab&#xff09; BP神经网络水果识别系统 一、引言 1.1、研究背景及意义 在当今科技迅速发展的背景下&#xff0c;人工智能技术尤其是在图像识别领域的应用日益广泛。水果识别作为…

如何在网页上显示3D CAD PMI

在现代制造业中&#xff0c;3D CAD模型已成为产品设计和制造的核心。为了更有效地传达设计意图和制造信息&#xff0c;产品和制造信息&#xff08;PMI&#xff09;被嵌入到3D模型中。然而&#xff0c;如何在网页上清晰、准确地显示这些3D CAD PMI&#xff0c;成为了一个重要的技…

java后端开发day24--阶段项目(一)

&#xff08;以下内容全部来自上述课程&#xff09; GUI&#xff1a;Graphical User Interface 图形用户接口&#xff0c;采取图形化的方式显示操作界面 分为两套体系&#xff1a;AWT包&#xff08;有兼容问题&#xff09;和Swing包&#xff08;常用&#xff09; 拼图小游戏…

vmware安装firepower ftd和fmc

在vmware虚拟机中安装cisco firepower下一代防火墙firepower threat defence&#xff08;ftd&#xff09;和管理中心firepower management center&#xff08;fmc&#xff09;。 由于没有cisco官网下载账号&#xff0c;无法下载其中镜像。使用eveng模拟器中的ftd和fmc虚拟镜像…

DeepSeek 助力 Vue3 开发:打造丝滑的弹性布局(Flexbox)

前言&#xff1a;哈喽&#xff0c;大家好&#xff0c;今天给大家分享一篇文章&#xff01;并提供具体代码帮助大家深入理解&#xff0c;彻底掌握&#xff01;创作不易&#xff0c;如果能帮助到大家或者给大家一些灵感和启发&#xff0c;欢迎收藏关注哦 &#x1f495; 目录 Deep…

DeepSeek开源周Day5压轴登场:3FS与Smallpond,能否终结AI数据瓶颈之争?

2025年2月28日&#xff0c;DeepSeek开源周迎来了第五天&#xff0c;也是本次活动的收官之日。自2月24日启动以来&#xff0c;DeepSeek团队以每天一个开源项目的节奏&#xff0c;陆续向全球开发者展示了他们在人工智能基础设施领域的最新成果。今天&#xff0c;他们发布了Fire-F…

SQL AnyWhere 的备份与恢复

目录 一、备份 二、恢复 1、自动恢复 2、映像恢复 3、日志恢复-指定时间点 4、日志恢复-指定偏移 5、完整的恢复流程 6、恢复最佳实践 三、其他操作 1、dbtran 2、SQL Shell 工具 数据库的安装与基本使用内容请参考博客: SAP SQLAnyWhere 17 的安装与基本使用_sql…

入门基础项目(SpringBoot+Vue)

文章目录 1. css布局相关2. JS3. Vue 脚手架搭建4. ElementUI4.1 引入ElementUI4.2 首页4.2.1 整体框架4.2.2 Aside-logo4.2.3 Aside-菜单4.2.4 Header-左侧4.2.5 Header-右侧4.2.6 iconfont 自定义图标4.2.7 完整代码 4.3 封装前后端交互工具 axios4.3.1 安装 axios4.3.2 /src…

unity学习61:UI布局layout

目录 1 布局 layout 1.1 先准备测试UI,新增这样一组 panel 和 image 1.2 新增 vertical layout 1.3 现在移动任意一个image 都会影响其他 1.3.1 对比 如果没有这个&#xff0c;就会是覆盖效果了 1.3.2 对比 如果没有这个&#xff0c;就会是覆盖效果了 1.4 总结&#xf…

翻译: 深入分析LLMs like ChatGPT 一

大家好&#xff0c;我想做这个视频已经有一段时间了。这是一个全面但面向普通观众的介绍&#xff0c;介绍像ChatGPT这样的大型语言模型。我希望通过这个视频让大家对这种工具的工作原理有一些概念性的理解。 首先&#xff0c;我们来谈谈你在这个文本框里输入内容并点击回车后背…

Python 实战:构建分布式文件存储系统全解析

Python 实战&#xff1a;构建分布式文件存储系统全解析 在当今数据爆炸的时代&#xff0c;分布式文件存储系统凭借其高可扩展性、高可靠性等优势&#xff0c;成为了数据存储领域的热门选择。本文将详细介绍如何使用 Python 构建一个简单的分布式文件存储系统。从系统架构设计&…

tableau之标靶图、甘特图和瀑布图

一、标靶图 概念 标靶图&#xff08;Bullet Chart&#xff09;是一种用于显示数据与目标之间关系的可视化图表&#xff0c;常用于业务和管理报告中。其设计旨在用来比较实际值与目标值&#xff0c;同时展示额外的上下文信息&#xff08;如趋势&#xff09;。 作用 可视化目标…

Linux下的网络通信编程

在不同主机之间&#xff0c;进行进程间的通信。 1解决主机之间硬件的互通 2.解决主机之间软件的互通. 3.IP地址&#xff1a;来区分不同的主机&#xff08;软件地址&#xff09; 4.MAC地址&#xff1a;硬件地址 5.端口号&#xff1a;区分同一主机上的不同应用进程 网络协议…

530 Login fail. A secure connection is requiered(such as ssl)-java发送QQ邮箱(简单配置)

由于cs的csdN许多文章关于这方面的都是vip文章&#xff0c;而本文是免费的&#xff0c;希望广大网友觉得有帮助的可以多点赞和关注&#xff01; QQ邮箱授权码到这里去开启 授权码是16位的字母&#xff0c;填入下面的mail.setting里面的pass里面 # 邮件服务器的SMTP地址 host…

SOME/IP-SD -- 协议英文原文讲解6

前言 SOME/IP协议越来越多的用于汽车电子行业中&#xff0c;关于协议详细完全的中文资料却没有&#xff0c;所以我将结合工作经验并对照英文原版协议做一系列的文章。基本分三大块&#xff1a; 1. SOME/IP协议讲解 2. SOME/IP-SD协议讲解 3. python/C举例调试讲解 5.1.3.1 E…

【Linux】learning notes(3)make、copy、move、remove

文章目录 1、mkdir &#xff08;make directory&#xff09;2、rmdir &#xff08;remove directory&#xff09;3、rm&#xff08;remove&#xff09;4、>5、touch 新建文件6、mv&#xff08;move&#xff09;7、cp&#xff08;copy&#xff09; 1、mkdir &#xff08;make…