没有合适的资源?快使用搜索试试~ 我知道了~
数据爬取_正则解析1
需积分: 0 0 下载量 137 浏览量
2022-08-03
15:40:54
上传
评论
收藏 1.08MB PDF 举报
温馨提示
试读
18页
第一页 url: https://www.neihan8.com/article/index.html 第二页 url: https://www.neihan8
资源详情
资源评论
资源推荐
python 之――――用正则表达式提取数据
1 / 18 奇酷学院高级讲师:郭建涛
用正则表达式提取数据
正则表达式,又称规则表达式,通常被用来检索、替换那些符合某个模式(规则)的文本。
复习回顾
单字符匹配,多字符匹配,匹配分组,
对文本进行匹配查找的一系列方法
match 方法:从起始位置开始查找,一次匹配
search 方法:从任何位置开始查找,一次匹配
findall 方法:全部匹配,返回列表
finditer 方法:全部匹配,返回迭代器
split 方法:分割字符串,返回列表
sub 方法:替换
python 之――――用正则表达式提取数据
2 / 18 奇酷学院高级讲师:郭建涛
数据提取中常用的操作
抓取标签间的内容
案例:抓取 title 标签间的内容
from urllib import request
import re
import chardet
def down(url):
head = {}
#写入 User Agent 信息
head['User-Agent'] = 'Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D)
AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19'
#创建 Request 对象
req = request.Request(url, headers=head)
response = request.urlopen(req)
html = response.read()
charset = chardet.detect(html)
#print(charset)
#print(charset['encoding'])
html = html.decode(charset['encoding'])
return html
if __name__ == "__main__":
#print('hello')
html = down("https://fanyi.baidu.com/")
pat = r'<title>(.*?)</title>'
ex = re.compile(pat, re.M|re.S) #只取中间的文字
obj = re.search(ex, html)
title = obj.group(1)
print(title)
案例:抓取超链接标签间的内容
from urllib import request
import re
python 之――――用正则表达式提取数据
3 / 18 奇酷学院高级讲师:郭建涛
import chardet
def down(url):
head = {}
#写入 User Agent 信息
head['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
#创建 Request 对象
req = request.Request(url, headers=head)
response = request.urlopen(req)
html = response.read()
charset = chardet.detect(html)
#print(charset)
print(charset['encoding'])
html = html.decode(charset['encoding'])
return html
if __name__ == "__main__":
# 获取超链接<a>和</a>之间内容
url = "https://www.baidu.com/"
html = down(url)
print(html)
pat = re.compile(r'<a .*?>(.*?)</a>',re.M|re.S)
texts = pat.findall(html)
for t in texts:
print(t)
案例:抓取 tr\td 标签间的内容
if __name__ == "__main__":
content = '''
<html>
<head><title>表格</title></head>
<body>
<table border=1>
<tr><th>学号</th><th>姓名</th></tr>
<tr><td>1001</td><td>李白</td></tr>
<tr><td>1002</td><td>杜甫</td></tr>
</table>
</body>
</html>
python 之――――用正则表达式提取数据
4 / 18 奇酷学院高级讲师:郭建涛
'''
# 获取<tr></tr>间内容
res = r'<tr>(.*?)</tr>'
texts = re.findall(res, content, re.S | re.M)
for m in texts:
print(m)
# 获取<th></th>间内容
for m in texts:
res_th = r'<th>(.*?)</th>'
m_th = re.findall(res_th, m, re.S | re.M)
for t in m_th:
print(t)
# 直接获取<td></td>间内容
res = r'<td>(.*?)</td><td>(.*?)</td>'
texts = re.findall(res, content, re.S | re.M)
for m in texts:
print(m[0], m[1])
抓取标签中的参数
案例:抓取超链接标签的 URL
import re
content = '''
<a href="http://news.baidu.com" name="tj_trnews" class="mnav">新闻</a>
<a href="http://www.hao123.com" name="tj_trhao123" class="mnav">hao123</a>
<a href="http://map.baidu.com" name="tj_trmap" class="mnav">地图</a>
<a href="http://v.baidu.com" name="tj_trvideo" class="mnav">视频</a>
'''
res = r'href="(.+?)"'
urls = re.findall(res, content, re.I | re.S | re.M)
for url in urls:
print(url)
剩余17页未读,继续阅读
赶路的稻草人
- 粉丝: 24
- 资源: 330
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0