Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check that list of tuples has tuple with 1st element as defined string

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

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

It looks something like this:

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

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

  1. List have tuple with 1st element called class,
  2. Values of tuples 1st element (it will be 2nd element) is content;

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

like image 256
daGrevis Avatar asked Dec 10 '22 01:12

daGrevis


2 Answers

When looping through your elements:

if ('class', 'content') in element_attributes:
    #do stuff
like image 102
Silas Ray Avatar answered May 25 '23 15:05

Silas Ray


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

('class', 'content') in l

returns True, because there is at least one tuple with 'class' as first and 'content' as second element.

You can now use it:

if ('class', 'content') in l:
    # do something
like image 35
eumiro Avatar answered May 25 '23 16:05

eumiro