检查元组列表是否具有将第一个元素作为已定义字符串的元组 [英] Check that list of tuples has tuple with 1st element as defined string

查看:66
本文介绍了检查元组列表是否具有将第一个元素作为已定义字符串的元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解析HTML,我只需要获取带有选择器的标签,例如div.content.

I'm parsing HTML and I need to get only tags with selector like div.content.

对于解析,我使用的是 HTMLParser .到目前为止,我已经获得了标签属性的列表.

For parsing I'm using HTMLParser. I'm so far that I get list of tags' attributes.

它看起来像这样:

[('class', 'content'), ('title', 'source')]

问题是我不知道如何检查:

The problem is that I don't know how to check that:

  1. 列表中有第一个元素为class的元组,
  2. 元组第一个元素(将成为第二个元素)的值为content;
  1. List have tuple with 1st element called class,
  2. Values of tuples 1st element (it will be 2nd element) is content;

我知道这是一个简单的问题,但是我对Python也很陌生.谢谢您的任何建议!

I know this is easy question, but I'm quite new with Python as well. Thanks in any advice!

推荐答案

遍历元素时:

if ('class', 'content') in element_attributes:
    #do stuff

这篇关于检查元组列表是否具有将第一个元素作为已定义字符串的元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆