1

python returns error regarding attributes when using stripped_strings while parsing a html content. This html content is added to the class. Here is the code snippet, the data that needs to be extracted is part of a list.

Updated Code : 

from typing import List, Any, Optional

import requests from bs4 import BeautifulSoup

class RESTApp: def init(self, url): self.url = url

def getAllUsers(self):
    # the list is added here
    data = '''<ul class="users-list clearfix">
                <li>
                  <img src="dist/img/user1-128x128.jpg" alt="User Image">
                  <a class="users-list-name" href="#">Alexander Pierce</a>
                  <span class="users-list-date">Today</span>
                </li>
                <li>
                  <img src="dist/img/user8-128x128.jpg" alt="User Image">
                  <a class="users-list-name" href="#">Norman</a>
                  <span class="users-list-date">Yesterday</span>
                </li>
                <li>
                  <img src="dist/img/user7-128x128.jpg" alt="User Image">
                  <a class="users-list-name" href="#">Jane</a>
                  <span class="users-list-date">12 Jan</span>
                </li>
                <li>
                  <img src="dist/img/user6-128x128.jpg" alt="User Image">
                  <a class="users-list-name" href="#">John</a>
                  <span class="users-list-date">12 Jan</span>
                </li>
                <li>
                  <img src="dist/img/user2-160x160.jpg" alt="User Image">
                  <a class="users-list-name" href="#">Alexander</a>
                  <span class="users-list-date">13 Jan</span>
                </li>
                <li>
                  <img src="dist/img/user5-128x128.jpg" alt="User Image">
                  <a class="users-list-name" href="#">Sarah</a>
                  <span class="users-list-date">14 Jan</span>
                </li>
                <li>
                  <img src="dist/img/user4-128x128.jpg" alt="User Image">
                  <a class="users-list-name" href="#">Nora</a>
                  <span class="users-list-date">15 Jan</span>
                </li>
                <li>
                  <img src="dist/img/user3-128x128.jpg" alt="User Image">
                  <a class="users-list-name" href="#">Nadia</a>
                  <span class="users-list-date">15 Jan</span>
                </li>
              </ul>'''
    soup = BeautifulSoup(data, "html.parser")
    user_names = soup.find('ul', class_='users-list clearfix')
    split_details = list(user_names.stripped_strings)
    print(split_details)
    test = RESTApp("https://adminlte.io/themes/AdminLTE/pages/examples/profile.html")
    test.getAllUsers()

ACTUAL RESULT = AttributeError: 'NoneType' object has no attribute 'stripped_strings'

2 Answers 2

1

You're constructing BeautifulSoup with a string literal, '''data''', instead of the variable data.

import requests
from bs4 import BeautifulSoup

class RESTApp:
    def __init__(self, url):
        self.url = url

    def getAllUsers(self):
        # the list is added here
        data = '''<ul class="users-list clearfix">
                <li>
                ...............
                </li>

                <li>
                ......
                </li>
                <li>
                .....
                </li>
            </ul>'''


        # parsing the data to get text from list.
        soup = BeautifulSoup(data, "html.parser")
        user_names = soup.find('ul', class_='users-list clearfix')
        split_details = list(user_names.stripped_strings)
        print(split_details)

test = RESTApp("https://adminlte.io/themes/AdminLTE/pages/examples/profile.html")
test.getAllUsers()
Sign up to request clarification or add additional context in comments.

7 Comments

Returns error for soup = BeautifulSoup(data, "html.parser") NameError: name 'data' is not defined
@Rima Did you copy my code or just update yours to not point to '''data'''? Your post had indentation errors I also fixed, but I didn't call them out because I wasn't sure that was your source code or just a copy/paste issue.
I am using PyCharm and fixed the indentation using Ctrl+Alt+I. So now there is no error but it returns nothing, Does not parse the data. What am I missing now?
@Rima I don't know. Using the code exactly as I provided returns ['...............', '......', '.....']. I'm not positive you've fixed all the indentation problems.
Updated the code snippet with the entire code that I am using.
|
1

Change soup = BeautifulSoup('''data''', "html.parser") to soup = BeautifulSoup(data, "html.parser")

6 Comments

Updated as you suggested and it returns soup = BeautifulSoup(data, "html.parser") NameError: name 'data' is not defined
In such case, you have incorrect indentation for data = ... and soup = ... They must have the same level of indentation.
I am using PyCharm and fixed the indentation using Ctrl+Alt+I. So now there is no error but it returns nothing, Does not parse the data. What am I missing now?
Two last lines must be shifted one level to the left. Refer to the code @Web-Head has provided.
This is amazing.I am learning python, very new to this language and surprised how indentation changes the output. But worth all the hours spent on figuring out the problem. Thank you so very much!!
|

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.