【问题标题】:Insert HTML Attribute with Beautiful Soup用漂亮的汤插入 HTML 属性
【发布时间】:2012-01-06 22:34:13
【问题描述】:

我有一个简单的需求:我希望使用 Beautiful Soup 将属性插入到 HTML 部分中,该属性会反映到网页中。

例如,对于 HTML 代码:

<input type="submit" name="name1">

我有 BeautifulSoup:

from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup('<input type="submit" name="name1">')
getElementByName = soup.find(attrs={'name':'name1'})
# getElementByName.insert method does not reflect the insertion on the webpage

我希望为 Beautiful Soup 模仿以下 Javascript 方法:

document.getElementByName("name1").setAttribute("id", "id1");

我相信 Beautiful Soup 不能做到这一点,因为它只是一个 XML/HTML 解析器。但是,如果有人知道答案,那就把它扔在那里?

谢谢, 阿米特

【问题讨论】:

    标签: python beautifulsoup


    【解决方案1】:

    Documentation 是个好东西

    是的,您可以修改解析树。在这种情况下,可能类似于:

    getElementByName("name1")["id"] = "id1"
    

    【讨论】:

      猜你喜欢
      • 2017-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多