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
l4ever
V2EX  ›  Python

BeautifulSoup 找到的内容, 属性为什么是倒过来的?

  •  
  •   l4ever · 2020-02-20 20:21:42 +08:00 · 2768 次点击
    这是一个创建于 1525 天前的主题,其中的信息可能已经有所发展或是发生改变。
    from bs4 import BeautifulSoup
    
    content = """
        <text transform="matrix(1.0318 0 0 1 575.125 394.0762)" class="abc" style="a123">ABC</text>
    
        <text transform="matrix(1 0 0 1 531.0552 394.0762)" font-family="'MicrosoftYaHei'" font-size="18px">DEF</text>
        """
    soap = BeautifulSoup(markup=content, features='lxml')
    soap_text = soap.find_all('text')
    for s in soap_text:
        print(s)
    
    
    

    输出:

    <text class="abc" style="a123" transform="matrix(1.0318 0 0 1 575.125 394.0762)">ABC</text>
    
    <text font-family="'MicrosoftYaHei'" font-size="18px" transform="matrix(1 0 0 1 531.0552 394.0762)">DEF</text>
    

    很是费解, 为什么输出的内容和原文不一样了呢, 有些属性调整了位置.

    10 条回复    2020-02-20 23:42:47 +08:00
    l4ever
        1
    l4ever  
    OP
       2020-02-20 20:22:45 +08:00
    我是要读出来, 然后直接逐行进行修改后替换回去, 这样更改了位置. 没法替换回去啊.
    freakxx
        2
    freakxx  
       2020-02-20 21:37:41 +08:00   ❤️ 1
    直接这样不行?
    s.attrs[attr_name] = attr_value
    l4ever
        3
    l4ever  
    OP
       2020-02-20 22:13:19 +08:00 via iPhone
    @freakxx 我要修改 text 值 ,好像修改不了。看了一下 sf 也有人问,貌似无解。只能拿到唯一值去 html 源代码搜,找到对应行读回来。🤣
    jmc891205
        4
    jmc891205  
       2020-02-20 22:21:49 +08:00 via iPhone
    html 标签的属性是无序的 不知道你要做什么 但不要依赖于 html 标签属性的顺序去写应用
    learnshare
        5
    learnshare  
       2020-02-20 22:27:07 +08:00
    属性并没有强制顺序,应当解析并根据 元素 + 属性名 查找
    Shazoo
        6
    Shazoo  
       2020-02-20 22:44:00 +08:00
    soup.replae_with() + soup.prettify() ?
    encro
        7
    encro  
       2020-02-20 22:49:24 +08:00   ❤️ 1
    这个,只能看源码吧。
    如果没有记错的话,反转不是直接利用原来文档,是重新生成了,要原来文档有一个双下划线的属性。
    l4ever
        8
    l4ever  
    OP
       2020-02-20 22:53:39 +08:00 via iPhone
    @encro 帮我看看是哪个属性啊?我现在是找出来之后,有回到 html 源代码读原文的 特别别扭。
    encro
        9
    encro  
       2020-02-20 23:03:59 +08:00
    三个方法:
    1,print(s)改为 dir(s)
    2,直接用 ipython 看一下
    3,直接用 ide 调试看
    whoops
        10
    whoops  
       2020-02-20 23:42:47 +08:00   ❤️ 1
    修改属性按二楼的方法,修改 text 修改 string

    for s in soap_text:
    s.string = 'Something'
    print(s)

    另外属性不是倒过来了,是按字母排序了,具体前面有人说了看源码__str__方法的实现
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3724 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 04:38 · PVG 12:38 · LAX 21:38 · JFK 00:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.