0

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 ?

1 Answer 1

1

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')
Sign up to request clarification or add additional context in comments.

1 Comment

i got understood! Thanks have a great day.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.