Cho
Cho

Reputation: 153

HTML parsing by python, how to parse attribute that is unusual?

I got a question for HTML parsing using python.

I'm trying to get the attribute key and value to use if fomular in python.

<!DOCTYPE HTML>
<html>
    <body>
        <p dir="ltr" class="FM_title_0">Test Color</p>
    </body>
</html>

and this is code that i am making as follow

with open("C:\\file") as fp:
    soup = BeautifulSoup(fp, "html.parser")
tag = soup.p
if tag['class'] == 'FM_title_0' :
    print('aaa')

but the result shows nothing. i figure out that tag['class'] is not able to read 'FM_title_0'. why does it not read 'FM_title_0' ??

additionally, when i run this code below

with open("C:\\file") as fp:
    soup = BeautifulSoup(fp, "html.parser")
tag = soup.p
print(tag['class'])

it shows ['FM_title_0']. but please look this one below

with open("C:\\file") as fp:
    soup = BeautifulSoup(fp, "html.parser")
tag = soup.p
print(tag['dir'])

this result shows ltr.

Is there any difference between [xxx] and xxx when read attribute's value ?

Upvotes: 0

Views: 38

Answers (1)

PARMESH
PARMESH

Reputation: 98

The return type of tag['class'] is a list. It contains list of all the classes the tag has. So you can use the in operator to compare. Check the code below for your reference

from bs4 import BeautifulSoup
with open("test.html") as fp:
    soup = BeautifulSoup(fp, "html.parser")
tag = soup.p
print tag['class']
if 'FM_title_0' in tag['class'] :
    print('aaa')

Upvotes: 1

Related Questions