Python Forum
Problems with using request in python 3
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problems with using request in python 3
#1
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
Reply
#2
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.
Reply
#3
Change the User-agent in the request.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Debian 11 Bullseye | Python 3.9.x | pip install mysql-connector-python-rf problems BrandonKastning 4 6,574 Feb-05-2022, 08:25 PM
Last Post: BrandonKastning
  how can I correct the Bad Request error on my curl request tomtom 8 4,968 Oct-03-2021, 06:32 AM
Last Post: tomtom
  Python Request Errors PythonNoob1998 7 3,769 Jan-07-2021, 05:18 PM
Last Post: buran
  ImportError: cannot import name 'Request' from 'request' abhishek81py 1 3,860 Jun-18-2020, 08:07 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020