当转换为带有.text的文本时,漂亮的汤XML标记(<>)成为非常非结构化的文本

omkar patil
from bs4 import BeautifulSoup
import requests

source = requests.get('http://www.mocky.io/v2/5e34780e3000008c00d964dd').text  
soup = BeautifulSoup(source)
print(soup)

输出:

 <textarea cols="100" name="olidata" readonly="" rows="40">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;EVENT spec="IDL:o2bcs/automator/common/tasklistEvents:1.0#tasklistupdateevent"&gt;
  <?xml version="1.0" encoding="UTF-8"?>
<event spec="IDL:o2bcs/automator/common/tasklistEvents:1.0#tasklistupdateevent">
  <tasklistoli>
    <bpid>
      <oid>B32028040:M11</oid>
      <type>MIGOPT1</type>
    </bpid>
    <oli>
      <tolicontrol>
        <oliid>1</oliid>
        <externalid1></externalid1>
        <externalid2></externalid2>
        <highlevelstatus>1</highlevelstatus>
        <status>550</status>
        <catalogue>14</catalogue>
        <errorcode>500220</errorcode>
        <errorstring>Unable to select the given SI COMP for deletion.</errorstring>
        <subscriptionid></subscriptionid>
        <activityid></activityid>
        <activityaccesscode></activityaccesscode>
        <dateofnetworkexecution></dateofnetworkexecution>
      </tolicontrol>
      <toli_1>
        <discriminator>29</discriminator>
        <tmigopt>

这为我提供了间距良好的结构化XML(类型= bs4.BeautifulSoup)

现在,如果我用

    print(soup.text)

输出:

<?xml version="1.0" encoding="UTF-8"?>\r\n<EVENT spec="IDL:o2bcs/automator/common/tasklistEvents:1.0#tasklistupdateevent">\r\n  \r\n\r\n  \r\n    \r\n      B32028040:M11\r\n      MIGOPT1\r\n    \r\n    \r\n      \r\n        1\r\n        \r\n        \r\n        1\r\n        550\r\n        14\r\n        500220\r\n        Unable to select the given SI COMP for deletion.\r\n        \r\n        \r\n        \r\n        \r\n      \r\n      \r\n        29\r\n        \r\n          \r\n            \r\n              524742\r\n              40193375\r\n              \r\n              \r\n              40003859\r\n              MOB\r\n              o2UniteBasicService\r\n              O2P0058\r\n              2018-05-08 00:00:00\r\n              \r\n              \r\n              \r\n              N\r\n              \r\n              \r\n              \r\n              2014-07-09 00:00:00\r\n              0\r\n              \r\n              \r\n                O2O0014\r\n                \r\n                524742\r\n                SIM

这给了我非常糟糕的非结构化数据,(类型= str)

我打算在文本上使用正则表达式,但需要适当的数据,请帮忙

大兵搜

我仍然不明白您的确切需求。让我举一个例子,看看它是否对您有帮助

from simplified_scrapy.request import req
from simplified_scrapy.simplified_doc import SimplifiedDoc
html = req.get('http://www.mocky.io/v2/5e34780e3000008c00d964dd') 
doc = SimplifiedDoc(html)
print (doc.event.text) # Output the text in the event tag
print ('-'*50)
# Traverse all nodes
def test(ele):
    if isinstance(ele,list):
        for e in ele:
            test(e)
        return
    children = ele.children
    if children:
        for e in children:
            test(e)
    else:
        print (ele.tag,ele.text)

test(doc.event.children)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Java将非结构化数据(文本)转换为结构化格式

Excel或VBA将非结构化文本转换为列

在 Python 中加载带有非结构化文本的 txt

递归解析/将结构化文本转换为字典

改善非结构化文本的解析

将带有链接的文本转换为可点击的 - 错误

将Dataset或Datagridview转换为带有列的文本

将带有列的 PDF 转换为 rails 中的文本

将带有标签的列表转换为文本列表,python

如何将结构化文本文件转换为xml文件api

如何以结构化的方式将表格结构转换为纯文本

从带有文本的csv文件转换为带有python的字典

读取非结构化文本文件后,如何在 Spark Scala 中使用正则表达式将 RDD 转换为 Dataframe?

将非结构化数据(连接的文本字符串)转换为可使用、可读的格式/从字符串中提取数据点

如何将带有html标记的文本列转换为Excel中的vba中的格式化文本

如何将 HTML 转换为带有文本和格式的对象结构?

将带有结构文本的地图文件转换为实际的HashMap

如何在结构化文本中将枚举转换为 uint

Excel公式:将结构化表的名称转换为文本

将结构化文本文件转换为 R 中的列式 CSV

在 Python 中读取的非结构化文本文件使其结构化

如何从非结构化文本创建python字典?

从非结构化文本分配计数

按非结构化文本列分组以透视数据

熊猫:解析结构化非表格文本中的值

在python中解析非结构化文本

使用Shell脚本将具有表名的文本转换为带有列名的单独值

批处理文件可将所有pdf转换为文本(带有xpdf)

将结构化但非标准结构的文本文件转换为R中的数据框