0

I am trying to write code to loop through ~90k pages on a website in order to scrape data, and I'm trying to get started using a "with" statement. My csv file has one column called "Symbol" and I can share that if needed.

I understand that this is a syntax error, but I don't see what the error is. I've tried changing the last line to something else, but I always get the same error. I read in another thread that "with" closes itself, so I'm not sure what else I should do.

In terms of my eventual plans, I would like to write a loop to iterate through all 90k websites, scraping data. I have code for some of the subsequent steps, so if there is a better way for me to do this step I am all ears about other solutions.

Thank you!

My code:

with open('~/plants_symbols.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        try:
            rkey = requests.get('https://plants.usda.gov/java/reference' + row['Symbol'])
            if rkey.status_code == 200: 
                print("hooray!")

File "", line 7 print("hooray!") ^ SyntaxError: unexpected EOF while parsing

1 Answers1

0

You need to add an except statement for your try.

Like this:

import requests

with open('~/plants_symbols.csv') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        try:
            rkey = requests.get('https://plants.usda.gov/java/reference' + row['Symbol'])
            if rkey.status_code == 200:
                print("hooray!")
        except Exception: 
            print('Error!')
Nick Weseman
  • 1,502
  • 3
  • 16
  • 22
  • Ah!! Thank you! I'm happy it was so simple. Silly mistake, but it's all great in terms of the learning process. –  May 05 '17 at 11:54
  • Hey! I've tried to use your fix, but I'm not getting any print output. I realized that I was using the " symbols around my if and you used ' symbols around the except statement, so I changed them to be uniform. I also messed around with indentation, but it's still not working for me. Also, after I run this I can't get my print function to work on a test, either. Any ideas? –  May 05 '17 at 17:19
  • Hi! The "try" and "with" look like they might be inverted, based on my reading of this: http://stackoverflow.com/questions/5205811/catching-an-exception-while-using-a-python-with-statement-part-2 –  May 05 '17 at 17:26