Reputation: 323
I try to web scrape from a webpage after I login some products with beautifulsoup. There is a case where product is no longer available. The webpage has a div class like the following only on page which has not a product
<div class="alert alert-danger">
<p>There is an error</p>
So I do
if soup.find_all('div', {'class': 'alert'}):
print('Alert...')
or
if soup.find_all('div', {'class': 'alert alert-danger'}):
print('Alert...')
But I get 'int' object has no attribute text in the place of a product
The status code of request is 200
How can I fix this and in place of empty product display something?
Upvotes: 0
Views: 1273
Reputation: 30971
I ran the content of if from both your code samples:
soup.find_all('div', {'class': 'alert'})
and
soup.find_all('div', {'class': 'alert alert-danger'})
In both cases I got:
[<div class="alert alert-danger">
<p>There is an error</p>
</div>]
So I can not replicate your error. Maybe you use some old version of BeautifulSoup?
I have version 4.7.1. Try to upgrade your installation of BeautifulSoup.
Yet another method how to check whether your document contains a div
element with class="alert"
:
if soup.find_all('div', class_='alert'):
print("Alert....")
Note that the keyword parameter contains _
at the end, in order to be
different from Python reseved word (class). This is a relatively new
feature of BeautifulSoup.
Upvotes: 1
Reputation: 121
Hmm, maybe, this is your solution
from bs4 import BeautifulSoup
p = '<div class="alert alert-danger">\n<p>There is an error</p>'
alert = 'alert'
soup = BeautifulSoup(p, 'html.parser').div['class']
if alert in list(soup):
print("Alert....")
Upvotes: 2