【问题标题】:Beautiful Soup extraction of content based on typesBeautiful Soup 基于类型的内容提取
【发布时间】:2018-12-11 08:27:42
【问题描述】:

我想从以下 xml 格式中提取问题 (type='q') 和答案 (type='a') 对作为单个数据点:

<?xml version="1.0" encoding="us-ascii"?>
<transcript id="001" >
<body>
<section name="Q&amp;A">
      <speaker id="0">
        <plist>
          <p>Thank you. We'll now be conducting the question-and-answer session. <mark type="Operator Instructions" /> Thank you. Please go ahead with your question.</p>
        </plist>
      </speaker>
      <speaker id="3" type="q">
        <plist>
          <p>Good morning. First of all, Happy New Year.</p>
        </plist>
      </speaker>
      <speaker id="2" type="a">
        <plist>
          <p>Happy New Year, sir.</p>
        </plist>
      </speaker>
      <speaker id="3" type="q">
        <plist>
          <p>Thank you. How is your pain now?.</p>
        </plist>
      </speaker>
       <speaker id="2" type="a">
            <plist>
              <p>Oh, it's better now. I think i am healing.</p>
            </plist>
          </speaker>
</section>
</body>
</transcript>

即输出应该是这样的:['早上好。首先,新年快乐。先生,新年快乐。','谢谢。你现在的疼痛如何?哦,现在好多了。我想我正在康复。']

谁能帮我用美丽的汤做这个?我当前的代码提取了文档中的所有 &lt;p&gt; 标记,但问题是还有其他部分(“Q&A”除外)以及其 &lt;p&gt; 标记被提取。

soup = BeautifulSoup(handler, "html.parser")
texts = []
for node in soup.findAll('p'):
    text = " ".join(node.findAll(text=True))
    #text = clean_text(text)
    texts.append(text)

【问题讨论】:

    标签: python xml beautifulsoup xml-parsing


    【解决方案1】:

    您可以分别使用find_all('speaker', type='q')find_all('speaker', type='a') 找到所有问题和所有答案。然后用zip加入对应的问题及其答案。

    代码:

    questions = soup.find_all('speaker', type='q')
    answers = soup.find_all('speaker', type='a')
    
    for q, a in zip(questions, answers):
        print(' '.join((q.p.text, a.p.text)))
    

    输出:

    Good morning. First of all, Happy New Year. Happy New Year, sir.
    Thank you. How is your pain now?. Oh, it's better now. I think i am healing.
    

    如果你想把它放在一个列表中,你可以使用列表推导

    q_and_a = [' '.join((q.p.text, a.p.text)) for q, a in zip(questions, answers)]
    print(q_and_a)
    # ['Good morning. First of all, Happy New Year. Happy New Year, sir.',
    #  "Thank you. How is your pain now?. Oh, it's better now. I think i am healing."]
    

    【讨论】:

      【解决方案2】:

      您可以使用findAll('speaker', {"type": "q"})查找问题,使用findNext("speaker")查找相应答案。

      例如:

      from bs4 import BeautifulSoup
      soup = BeautifulSoup(handler, "html.parser")
      for node in soup.findAll('speaker', {"type": "q"}):
          print( node.find("p").text )
          print( node.findNext("speaker").find("p").text)
          print( "--" )
      

      输出:

      Good morning. First of all, Happy New Year.
      Happy New Year, sir.
      --
      Thank you. How is your pain now?.
      Oh, it's better now. I think i am healing.
      --
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-24
        • 2020-05-04
        • 2011-01-09
        • 2017-12-31
        • 2018-01-15
        相关资源
        最近更新 更多