首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用xmltodict访问Python标记中的一行

使用xmltodict访问Python标记中的一行
EN

Stack Overflow用户
提问于 2019-11-26 05:56:33
回答 2查看 301关注 0票数 0

我有一个xml文件,看起来如下:

代码语言:javascript
复制
<!-- 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 -->

我想提取这条线:

代码语言:javascript
复制
src = https://someone@example.com/abcd.jpg

并将其赋值给python中的一个变量。xml解析的Bit新技术:

我试着说:

代码语言:javascript
复制
hit_doc = xmltodict.parse(get_hit['HIT']['Question'])
image_url = hit_doc['HTMLQuestion']['HTMLContent']['crowd-form']['crowd-image-classifier']

错误:

代码语言:javascript
复制
    image_url = hit_doc['HTMLQuestion']['HTMLContent']['crowd-form']['crowd-image-classifier']
TypeError: string indices must be integers

如果我没有在代码中访问'crowd-image-classifier'并限制自己

代码语言:javascript
复制
hit_doc = xmltodict.parse(get_hit['HIT']['Question'])
image_url = hit_doc['HTMLQuestion']['HTMLContent']

然后我将获得完整的xml文件。

如何访问img src?

EN

回答 2

Stack Overflow用户

发布于 2019-11-26 11:24:04

您可以使用BeautifulSoup。见下面的工作代码。

代码语言:javascript
复制
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'])

输出

代码语言:javascript
复制
https://someone@example.com/abcd.jpg
票数 1
EN

Stack Overflow用户

发布于 2019-11-29 06:04:43

我转而使用xml元素树。

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

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59044608

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档