'str' object is not callable - python

Not quite understanding why I am getting this trace error:
Traceback (most recent call last):
File "S:/Personal Folders/Andy/Python Projects/Salesforce BZ API/Automated Reports.py", line 15, in <module>
parse = br.soup("find('div')")
File "build\bdist.win32\egg\spynner\browser.py", line 409, in _get_soup
return self._html_parser(self.html)
TypeError: 'str' object is not callable
Here is my code:
from __future__ import division
#from __future__ import unicode_literals
from __future__ import print_function
import spynner
from BeautifulSoup import BeautifulSoup
#Loading up Salesforce
br = spynner.Browser()
#br.debug_level = spynner.DEBUG
br.create_webview()
br.show()
br.set_html_parser("BeautifulSoup")
br.load("https://login.salesforce.com/")
parse = br.soup("find('div')")
print(parse)
br.browse()
br.close()

It looks like you're setting the HTML parser to the string "BeautifulSoup", not to BeautifulSoup. I don't have it installed so I can't test whether it works, but it's worth a try.

I took a quick look here, and it seems that soup is not a function but a property.

Related

NameError in function to retrieve JSON data

I'm using python 3.6.1 and have the following code which successfully retrieves data in JSON format:
import urllib.request,json,pprint
url = "https://someurl"
response = urllib.request.urlopen(url)
data = json.loads(response.read())
pprint.pprint(data)
I want to wrap this in a function, so i can reuse it. This is what i have tried in a file called getdata.py:
from urllib.request import urlopen
import json
def get_json_data(url):
response = urlopen(url)
return json.loads(response.read())
and this is the error i get after importing the file and attempting to print out the response:
>>> import getdata
>>> print(getdata.get_json_data("https://someurl"))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Nick\getdata.py", line 6, in get_json_data
from urllib.request import urlopen
NameError: name 'urllib' is not defined
i also tried this and got the same error:
import urllib.request,json
def get_json_data(url):
response = urllib.request.urlopen(url)
return json.loads(response.read())
What do i need to do to get this to work please?
cheers
Its working now ! I think the problem was the hydrogen addon i have for the Atom editor. I uninstalled it, tried again and it worked. Thanks for looking.

NameError: name 'urllib' is not defined "

CODE:
import networkx as net
from urllib.request import urlopen
def read_lj_friends(g, name):
# fetch the friend-list from LiveJournal
response=urllib.urlopen('http://www.livejournal.com/misc/fdata.bml?user='+name)
ERROR:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'urllib' is not defined
You've imported urlopen directly, so you should refer to it like that rather than via urllib:
response = urlopen('...')
You can also try in Python 3:
from six.moves import urllib
temp_file, _ = urllib.request.urlretrieve(url)
Just put import urllib at the top of your code
Try pls:
from urllib.request import urlopen
html = urlopen("http://www.google.com/")
print(html.read) # Content
For your case:
import networkx as net
from urllib.request import urlopen
def read_lj_friends(g, name):
# fetch the friend-list from LiveJournal
response=urlopen('http://www.livejournal.com/misc/fdata.bml?user='+name)

AttributeError: 'module' object has no attribute 'loads' while parsing json in python

I am getting this error:
Traceback (most recent call last):
File "C:/Users/Shivam/Desktop/jsparse.py", line 13, in <module>
info = json.loads(str(data))
AttributeError: 'module' object has no attribute 'loads'
Any thoughts what wrong I am doing here?
This is my code:
import json
import urllib
url = ''
uh = urllib.urlopen(url)
data = uh.read()
info = json.loads(str(data))
The problem is that you're using Python 2.5.x, which doesn't have the json module. If possible, I recommend upgrading to Python 2.7.x, as 2.5.x is badly outdated.
If you need to stick with Python 2.5.x, you'll have to use the simplejson module (see here). This code will work for 2.5.x as well as newer Python versions:
try:
import json
except ImportError:
import simplejson as json
Or if you're only using Python 2.5, just do:
import simplejson as json

Django views ImportError

I don't know if this logic is correct,
I'm trying to import a Django view in 2 different views.
I have an import chain like this:
the
a.views import b.views
b.views import c.views
c.views import d.views
and
d.views import b.views
but when I reach the last step I get an ImportError.
If I put a comment in d.views avoiding the import of b.views, it works.
I'm new with Django, can somebody help me?
If I use in a.views and in d.views the syntax
from b.views import *
it works, but.. the code is not so readable.
If I use
from b.views import my_func
it doesn't work!
This is the error from django shell:
>>> import maps.views
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/save/sites/myblog/maps/views.py", line 19, in <module>
from places.views import *
File "/Users/save/sites/myblog/places/views.py", line 22, in <module>
from posts.views import *
File "/Users/save/sites/myblog/posts/views.py", line 31, in <module>
from maps.views import render_map_geoloc
ImportError: cannot import name render_map_geoloc
Its because of cyclic dependency or circular reference.
b depends on c
c depends on d
d depends on b #which depends on c
Not sure for what purpose you are using. But you do explicit import to that function, and right above where its been used.
Looking at the error you are getting, it might be because of some dependency expected for d is coming from b so if you from b.views import *, it gets you that dependency. But if you import specific view (my_func), its missing that dependency.
Some more details you can find on SO answer thread - Django App Dependency Cycle

renderContents in beautifulsoup (python)

The code I'm trying to get working is:
h = str(heading)
# '<h1>Heading</h1>'
heading.renderContents()
I get this error:
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
print h.renderContents()
AttributeError: 'str' object has no attribute 'renderContents'
Any ideas?
I have a string with html tags and i need to clean it if there is a different way of doing that please suggest it.
Your error message and your code sample don't line up. You say you're calling:
heading.renderContents()
But your error message says you're calling:
print h.renderContents()
Which suggests that perhaps you have a bug in your code, trying to call renderContents() on a string object that doesn't define that method.
In any case, it would help if you checked what type of object heading is to make sure it's really a BeautifulSoup instance. This works for me with BeautifulSoup 3.2.0:
from BeautifulSoup import BeautifulSoup
heading = BeautifulSoup('<h1>heading</h1>')
repr(heading)
# '<h1>heading</h1>'
print heading.renderContents()
# <h1>heading</h1>
print str(heading)
# '<h1>heading</h1>'
h = str(heading)
print h
# <h1>heading</h1>

Categories