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)
Related
Hello guys now I know there are solutions on stack overflow I just did not understand them, here is my code
enter code here
import urllib.request import urlopen
def read_text():
quotes = open(r"C:\Users\LEON\Desktop\python\Swear_words.txt")
contents_of_file = quotes.read()
print(contents_of_file)
quotes.close()
check_profanity(contents_of_file)
def check_profanity(text_to_check):
connection =
urllib.urlopen("http://www.wdylike.appspot.com/q=s"+text_to_check)
output = connection.read()
read_text()
error
NameError: name 'urllib' is not defined
The import statement itself has incorrect syntax. It should/could be
from urllib.request import urlopen
Then the 3rd last line doesn't need the urllib prefix
urlopen("http://www.wdylike.appspot.com/q=s"+text_to_check)
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.
How can I use the python six library for 2 and 3 compatibility on the foll. code sample:
import urllib.request
wp = urllib.request.urlopen("http://google.com")
pw = wp.read()
print(pw)
-- EDIT I tried this:
from six.moves.urllib.request import urlopen, urlretrieve
import six.moves.urllib.request as Request
request = Request('http://google.com')
but get this error:
TypeError: 'Module_six_moves_urllib_request' object is not callable
You've almost had it:
from six.moves.urllib.request import urlopen
wp = urlopen("http://google.com")
pw = wp.read()
print(pw)
Or if you wanted to addess urllib directly as in the first attempt, use from six.moves import urllib.
So I'm accessing the poloniex API with python and this is my code:
from poloniex import Poloniex
import krakenex
import threading
import pprint
import urllib.request
import json
####POLONIEX####
#FUNCTIONS
polo = Poloniex()
def BTC_USDT_LAST_POLONIEX():
polo = Poloniex()
threading.Timer(1.0, BTC_USDT_LAST_POLONIEX).start() # called every minute
print("BTC Last Price = " + (polo('returnTicker')['USDT_BTC']['last']))
def POLONIEX_ASSET_LIST():
pprint.pprint(sorted(list(polo('returnTicker'))))
Everything is working so far and I want to avoid using urllib as its a pain to turn a http request into a list. I'm trying to access the order book but get the following error:
>>> polo('returnOrderBook')
Traceback (most recent call last):
File "<pyshell#27>", line 1, in <module>
polo('returnOrderBook')
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/poloniex/retry.py", line 15, in wrapped
return function(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/poloniex/__init__.py", line 183, in __call__
return self.parseJson(ret.text)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/poloniex/__init__.py", line 197, in parseJson
raise PoloniexError(jsonout['error'])
poloniex.PoloniexError: Please specify a currency pair.
I've tried specifying the currency pair but have no idea how to plug it in.
Rewrite your code and use requests module instead of urllib:
import requests
ret = requests.get('http://poloniex.com/public?command=returnOrderBook¤cyPair=BTC_BCN').json()
print ret
>>> {u'bids': [[u'0.00000034', 20629605.566027], [u'0.00000033', 43382683.465305], [u'0.00000032', 70007976.087993], [u'0.00000031', 49571221.248027], [u'0.00000030', 77520227.415484], [u'0.00000029', 46037827.046996], [u'0.00000028', 26267440.401662], [u'0.00000027', 22511987.85933], [u'0.00000026', 18885378.040015], [u'0.00000025', 13313109.292994], [u'0.00000024', 6243527.5236432], [u'0.00000023', 7504850.7832509], [u'0.00000022', 8443683.7997507], [u'0.00000021', 8996262.9826951], [u'0.00000020', 24601532.006268], [u'0.00000019', 26853346.478659], [u'0.00000018', 6027262.24889 etc....
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.