https://www.nipic.com/
时间: 2023-08-05 11:04:36 浏览: 98
如果你想编写一个用于匹配 `https://www.nipic.com/` 这个网址的正则表达式,可以使用以下模式:
```
^https:\/\/www\.nipic\.com\/$
```
这个正则表达式的含义是:
- `^` 表示匹配字符串的开头
- `https:\/\/` 表示精确匹配 "https://" 这个协议部分
- `www\.nipic\.com` 表示精确匹配 "www.nipic.com" 这个域名部分,使用 `\.` 来转义匹配点号(.)
- `\/` 表示精确匹配 "/" 这个路径部分
- `$` 表示匹配字符串的结尾
这个正则表达式只能精确匹配 `https://www.nipic.com/` 这个网址,如果你需要匹配更多的变种或者特定规则的网址,可能需要调整和扩展这个正则表达式。请注意,正则表达式只是一种模式匹配工具,对于复杂的网址规则,也可以考虑使用其他方法来处理。
相关问题
<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
可以使用正则表达式来提取出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
```
python爬虫图片代码
以下是一个Python爬虫图片的代码示例:
```python
import requests
import re
import urllib
from io import BytesIO
from PIL import Image
def get_pictures(url):
data = requests.get(url).text
regex = r'<img src="(.*?.jpg)"'
pa = re.compile(regex)
images = re.findall(pa, data)
i = 0
print('*****Starting*****')
for image in images:
if '" data-original="' in image:
address = image.split('" data-original="')[1]
else:
address = image
i += 1
image = requests.get(address)
image = Image.open(BytesIO(image.content))
image.save('D:\\111\\' + str(i) + '.jpg')
print("*****Ending*****")
if __name__ == '__main__':
url = "http://soso.nipic.com/q_中国交通and隧道景观_g_0.html"
get_pictures(url)
print("OK!!!")
```
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)