<li class="new-works-box fl"> <a class="relative block works-detail hover-none new-works-img-box" title="湖洞水风光" hidefocus="true" href="//www.nipic.com/show/43693997.html"><span class="for-vertical"></span><span><img src="//static.ntimg.cn/original/images/grey.gif" data-src="//pic.nximg.cn/pic/20230525/9446643_143102439100_4.jpg" alt="湖洞水风光"/></span></a> <div class="new-search-works-info"> <a href="//www.nipic.com/show/43693997.html" class="search-works-name ellipsis search-left-box" title="湖洞水风光">湖洞水风光</a> <span class="search-works-price search-right-box">共享分:<span class="red">100</span></span> </div> </li> 爬虫 提取出这段文本中的title和data-src
时间: 2023-07-19 17:23:52 浏览: 156
在vue中使用v-bind:class的选项卡方法
可以使用正则表达式来提取出title和data-src,示例代码如下:
```python
import re
text = '<li class="new-works-box fl"> <a class="relative block works-detail hover-none new-works-img-box" title="湖洞水风光" hidefocus="true" href="//www.nipic.com/show/43693997.html"><span class="for-vertical"></span><span><img src="//static.ntimg.cn/original/images/grey.gif" data-src="//pic.nximg.cn/pic/20230525/9446643_143102439100_4.jpg" alt="湖洞水风光"/></span></a> <div class="new-search-works-info"> <a href="//www.nipic.com/show/43693997.html" class="search-works-name ellipsis search-left-box" title="湖洞水风光">湖洞水风光</a> <span class="search-works-price search-right-box">共享分:<span class="red">100</span></span> </div> </li>'
title_pattern = re.compile(r'title="([^"]*)"')
data_src_pattern = re.compile(r'data-src="([^"]*)"')
title_match = title_pattern.search(text)
data_src_match = data_src_pattern.search(text)
if title_match:
title = title_match.group(1)
print(title)
if data_src_match:
data_src = data_src_match.group(1)
print(data_src)
```
输出结果:
```
湖洞水风光
//pic.nximg.cn/pic/20230525/9446643_143102439100_4.jpg
```
阅读全文