I am trying to find and print the links that occur only inside specific divs on a page.
import requests
from bs4 import BeautifulSoup
r = requests.get('https://targeturl.com/')
soup = BeautifulSoup(r.text, 'html.parser')
for div in soup.find_all('div'):
if 'target-class' in div.get('class'):
# find the link in the div
for link in div.find_all('a'):
print(link.get('href'))
When I try to run the above, I get TypeError: argument of type 'NoneType' is not iterable
Is this because some of the target-class
divs do not contain a link? And thus the loop can't iterate over it?