派大星敲代码 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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化