【问题标题】:Indexing xml/svg file with Beautiful Soup用 Beautiful Soup 索引 xml/svg 文件
【发布时间】:2018-11-12 23:26:09
【问题描述】:

我有一个带有唯一 ID 标题的 svg 文件,如下所示:

<title id="t0">
<title id="t1">

这样的标题有成千上万个。

以下是我从 svg 创建 BS 对象的方法:

svg = open('counties.svg', 'r').read()
soup = BeautifulSoup(svg, 'xml')
paths = soup.findAll('path')

所以我可以拉出每条路径的样式,但我不能以相同的方式拉出标题:

for p in paths:
   p['style'] = "font-size:12px..." #this works
   p['title'] = "title for path..." #this doesnt work

我怀疑这是因为“标题”部分有一个唯一的 ID。

这是“路径”的样例

  <path d="M 405.190,251.879 L 405.748,257.292 L 401.350,257.81 L 401.012,254.629 L 400.885,253.474 L 401.300,253.43 L 401.430,253.322 L 401.300,252.294 L 405.190,251.879" id="01031" style="font-size:12px;fill-rule:nonzero;stroke:#FFFFFF;stroke-opacity:1;
    stroke-width:0.1;stroke-miterlimit:4;stroke-dasharray:none;stroke-linecap:butt;
    marker-start:none;stroke-linejoin:bevel;fill:#bcbddc">
   <title id="t31">
    Coffee, AL
   </title>
  </path>

如何正确索引标题以便更改其内容?

【问题讨论】:

    标签: python svg indexing web-scraping beautifulsoup


    【解决方案1】:

    试试:

    for p in paths:
       p['style'] = "font-size:102px" #this works
       p.title.string = "title for path..." 
    

    【讨论】:

    • 有效!谢谢!
    猜你喜欢
    • 2018-10-11
    • 2023-03-24
    • 1970-01-01
    • 2023-01-24
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多