带语句的Python

编程入门 行业动态 更新时间:2024-10-21 19:43:06
语句的Python - SyntaxError:解析时意外的EOF(Python with statement - SyntaxError: unexpected EOF while parsing)

我正在尝试编写代码来循环访问网站上的〜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!')

更多推荐

本文发布于:2023-04-29 10:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335920.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语句   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!