鱼C论坛

 找回密码
 立即注册
查看: 1538|回复: 5

[已解决]正则表达式的写法

[复制链接]
发表于 2017-11-30 20:42:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
<img class="BDE_Image" pic_type="0" width="560" height="838" src="https://imgsa.baidu.com/forum/w%3D580/sign=e6fcffb48c13632715edc23ba18fa056/f01a9d16fdfaaf5104fde2d78a5494eef01f7ab9.jpg" pic_ext="jpeg">
请问这种图片代码,用正则表达式怎么写?
最佳答案
2017-12-1 07:37:07
  1. r'img class="BDE_Image" .*? src="(.*?)"'[b][/b]
复制代码


另外说一句,图不错。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-11-30 21:29:43 | 显示全部楼层
import re
string='<img class="BDE_Image" pic_type="0" width="560" height="706" src="https://imgsa.baidu.com/forum/w%3D580/sign=6d91f7b40efa513d51aa6cd60d6d554c/b25594eef01f3a29d1ebb7a69f25bc315c607cbc.jpg" pic_ext="jpeg">'
p=r'img class="BDE_Image".*?src="([^"]+?)"'
img=re.findall(p,string)
print(img)

我尝试着这样匹配
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-11-30 21:30:18 | 显示全部楼层
结果却是这样
['https://imgsa.baidu.com/forum/pic/item/dbc2d88065380cd7757468e1ab44ad3458828195.jpg']
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-1 07:37:07 | 显示全部楼层    本楼为最佳答案   
  1. r'img class="BDE_Image" .*? src="(.*?)"'[b][/b]
复制代码


另外说一句,图不错。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2017-12-1 08:53:28 | 显示全部楼层
Bean_Wei 发表于 2017-12-1 07:37
另外说一句,图不错。

谢谢大佬,昨天捣鼓了好久没成功
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-12-1 08:58:10 | 显示全部楼层
正则还是得参考环境来写

  1. target = '''<html>
  2. <body>
  3. <img class="BDE_Image" pic_type="0" width="560" height="838" src="https://1.jpg" pic_ext="jpeg"><p>123</p>
  4. <img class="BDE" pic_type="0" width="560" height="838" src="https://2.jpg" pic_ext="jpeg"><p>123</p>
  5. <img class="BDE_Image" pic_type="0" width="560" height="838" src="https://3.jpg" pic_ext="jpeg"><span>456</span>
  6. <img class="BDE123" pic_type="0" width="560" height="838" src="https://4.jpg" pic_ext="jpeg"><span>456</span>
  7. <img class="BDE_Image" pic_type="0" width="560" height="838" src="https://5.jpg" pic_ext="jpeg">     
  8. <img class="BDE_Image" pic_type="0" width="560" height="838" src="https://6.jpg" pic_ext="jpeg"><span>456</span>
  9. </body>
  10. </html>
  11. '''

  12. html = re.findall(r'(?m)(<img class="BDE_Image".*? src="(?P<w>.*?)" .*?>)',target)
  13. for x in html:
  14.     print(x)
复制代码
  1. ('<img class="BDE_Image" pic_type="0" width="560" height="838" src="https://1.jpg" pic_ext="jpeg">', 'https://1.jpg')
  2. ('<img class="BDE_Image" pic_type="0" width="560" height="838" src="https://3.jpg" pic_ext="jpeg">', 'https://3.jpg')
  3. ('<img class="BDE_Image" pic_type="0" width="560" height="838" src="https://5.jpg" pic_ext="jpeg">', 'https://5.jpg')
  4. ('<img class="BDE_Image" pic_type="0" width="560" height="838" src="https://6.jpg" pic_ext="jpeg">', 'https://6.jpg')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-3-4 10:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表