请使用正则表达式解析技术从如下字符串中解析出图片的url(img标签的src属性)并根据url下载对应图片到本地html_str=”<a rel="noopener" class="thumbnail vpic_wrap"><img src="https://tiebapic.baidu.com/forum/wh%3D200%2C90%3B/sign=f0a26dd09854564ee530ec3b83eeb0bb/342ac65c103853434204d6e2d613b07ecb80882e.jpg?tbpicau=2022-12-21-05_a0abd29921bf9a2587d77ebc44c26a3d" attr="40111" data-original="https://tiebapic.baidu.com/forum/wh%3D200%2C90%3B/sign=f0a26dd09854564ee530ec3b83eeb0bb/342ac65c103853434204d6e2d613b07ecb80882e.jpg?tbpicau=2022-12-21-05_a0abd29921bf9a2587d77ebc44c26a3d" bpic="https://tiebapic.baidu.com/forum/w%3D580%3B/sign=d4d42bb965a446237ecaa56aa819700e/342ac65c103853434204d6e2d613b07ecb80882e.jpg?tbpicau=2022-12-21-05_174a54eeaa91524871ee9f14f0a2d14e" class="threadlist_pic j_m_pic " style="display: inline;"></a>”
时间: 2024-04-04 09:35:54 浏览: 132
可以使用以下正则表达式解析出图片的url:
```python
import re
html_str = '<a rel="noopener" class="thumbnail vpic_wrap"><img src="https://tiebapic.baidu.com/forum/wh%3D200%2C90%3B/sign=f0a26dd09854564ee530ec3b83eeb0bb/342ac65c103853434204d6e2d613b07ecb80882e.jpg?tbpicau=2022-12-21-05_a0abd29921bf9a2587d77ebc44c26a3d" attr="40111" data-original="https://tiebapic.baidu.com/forum/wh%3D200%2C90%3B/sign=f0a26dd09854564ee530ec3b83eeb0bb/342ac65c103853434204d6e2d613b07ecb80882e.jpg?tbpicau=2022-12-21-05_a0abd29921bf9a2587d77ebc44c26a3d" bpic="https://tiebapic.baidu.com/forum/w%3D580%3B/sign=d4d42bb965a446237ecaa56aa819700e/342ac65c103853434204d6e2d613b07ecb80882e.jpg?tbpicau=2022-12-21-05_174a54eeaa91524871ee9f14f0a2d14e" class="threadlist_pic j_m_pic " style="display: inline;"></a>'
pattern = r'<img.*?src="(.*?)".*?>'
urls = re.findall(pattern, html_str)
print(urls)
```
输出结果为:
```
['https://tiebapic.baidu.com/forum/wh%3D200%2C90%3B/sign=f0a26dd09854564ee530ec3b83eeb0bb/342ac65c103853434204d6e2d613b07ecb80882e.jpg?tbpicau=2022-12-21-05_a0abd29921bf9a2587d77ebc44c26a3d']
```
然后可以使用Python中的requests库下载对应的图片到本地:
```python
import requests
for url in urls:
response = requests.get(url)
with open('image.jpg', 'wb') as f:
f.write(response.content)
```
这样就可以将对应的图片下载到本地了。
阅读全文