我正在尝试编写代码来循环访问网站上的〜90k页面以便抓取数据,并且我试图开始使用“with”语句。 我的csv文件有一列名为“符号”,我可以分享,如果需要。
我知道这是一个语法错误,但我没有看到错误是什么。 我已经尝试将最后一行更改为其他内容,但我总是得到相同的错误。 我在另一个线程中读到“with”会自动关闭,所以我不知道我该怎么做。
就我最终的计划而言,我想写一个循环来遍历所有90k网站,搜集数据。 我有一些后续步骤的代码,所以如果有更好的方法让我做这一步,我就会对其他解决方案感兴趣。
谢谢!
我的代码:
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!")文件“”,第7行print(“hooray!”)^ SyntaxError:解析时意外的EOF
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
最满意答案
您需要为您的尝试添加except语句。
喜欢这个:
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!')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!')更多推荐
发布评论