Reputation: 25
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'
Upvotes: 1
Views: 200
Reputation:
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()
Upvotes: 1
Reputation: 552
Change soup = BeautifulSoup('''data''', "html.parser")
to soup = BeautifulSoup(data, "html.parser")
Upvotes: 1