【问题标题】:Using xmltodict to access a line inside a tag in Python使用 xmltodict 在 Python 中访问标签内的一行
【发布时间】:2019-11-26 05:56:33
【问题描述】:

我有一个看起来像这样的 xml 文件:

<!-- For the full list of available Crowd HTML Elements and their input/output documentation,
      please refer to https://docs.aws.amazon.com/sagemaker/latest/dg/sms-ui-template-reference.html -->

<!-- You must include crowd-form so that your task submits answers to MTurk -->
<crowd-form answer-format="flatten-objects">

    <!-- The crowd-classifier element will create a tool for the Worker to
 select the correct answer to your question.
          Your image file URLs will be substituted for the "image_url" variable below

          when you publish a batch with a CSV input file containing multiple image file URLs.

          To preview the element with an example image, try setting the src attribute to

          "https://s3.amazonaws.com/cv-demo-images/two-birds.jpg" -->
<crowd-image-classifier\n        
src= "https://someone@example.com/abcd.jpg"\n        
categories="[\'Yes\', \'No\']"\n        
header="abcd"\n        
name="image-contains">\n\n       
<!-- Use the short-instructions section for quick instructions that the Worker\n
will see while working on the task. Including some basic examples of\n              
good and bad answers here can help get good results. You can include\n              
any HTML here. -->\n        
<short-instructions>\n\n        
</crowd-image-classifier>
</crowd-form>
<!-- YOUR HTML ENDS -->

我要提取行:

src = https://someone@example.com/abcd.jpg

并将其分配给python中的变量。 xml 解析新手:

我试过了:

hit_doc = xmltodict.parse(get_hit['HIT']['Question'])
image_url = hit_doc['HTMLQuestion']['HTMLContent']['crowd-form']['crowd-image-classifier']

错误:

    image_url = hit_doc['HTMLQuestion']['HTMLContent']['crowd-form']['crowd-image-classifier']
TypeError: string indices must be integers

如果我不访问代码中的 ['crowd-image-classifier'] 并将自己限制在

hit_doc = xmltodict.parse(get_hit['HIT']['Question'])
image_url = hit_doc['HTMLQuestion']['HTMLContent']

然后我得到完整的 xml 文件。

如何访问该 img src?

【问题讨论】:

  • 看起来hit_doc['HTMLQuestion']['HTMLContent'] 返回一个包含多个crowd-image-classifier 的列表,而不是一个字典。试试image_url = hit_doc['HTMLQuestion']['HTMLContent'][0]['crowd-image-classifier']
  • 现在已经编辑了我的问题。希望它有更好的帮助。 PS,还是同样的错误。

标签: python regex python-3.x xml xmltodict


【解决方案1】:

您可以使用 BeautifulSoup。请参阅下面的工作代码。

from bs4 import BeautifulSoup


html = '''<!-- For the full list of available Crowd HTML Elements and their input/output documentation,
      please refer to https://docs.aws.amazon.com/sagemaker/latest/dg/sms-ui-template-reference.html -->

<!-- You must include crowd-form so that your task submits answers to MTurk -->
<crowd-form answer-format="flatten-objects">

    <!-- The crowd-classifier element will create a tool for the Worker to
 select the correct answer to your question.
          Your image file URLs will be substituted for the "image_url" variable below

          when you publish a batch with a CSV input file containing multiple image file URLs.

          To preview the element with an example image, try setting the src attribute to

          "https://s3.amazonaws.com/cv-demo-images/two-birds.jpg" -->
<crowd-image-classifier\n        
src= "https://someone@example.com/abcd.jpg"\n        
categories="[\'Yes\', \'No\']"\n        
header="abcd"\n        
name="image-contains">\n\n       
<!-- Use the short-instructions section for quick instructions that the Worker\n
will see while working on the task. Including some basic examples of\n              
good and bad answers here can help get good results. You can include\n              
any HTML here. -->\n        
<short-instructions>\n\n        
</crowd-image-classifier>
</crowd-form>
<!-- YOUR HTML ENDS -->'''

soup = BeautifulSoup(html, 'html.parser')
element = soup.find('crowd-image-classifier')
print(element['src'])

输出

https://someone@example.com/abcd.jpg

【讨论】:

  • hit_doc = xmltodict.parse(get_hit['HIT']['Question'])['HTMLQuestion']['HTMLContent'] 行给了我相同的 xml 内容,类型为 str。但是,在使用语句 soup = BeautifulSoup(hit_doc, 'html.parser') element = soup.find('crowd-image-classifier') print(element) 时,它会将元素返回为无。 :(
【解决方案2】:

我切换到使用 xml 元素树

我得到的语法有点类似于:

import xml.etree.ElementTree as ET
root = ET.fromstring(hit_doc)
for child in root:
    if child[0].text == 'crowd-image-classifier':
    image_data = child[1].text

【讨论】:

    猜你喜欢
    • 2018-03-20
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-03
    相关资源
    最近更新 更多