buckyroberts / Source-Code-from-Tutorials

Here is the source code from all of my tutorials.
https://www.youtube.com/user/thenewboston
2.18k stars 2.9k forks source link

this is regarding the python source code tutorial 24 , seeing errors when trying trying to run the code #129

Open dsurana opened 6 years ago

dsurana commented 6 years ago

I am using the latest available Python 3.7, Please help me in resolving this issue and make my code run

from urllib import request

goog_url = http://real-chart.finance.yahoo.com/table.csv?s=GOOG&d=2&e=8&f=2015&g=d&a=2&b=27&c=2014&ignore=.csv

def download_stock_data(csv_url): response = request.urlopen(csv_url) csv = response.read() csv_str = str(csv) lines = csv_str.split("\n") dest_url = r'goog.csv' fx = open(dest_url, "w") for line in lines: fx.write(line + "\n") fx.close()

download_stock_data(goog_url)

After running this above code seeing following errors C:\Users\Pritam\PycharmProjects\Ganesha_First\venv\Scripts\python.exe C:/Users/Pritam/PycharmProjects/Ganesha_First/bucky.py Traceback (most recent call last): File "C:\Users\Pritam\Desktop\Dhiraj_techdocs\lib\urllib\request.py", line 1317, in do_open encode_chunked=req.has_header('Transfer-encoding')) File "C:\Users\Pritam\Desktop\Dhiraj_techdocs\lib\http\client.py", line 1229, in request self._send_request(method, url, body, headers, encode_chunked) File "C:\Users\Pritam\Desktop\Dhiraj_techdocs\lib\http\client.py", line 1275, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "C:\Users\Pritam\Desktop\Dhiraj_techdocs\lib\http\client.py", line 1224, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "C:\Users\Pritam\Desktop\Dhiraj_techdocs\lib\http\client.py", line 1016, in _send_output self.send(msg) File "C:\Users\Pritam\Desktop\Dhiraj_techdocs\lib\http\client.py", line 956, in send self.connect() File "C:\Users\Pritam\Desktop\Dhiraj_techdocs\lib\http\client.py", line 928, in connect (self.host,self.port), self.timeout, self.source_address) File "C:\Users\Pritam\Desktop\Dhiraj_techdocs\lib\socket.py", line 707, in create_connection for res in getaddrinfo(host, port, 0, SOCK_STREAM): File "C:\Users\Pritam\Desktop\Dhiraj_techdocs\lib\socket.py", line 748, in getaddrinfo for res in _socket.getaddrinfo(host, port, family, type, proto, flags): socket.gaierror: [Errno 11001] getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:/Users/Pritam/PycharmProjects/Ganesha_First/bucky.py", line 16, in download_stock_data(goog_url) File "C:/Users/Pritam/PycharmProjects/Ganesha_First/bucky.py", line 6, in download_stock_data response = request.urlopen(csv_url) File "C:\Users\Pritam\Desktop\Dhiraj_techdocs\lib\urllib\request.py", line 222, in urlopen return opener.open(url, data, timeout) File "C:\Users\Pritam\Desktop\Dhiraj_techdocs\lib\urllib\request.py", line 525, in open response = self._open(req, data) File "C:\Users\Pritam\Desktop\Dhiraj_techdocs\lib\urllib\request.py", line 543, in _open '_open', req) File "C:\Users\Pritam\Desktop\Dhiraj_techdocs\lib\urllib\request.py", line 503, in _call_chain result = func(*args) File "C:\Users\Pritam\Desktop\Dhiraj_techdocs\lib\urllib\request.py", line 1345, in http_open return self.do_open(http.client.HTTPConnection, req) File "C:\Users\Pritam\Desktop\Dhiraj_techdocs\lib\urllib\request.py", line 1319, in do_open raise URLError(err) urllib.error.URLError: <urlopen error [Errno 11001] getaddrinfo failed>

Process finished with exit code 1

Pihu1998 commented 5 years ago

The url should be enclosed within single quotes and indents should be given before every statements under a function and a loop. Above all, the url used is invalid i.e it no longer exists.

boiledeggs commented 5 years ago

As @Pihu1998 pointed out, you need to quote the link and also need indentations all along.

Also, Yahoo Finance is no longer allows downloading csv's. Gives an unauthorised error. I would suggest using some other free website for your source data. Try: https://support.spatialkey.com/spatialkey-sample-csv-data/