Python: BeautifulSoup - get an attribute value based on the name attribute

后端 未结 7 1800
抹茶落季
抹茶落季 2020-11-27 10:38

I want to print an attribute value based on its name, take for example


I want to do something

7条回答
  •  旧时难觅i
    2020-11-27 11:16

    theharshest answered the question but here is another way to do the same thing. Also, In your example you have NAME in caps and in your code you have name in lowercase.

    s = '
    Hello World
    ' soup = BeautifulSoup(s) attributes_dictionary = soup.find('div').attrs print attributes_dictionary # prints: {'id': 'get attrs', 'x': 'something', 'class': ['question'], 'name': 'python'} print attributes_dictionary['class'][0] # prints: question print soup.find('div').get_text() # prints: Hello World

提交回复
热议问题