Issue
So I am using this code to retrieve the anchor tags on a site but, I keep getting this error and the editor shows soup('a') as a problem.
import urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup
url = input('Enter:> ')
page_opener = urllib.request.urlopen(url).read()
x = BeautifulSoup(page_opener,'html.parser')
#Retrieving HTMl tags
tags = soup('a')
for tag in tags:
print(tag.get('href',None))
And here is the error I get from the Terminal
Traceback (most recent call last):
tags = soup('a')
NameError: name 'soup' is not defined
I would really appreciate assistance on why soup('a') is not being verified on the editor. Thank you in advance.
Solution
You have called your soup x.
x = BeautifulSoup(page_opener,'html.parser')
You can just change that to soup (which is conventional) or use your actual variable name instead.
soup = BeautifulSoup(page_opener,'html.parser')
Answered By - topsail
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.