派大星敲代码 2022-02-18 01:14 采纳率: 75%
浏览 47
已结题

python 正则匹配 所有符合条件的url

问题遇到的现象和发生背景

我想获取这串json字符串中所有视频url .mp4结尾的
但是它把所有url都返回出来了

问题相关代码,请勿粘贴截图

img

img

运行结果及报错内容
import re


class test():
    pass

url = '"url":"https://www.lego.com/cdn/cs/set/assets/blt264f911195f6cdef/42125_alt11.jpg?fit=bounds&format=jpg&quality=80&width=528&height=528&dpr=1","thumbnailUrl":"https://www.lego.com/cdn/cs/set/assets/blt264f911195f6cdef/42125_alt11.jpg?fit=bounds&format=jpg&quality=80&width=65&height=45&dpr=1","highResUrl":"https://www.lego.com/cdn/cs/set/assets/blt264f911195f6cdef/42125_alt11.jpg?fit=bounds&format=jpg&quality=80&width=1056&height=1056&dpr=1","fullscreenUrl":"https://www.lego.com/cdn/cs/set/assets/blt264f911195f6cdef/42125_alt11.jpg?fit=bounds&format=jpg&quality=80&width=792&height=792&dpr=1","__typename":"ProductImageUrls"},"$ProductImage:blt264f911195f6cdef.sizes.tablet":{"url":"https://www.lego.com/cdn/cs/set/assets/blt264f911195f6cdef/42125_alt11.jpg?fit=bounds&format=jpg&quality=80&width=640&height=640&dpr=1","thumbnailUrl":"https://www.lego.com/cdn/cs/set/assets/blt264f911195f6cdef/42125_alt11.jpg?fit=bounds&format=jpg&quality=80&width=65&height=45&dpr=1","highResUrl":"https://www.lego.com/cdn/cs/set/assets/blt264f911195f6cdef/42125_alt11.jpg?fit=bounds&format=jpg&quality=80&width=1280&height=1280&dpr=1","fullscreenUrl":"https://www.lego.com/cdn/cs/set/assets/blt264f911195f6cdef/42125_alt11.jpg?fit=bounds&format=jpg&quality=80&width=960&height=960&dpr=1","__typename":"ProductImageUrls"},"$ProductImage:blt5d1d4aeff01fcb63.sizes.desktop":{"url":"https://www.lego.com/cdn/cs/set/assets/blt5d1d4aeff01fcb63/42125_alt12.jpg?fit=bounds&format=jpg&quality=80&width=800&height=800&dpr=1","thumbnailUrl":"https://www.lego.com/cdn/cs/set/assets/blt5d1d4aeff01fcb63/42125_alt12.jpg?fit=bounds&format=jpg&quality=80&width=65&height=45&dpr=1","highResUrl":"https://www.lego.com/cdn/cs/set/assets/blt5d1d4aeff01fcb63/42125_alt12.jpg?fit=bounds&format=jpg&quality=80&width=1600&height=1600&dpr=1","fullscreenUrl":"https://www.lego.com/cdn/cs/set/assets/blt5d1d4aeff01fcb63/42125_alt12.jpg?fit=bounds&format=jpg&quality=80&width=1200&height=1200&dpr=1","__typename":"ProductImageUrls"},"$ProductImage:blt5d1d4aeff01fcb63.sizes":{"desktop":{"type":"id","generated":true,"id":"$ProductImage:blt5d1d4aeff01fcb63.sizes.desktop","typename":"ProductImageUrls"},"mobile":{"type":"id","generated":true,"id":"$ProductImage:blt5d1d4aeff01fcb63.sizes.mobile","typename":"ProductImageUrls"},"tablet":{"type":"id","generated":true,"id":"$ProductImage:blt5d1d4aeff01fcb63.sizes.tablet","typename":"ProductImageUrls"},"__typename":"ProductImageSizes"},"$ProductImage:blt5d1d4aeff01fcb63.sizes.mobile":{"url":"https://www.lego.com/cdn/cs/set/assets/blt5d1d4aeff01fcb63/42125_alt12.jpg?fit=bounds&format=jpg&quality=80&width=528&height=528&dpr=1","thumbnailUrl":"https://www.lego.com/cdn/cs/set/assets/blt5d1d4aeff01fcb63/42125_alt12.jpg?fit=bounds&format=jpg&quality=80&width=65&height=45&dpr=1","highResUrl":"https://www.lego.com/cdn/cs/set/assets/blt5d1d4aeff01fcb63/42125_alt12.jpg?fit=bounds&format=jpg&quality=80&width=1056&height=1056&dpr=1","fullscreenUrl":"https://www.lego.com/cdn/cs/set/assets/blt5d1d4aeff01fcb63/42125_alt12.jpg?fit=bounds&format=jpg&quality=80&width=792&height=792&dpr=1","__typename":"ProductImageUrls"},"$ProductImage:blt5d1d4aeff01fcb63.sizes.tablet":{"url":"https://www.lego.com/cdn/cs/set/assets/blt5d1d4aeff01fcb63/42125_alt12.jpg?fit=bounds&format=jpg&quality=80&width=640&height=640&dpr=1","thumbnailUrl":"https://www.lego.com/cdn/cs/set/assets/blt5d1d4aeff01fcb63/42125_alt12.jpg?fit=bounds&format=jpg&quality=80&width=65&height=45&dpr=1","highResUrl":"https://www.lego.com/cdn/cs/set/assets/blt5d1d4aeff01fcb63/42125_alt12.jpg?fit=bounds&format=jpg&quality=80&width=1280&height=1280&dpr=1","fullscreenUrl":"https://www.lego.com/cdn/cs/set/assets/blt5d1d4aeff01fcb63/42125_alt12.jpg?fit=bounds&format=jpg&quality=80&width=960&height=960&dpr=1","__typename":"ProductImageUrls"},"$ProductImage:blt3126f56afab0d3f5.sizes.desktop":{"url":"https://www.lego.com/cdn/cs/set/assets/blt3126f56afab0d3f5/42125_alt13.jpg?fit=bounds&format=jpg&quality=80&width=800&height=800&dpr=1","thumbnailUrl":"https://www.lego.com/cdn/cs/set/assets/blt3126f56afab0d3f5/42125_alt13.jpg?fit=bounds&format=jpg&quality=80&width=65&height=45&dpr=1","highResUrl":"https://www.lego.com/cdn/cs/set/assets/blt3126f56afab0d3f5/42125_alt13.jpg?fit=bounds&format=jpg&quality=80&width=1600&height=1600&dpr=1","fullscreenUrl":"https://www.lego.com/cdn/cs/set/assets/blt3126f56afab0d3f5/42125_alt13.jpg?fit=bounds&format=jpg&quality=80&width=1200&height=1200&dpr=1","__typename":"ProductImageUrls"},"$ProductImage:blt3126f56afab0d3f5.sizes":{"desktop":{"type":"id","generated":true,"id":"$ProductImage:blt3126f56afab0d3f5.sizes.desktop","typename":"ProductImageUrls"},"mobile":{"type":"id","generated":true,"id":"$ProductImage:blt3126f56afab0d3f5.sizes.mobile","typename":"ProductImageUrls"},"tablet":{"type":"id","generated":true,"id":"$ProductImage:blt3126f56afab0d3f5.sizes.tablet","typename":"ProductImageUrls"},"__typename":"ProductImageSizes"},"$ProductImage:blt3126f56afab0d3f5.sizes.mobile":{"url":"https://www.lego.com/cdn/cs/set/assets/blt3126f56afab0d3f5/42125_alt13.jpg?fit=bounds&format=jpg&quality=80&width=528&height=528&dpr=1","thumbnailUrl":"https://www.lego.com/cdn/cs/set/assets/blt3126f56afab0d3f5/42125_alt13.jpg?fit=bounds&format=jpg&quality=80&width=65&height=45&dpr=1","highResUrl":"https://www.lego.com/cdn/cs/set/assets/blt3126f56afab0d3f5/42125_alt13.jpg?fit=bounds&format=jpg&quality=80&width=1056&height=1056&dpr=1","fullscreenUrl":"https://www.lego.com/cdn/cs/set/assets/blt3126f56afab0d3f5/42125_alt13.jpg?fit=bounds&format=jpg&quality=80&width=792&height=792&dpr=1","__typename":"ProductImageUrls"},"$ProductImage:blt3126f56afab0d3f5.sizes.tablet":{"url":"https://www.lego.com/cdn/cs/set/assets/blt3126f56afab0d3f5/42125_alt13.jpg?fit=bounds&format=jpg&quality=80&width=640&height=640&dpr=1","thumbnailUrl":"https://www.lego.com/cdn/cs/set/assets/blt3126f56afab0d3f5/42125_alt13.jpg?fit=bounds&format=jpg&quality=80&width=65&height=45&dpr=1","highResUrl":"https://www.lego.com/cdn/cs/set/assets/blt3126f56afab0d3f5/42125_alt13.jpg?fit=bounds&format=jpg&quality=80&width=1280&height=1280&dpr=1","fullscreenUrl":"https://www.lego.com/cdn/cs/set/assets/blt3126f56afab0d3f5/42125_alt13.jpg?fit=bounds&format=jpg&quality=80&width=960&height=960&dpr=1","__typename":"ProductImageUrls"},"$ProductVideo:blt5107c5bf3552475f.video":{"url":"https://www.lego.com/cdn/cs/set/assets/blt5107c5bf3552475f/42125_v2.mp4","thumbnailUrl":"https://www.lego.com/cdn/cs/set/assets/bltb967a5b31d73b491/42125_v2_thumbnail.png?fit=bounds&format=png&width=65&height=45&dpr=1","__typename":"ProductVideoFormat"},"$ProductVideo:bltf0cfb8ca17ee084d.video":{"url":"https://www.lego.com/cdn/cs/set/assets/bltf0cfb8ca17ee084d/42125_v3.mp4","thumbnailUrl":"https://www.lego.com/cdn/cs/set/assets/blt3d70ca70084f16a6/42125_v3_thumbnail.png?fit=bounds&format=png&width=65&height=45&dpr=1","__typename":"ProductVideoFormat"},"$ProductVideo:blt8058851cc07026e6.video":{"url":"https://www.lego.com/cdn/cs/set/assets/blt8058851cc07026e6/42125_v1.mp4","thumbnailUrl":"https://www.lego.com/cdn/cs/set/assets/blt1e200c3f36b64a04/42125_v1_thumbnail.jpg?fit=bounds&format=jpg&quality=80&width=65&height=45&dpr=1","__typename":"ProductVideoFormat"'
print(re.findall('"url":"https://www.lego.com/cdn/cs/set/assets.*?mp4"',url))

我的解答思路和尝试过的方法

我尝试用加上不包含jpg的正则语句但是没什么效果,希望高人指点

我想要达到的结果
  • 写回答

2条回答 默认 最新

  • pppsso 2022-02-18 10:59
    关注
    
    re.findall('https://www.lego.com/cdn/cs/set/assets/[0-9A-z]{19}/\d{5}_v\d\.mp4', url)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 2月26日
  • 已采纳回答 2月18日
  • 创建了问题 2月18日

悬赏问题

  • ¥15 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥15 第一个已完成,求第二个做法
  • ¥20 搭建awx,试了很多版本都有错
  • ¥15 java corba的客户端该如何指定使用本地某个固定IP去连接服务端?
  • ¥15 activiti工作流问题,求解答
  • ¥15 有人写过RPA后台管理系统么?
  • ¥15 Bioage计算生物学年龄
  • ¥20 如何将FPGA Alveo U50恢复原来出厂设置哇?