V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
qianyealone
V2EX  ›  Python

Python 文本采集替换图片的问题

  •  
  •   qianyealone · 2020-05-03 16:50:31 +08:00 · 2215 次点击
    这是一个创建于 1425 天前的主题,其中的信息可能已经有所发展或是发生改变。
    文章采集下来了带图片的内容,想把图片地址换成本地,图片也下了,但是怎么批量把远程图片地址换成本地的呢

    把<img alt="" src="http://img.baidu.com/2020/05/20200503105830041.jpg"/>换成
    <img alt="" src="20200503105830041.jpg"/>

    新人跪求大佬带带
    13 条回复    2020-05-05 11:43:18 +08:00
    ClericPy
        1
    ClericPy  
       2020-05-03 17:06:11 +08:00
    本地开个 static/images 文件夹下载图片, 然后路径用相对路径, 记得别用 windows 的反斜杠, 用 as_posix 的

    大致用的就是:

    requests 的 Response.content 写入 file
    pathlib 的 as_posix

    没什么其他信息, 就这样吧

    PS: 图片名字如果不重要的话, 最简单的就是图片地址转 md5 名字, 避免文件名重复, 也不用处理不同路径同名文件
    jdhao
        2
    jdhao  
       2020-05-03 17:12:28 +08:00 via Android
    直接正则表达式都能替换了
    ClericPy
        4
    ClericPy  
       2020-05-03 17:17:41 +08:00
    jdhao
        5
    jdhao  
       2020-05-03 17:18:12 +08:00 via Android
    @qianyealone 查一下 python 的正则表达式,只能帮到这。。
    ClericPy
        6
    ClericPy  
       2020-05-03 17:22:37 +08:00
    我热晕了, 两次答非所问...

    html = '<img alt="" src="http://img.baidu.com/2020/05/20200503105830041.jpg"/>'
    print(re.sub('(?<=src=") http[^"]*/', '', html))
    # <img alt="" src="20200503105830041.jpg"/>
    qianyealone
        7
    qianyealone  
    OP
       2020-05-03 17:22:51 +08:00
    @ClericPy 谢谢了,大佬
    annielong
        8
    annielong  
       2020-05-03 17:56:20 +08:00
    习惯上还是 url 的目录来存文件,只用把域名替换就行了,实际上好多图片都是用的相对路径,域名都不需要替换,只用按目录保存图片就可以直接用
    llsquaer
        9
    llsquaer  
       2020-05-04 10:19:33 +08:00
    @ClericPy 直接复制你的代码 测试的..没变化啊 我用的 python3.6
    ClericPy
        10
    ClericPy  
       2020-05-04 10:33:56 +08:00
    @llsquaer #9
    我代码贴上来以后不知道 http 前面为什么多了空格, 反正在本站贴代码基本就是脑补
    https://paste.ubuntu.com/p/M4Q3QwnZRs/
    lau52y
        11
    lau52y  
       2020-05-04 14:15:50 +08:00 via iPhone
    我一般用 bs4 获取所有 img 然后就给 replace 替换了
    llsquaer
        12
    llsquaer  
       2020-05-05 11:36:10 +08:00
    @ClericPy 谢谢...我还在研究正则.一直用不好,之前做这种图片本地化是 自己手写的一个函数替换的.你这样更简单了.
    ClericPy
        13
    ClericPy  
       2020-05-05 11:43:18 +08:00
    @llsquaer #12 用正则替换虽然性能高, 但是准确度容错率一般, 比如这里的双引号遇到单引号又得适配, 遇到不是 http 开头的 src 又得适配, 遇到莫名其妙有个换行的, 遇到 src 实际是 data-src 的, 最稳妥的还是 bs4 之类的 DOM 解析然后替换
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2703 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 15:38 · PVG 23:38 · LAX 08:38 · JFK 11:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.