Python Forum
NameError: name 'bsObj' is not defined - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: NameError: name 'bsObj' is not defined (/thread-606.html)

Pages: 1 2


NameError: name 'bsObj' is not defined - Blue Dog - Oct-22-2016

I am not sure what this error is.
here is the line of code it is talking about:

for link in bsObj.findAll("a", href=re.compile("^(/wiki/)")):

Here it the whole error:

Traceback (most recent call last):
  File "C:\Users\renny and kite\Desktop\web scraping the book\example_11\example
_11\example_11.py", line 15, in <module>
    for link in bsObj.findAll("a", href=re.compile("^(/wiki/)")):
NameError: name 'bsObj' is not defined
Press any key to continue . . .

How is the name not defined? Huh


RE: NameError: name 'bsObj' is not defined - buran - Oct-22-2016

please, post the whole code, exactly as you try to run it.
The error is clear bsObj is not defined at the time when you try to use it in that line.


RE: NameError: name 'bsObj' is not defined - Yoriz - Oct-22-2016

We cant tell because the rest of the code where it has not been defined is not shown.


RE: NameError: name 'bsObj' is not defined - ichabod801 - Oct-22-2016

(Oct-22-2016, 07:24 PM)Blue Dog Wrote: How is the name not defined?

That's what you would have to go back through your code and figure out. The error is telling you that when Python gets to that line of code, it has not seen the name bsObj before, at least in that scope. When I get that error, one of three things has happened: I mistyped the variable name when it was first mentioned, some if/elif logic skipped over the first mention of that name, or I didn't scope it correctly (it should be self.bsObj or something).


RE: NameError: name 'bsObj' is not defined - Blue Dog - Oct-22-2016

Here is the code:

from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
pages = set()
def getLinks(pageUrl):
 global pages
 html = urlopen("http://en.wikipedia.org"+pageUrl)
 try:
    print(bsObj.h1.get_text())
    print(bsObj.find(id ="mw-content-text").findAll("p")[0])
    print(bsObj.find(id="ca-edit").find("span").find("a").attrs['href'])
 except AttributeError:
   
    print("This page is missing something! No worries though!")
for link in bsObj.findAll("a", href=re.compile("^(/wiki/)")):
   if 'href' in link.attrs:
       if link.attrs['href'] not in pages:
#We have encountered a new page
         newPage = link.attrs['href']
         print("----------------\n"+newPage)
         pages.add(newPage)
         getLinks(newPage)
getLinks("")
That it, I found this on the web and change some of it, the findAll is defined I think = print(bsObj.find(id ="mw-content-text").findAll("p")[0])

Thank you Dodgy


RE: NameError: name 'bsObj' is not defined - Yoriz - Oct-22-2016

Go back and look at the code you copied and find where it is you changed the definition of bsObj out of the code.


RE: NameError: name 'bsObj' is not defined - snippsat - Oct-22-2016

Fix indention the code is a mess.
The first error is because no soup object is defined
bsObj = BeautifulSoup(html, 'html.parser')



RE: NameError: name 'bsObj' is not defined - Blue Dog - Oct-23-2016

from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
pages = set()
def getLinks(pageUrl):
 global pages
 html = urlopen("http://en.wikipedia.org"+pageUrl)
 try:
    print(bsObj.h1.get_text())
    print(bsObj.find(id ="mw-content-text").findAll("p")[0])
    print(bsObj.find(id="ca-edit").find("span").find("a").attrs['href'])
 except AttributeError:
    
    print("This page is missing something! No worries though!")
    for link in bsObj.findAll("a", href=re.compile("^(/wiki/)")):
       if 'href' in link.attrs:
           if link.attrs['href'] not in pages:
#We have encountered a new page
         newPage = link.attrs['href']
         print("----------------\n"+newPage)
         pages.add(newPage)
         getLinks(newPage)
getLinks("")
Is that better, still will not work, but I am have for the problem


RE: NameError: name 'bsObj' is not defined - Yoriz - Oct-23-2016

It looks like all you have done is changed the indentation and still left out the bsObj


RE: NameError: name 'bsObj' is not defined - sparkz_alot - Oct-23-2016

indentation still wrong (hint line 17) I presume you are nesting the 'if' statement