Python Forum

Full Version: Problems with using request in python 3
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Im new to Python, and Im learning it fro the new boston's guide on youtube.
im having trouble downloading csv files through python.
this is the code -

from urllib import request

goog_url = 'http://www.google.com/finance/historical?q=NASDAQ:GOOG&output=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)
The error im receiving is-

Traceback (most recent call last):
File "C:/Users/user/PycharmProjects/untitled/New_Boston_ep_24.py", line 1, in <module>
from urllib.request import urlopen
ImportError: No module named request



PS im new to forums, so let me know if i made any mistake in posting. Help is appreciated
I added python tags please use them in all future posts. (see BBCODE)
use requests, you will have to install the package, if you are unfamiliar with the install process, open a command window and type:
pip install requests
code:
import requests


def download_stock_data(url, savefile):
    page =  requests.get(url, allow_redirects=False)
    if page.status_code == 200:
        with open(savefile, 'wb') as f:
            f.write(page.content)
    else:
        print('Error downloading file')
        # ... more processing here ...

def main():
    download_stock_data(url='http://www.google.com/finance/historical?q=NASDAQ:GOOG&output=csv',
                        savefile='goog.csv')

if __name__ == '__main__':
    main()
But .... Google responds with:
Quote:Google
Sorry...
We're sorry...

... but your computer or network may be sending automated queries. To protect our users, we can't process your request right now.
See Google Help for more information.
Change the User-agent in the request.
Okay, so im guessing the url is invalid
but i have a doubt, it may be silly, but how exactly do i open a 'command window' i use pycharm community edition. is it the command line thing? i tried entering that lne there, but it says invalid syntax

(Feb-15-2018, 07:40 AM)wavic Wrote: [ -> ]Change the User-agent in the request.

the what now (im really new, sry)