Getting UnboundLocalError while scraping

Try declaring the tag=None right outside your for loop like follows

def main(req, date):
r = req.get(f"website/{date.strftime('%Y%m%d')}")
soup = BeautifulSoup(r.content, 'html.parser')
tag=None
for tag in soup.select(".b-plainlist "):

The error occurs as the control never enters the loop and in-turn, variable ‘tag’ is never initialized. Hence when you try to return tag.select_one(“.b-plainlist__date”), the compiler throws an UnboundLocalError

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top