Another question, this time about None Type.
I made another function in the crawler to get another piece of info.
If i put the if statatement in the function like this:
I made another function in the crawler to get another piece of info.
def get_single_item_data_3(item_url): source_code = requests.get(item_url) plain_text = source_code.text soup = BeautifulSoup(plain_text, 'html.parser') uls = soup.find_all('ul', {'class': 'labels'}) for ul in uls: return(ul.find('li').text.strip())If i only print out this function the result is:
Output:Nieuw
Nieuw
None
None
None
None
None
None
None
If i put this output in the string with my other results i get an error for the first " None" return:Error:TypeError: Can't convert 'NoneType' object to str implicitly
I tried several things to only let the function return a result if the result is not None. But without success. For example, if i call the function like this:if get_single_item_data_3(href) is not None: status = get_single_item_data_3(href) print(status)the result is on each row " nieuw" (output of the first item).
If i put the if statatement in the function like this:
def get_single_item_data_3(item_url): source_code = requests.get(item_url) plain_text = source_code.text soup = BeautifulSoup(plain_text, 'html.parser') uls = soup.find_all('ul', {'class': 'labels'}) for ul in uls: if get_single_item_data_3(item_url) is not None: return(ul.find('li').text.strip())Nothing is happening (i interrupted).
Error:
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/bs4/__init__.py", line 228, in __init__
self._feed()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/bs4/__init__.py", line 289, in _feed
self.builder.feed(self.markup)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/bs4/builder/_htmlparser.py", line 167, in feed
parser.feed(markup)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/html/parser.py", line 111, in feed
self.goahead(0)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/html/parser.py", line 173, in goahead
k = self.parse_endtag(i)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/html/parser.py", line 422, in parse_endtag
self.clear_cdata_mode()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/html/parser.py", line 128, in clear_cdata_mode
self.interesting = interesting_normal
KeyboardInterrupt
How can I print the output of my function (def get_single_item_data_3) in a string together with my other outputs, while ignorning the items that have a NoneType?