Issue
I'm quite new to Python and can usually figure out issues with some scouring, but with this I'm stumped, a website I frequent clandestinely releases products on their shop page, when they are sold out they are no longer visible, they don't just say sold out, they are removed, so their are no links in the list element (not sure if using the term element correctly there).
I have managed to use beautifulsoup to extract when there is a link, but the majority of the time there isn't one, I'm now trying to use a 'while True' and 'IF' statement to routinely check if a link isn't there, if there is no link, recheck after some time, but I cannot get python to print a True result when no link is returned and I'm not sure why.
soup = BeautifulSoup(response.content, 'html5lib')
list = soup.find('div', class_='list-grid')
for link in list.find_all('a'):
item = (link.get('href'))
if link is NoneType:
print("true")
else:
print("false")
I've tried link == "", link is "", link is None, link is NoneType (though I'm not sure the difference) and all the same with the 'item' variable instead, but when no link is returned the if statement returns no value, no True or False, it's like it ignores the statement entirely even though it's a yes/no question.
Solution
It seems you're really only concerned with whether or not the call to list.find_all('a')
actually returns any matching elements, in which case you can do away with the loop entirely and simply test the length of the list that's returned by Beautiful Soup:
soup = BeautifulSoup(response.content, 'html5lib')
list = soup.find('div', class_='list-grid')
if len(list.find_all('a')) > 0:
print("true")
else:
print("false")
This solution will be vastly more performant in production and reduces the verbosity of your check significantly.
Answered By - esqew
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.