Python爬虫之使用正则表达式抓取数据

目录

匹配标签

匹配title标签

a标签

table标签

匹配标签里面的属性

匹配a标签里面的URL

匹配img标签里的 src


相关文章:Linux中的正则表达式

                 Python中的正则表达式

实例:

匹配标签

匹配title标签

匹配网页的 <title></title> 标签,也就是网页的标题。  .*? 就是匹配1个或多个字符,也就是这里不能是空的。当加入括号的话,就是代表取值了 (.*?)

import re
import requests

resp=requests.get("http://www.baidu.com")
resp.encoding="utf-8"  #设置编码格式为utf-8
html=resp.text   
title=re.findall(r'<title>.*?</title>',html)  #匹配 <title></title>
for t in title:
    print(t)
title_value=re.findall(r'<title>(.*?)</title>',html)  #匹配 <title></title>里面的内容
for t in title_value:
    print(t)
#####################################################################
<title>百度一下,你就知道</title>
百度一下,你就知道

a标签

匹配<a  href="" ></a> ,并且获取a标签里面的内容

import re
import requests

resp=requests.get("http://www.baidu.com")
resp.encoding="utf-8"  #设置编码格式为utf-8
html=resp.text  
 
urls = re.findall(r"<a.*?>.*?<\/a>", html)   #匹配所有的a标签
for u in urls:
    print(u)
 
texts = re.findall(r"<a.*?>(.*?)</a>", html)   #获取超链接<a>和</a>之间内容
for t in texts:
    print(t)
#######################################################################################
<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>
<a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a>
<a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a>
<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>
<a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a>
<a href=http://home.baidu.com>关于百度</a>
<a href=http://ir.baidu.com>About Baidu</a>
<a href=http://www.baidu.com/duty/>使用百度前必读</a>
<a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>
新闻
hao123
地图
视频
贴吧
登录
登录
更多产品
关于百度
About Baidu
使用百度前必读
意见反馈

table标签

抓取 <table></table> 表格中的内容。

假设现在有这么一个网页

<html>
<table class="table">    
            <tr>
                <th>姓名</th>
                <th>性别</th>
            </tr>
            <tr>
                <td>小谢</td>
                <td>男</td>
            </tr>
            <tr>
                <td>小红</td>
                <td>女</td>
            </tr>
</table>
</html>

匹配代码

import re
import requests

resp=requests.get("http://127.0.0.1/1.html")
resp.encoding="utf-8"  #设置编码格式为utf-8
html=resp.text  

#匹配table标签
tables=re.findall(r"<table.*?>.*?<\/table>",html,re.M|re.S)
for table in tables:
    print(table)

#匹配<tr></tr>之间的内容
trs=re.findall(r"<tr>(.*?)</tr>",html,re.S|re.M) #因为<tr>标签大多数不是在同一行,所以要加 re.S和re.M多行匹配
for tr in trs:
    print(tr)

#匹配<th></th>之间的内容
for row in trs:
    ths=re.findall(r"<th>(.*?)</th>",row,re.S|re.M)
    for th in ths:
        print(th)
        
#匹配<td></td>之间的内容
for row in trs:
    tds=re.findall(r"<td>(.*?)</td>",row,re.S|re.M)
    for td in tds:
        print(td)
##################################################################################
<table class="table">    
            <tr>
                <th>姓名</th>
                <th>性别</th>
            </tr>
            <tr>
                <td><B>小谢</B></td>
                <td>男<br/></td>
            </tr>
            <tr>
                <td><B>小红</B></td>
                <td>女<br/></td>
            </tr>
</table>

                <th>姓名</th>
                <th>性别</th>
            

                <td>小谢</td>
                <td>男</td>
            

                <td>小红</td>
                <td>女</td>
            
姓名
性别

小谢
男
小红
女

匹配标签里面的属性

匹配a标签里面的URL

假如现在有网页

<html>
	<a href="http://www.baidu.com">百度一下,你就知道</a>
	<a href="http://www.mi.com">小米官网</a>
</html>
import re
import requests

resp=requests.get("http://127.0.0.1/1.html")
resp.encoding="utf-8"  #设置编码格式为utf-8
html=resp.text  

urls=re.findall(r"(?<=href=\").+?(?=\")|(?<=href=\').+?(?=\')",html,re.I|re.S|re.M)  #匹配 href=""
for url in urls:
    print(url)
###################################################################################
http://www.baidu.com
http://www.mi.com

匹配img标签里的 src

加入现在有网页

<html>
	<img src="http://t1.27270.com/uploads/tu/201811/310/f3e9db6b68.jpg"  name="美女"/>
	<img src="http://t1.27270.com/uploads/tu/201811/229/ea7fda100e.jpg" />
</html>

匹配代码:

import re
import requests
resp=requests.get("http://127.0.0.1/1.html")
resp.encoding="utf-8"  #设置编码格式为utf-8
html=resp.text  

srcs=re.findall(r'src="(.*?)"',html,re.I|re.S|re.M)
for src in srcs:
    print(src)
##################################################################
http://t1.27270.com/uploads/tu/201811/310/f3e9db6b68.jpg
http://t1.27270.com/uploads/tu/201811/229/ea7fda100e.jpg


#假如要获取图片的名字,也就是上面的 f3e9db6b68.jpg 或者  ea7fda100e.jpg

import re
import requests
resp=requests.get("http://127.0.0.1/1.html")
resp.encoding="utf-8"  #设置编码格式为utf-8
html=resp.text  

srcs=re.findall(r'src="(.*?)"',html,re.I|re.S|re.M)
for src in srcs:
    name=src.split("/")[-1]
    print(name)
##################################################################
f3e9db6b68.jpg
ea7fda100e.jpg

 

  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Python 爬虫中,正则表达式是非常常用的工具。它可以帮助我们从网页中抓取需要的信息,过滤掉不需要的内容,提高爬取效率和准确性。以下是一些常用的正则表达式应用场景: 1. 匹配 URL 链接:可以使用正则表达式提取出页面中的所有链接,便于进一步抓取和分析页面内容。 2. 匹配特定文本:如果我们只需要网页中某个特定文本,可以使用正则表达式来匹配并提取。 3. 过滤 HTML 标签:网页中的 HTML 标签对于我们的数据分析来说是无用的,可以使用正则表达式去除标签。 4. 匹配时间日期:有些网站会包含时间日期等信息,可以使用正则表达式来提取这些信息。 Python 中可以使用 re 模块来实现正则表达式的应用。具体使用方法可以参考以下示例代码: ```python import re import requests # 抓取网页内容 r = requests.get('https://www.example.com') html = r.content.decode() # 匹配 URL 链接 urls = re.findall('<a href="(.*?)".*?>.*?</a>', html) print(urls) # 匹配特定文本 text = re.search('<title>(.*?)</title>', html).group(1) print(text) # 过滤 HTML 标签 clean = re.compile('<.*?>') content = re.sub(clean, '', html) print(content) # 匹配时间日期 dates = re.findall('\d{4}-\d{2}-\d{2}', html) print(dates) ``` 上述代码演示了如何使用正则表达式抓取网页内容并进行一些简单的数据处理。需要注意的是,在实际应用中,正则表达式应该根据具体情况进行调整和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谢公子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值