I have coded a web scraper for Stack Overflow, but it doesn't work. Apparently, in my soup there are NoneType objects that just came from nowhere. Here's the web scraper code:
import requests
from bs4 import BeautifulSoup
url = 'https://stackoverflow.com/questions?tab=newest&page='
r = requests.post(url)
soup = BeautifulSoup(r.text, 'lxml').find('div', id='questions').find_all('div')
for summary in soup: # FIXME: Prints each question twice
try:
print(f'Question: {summary.h3.text}')
print(f'Tags: {", ".join(summary.find("div", class_="tags").text[1:].split(" "))}')
except Exception as e:
print(e) # Prints "'NoneType' has no attribute 'text'" which shouldn't be in the soup
The error I get (If you didn't read the comment) is "'NoneType' has no attribute 'text'" which confuses me a lot because of the fact that there are NoneType objects in the soup.
I am using:
- Windows 10
- Python 3.8