跳过图像URL当HTTP错误出现在CSV文件中时
我有一个项目,试图从网站上进行映像。我使用带有所有URL的CSV文件。我没有打开的一些URL(或者不存在)。我从Phyton中获得了HTTP错误403。我只想尝试在CSV文件中尝试下一个URL并忽略错误。
import urllib.request
import csv
with open ('urls_01.csv') as images:
images = csv.reader(images)
img_count = 1
for image in images:
urllib.request.urlretrieve(image[0],
'images/image_{0}.jpg'.format(img_count))
img_count += 1
这是错误
Traceback (most recent call last):
File "c:\Users\Heigre\Documents\Phyton\img_test.py", line 8, in <module>
urllib.request.urlretrieve(image[0],
File "C:\Program Files\Python310\lib\urllib\request.py", line 241, in urlretrieve
with contextlib.closing(urlopen(url, data)) as fp:
File "C:\Program Files\Python310\lib\urllib\request.py", line 216, in urlopen
return opener.open(url, data, timeout)
File "C:\Program Files\Python310\lib\urllib\request.py", line 525, in open
response = meth(req, response)
File "C:\Program Files\Python310\lib\urllib\request.py", line 634, in http_response
response = self.parent.error(
File "C:\Program Files\Python310\lib\urllib\request.py", line 563, in error
return self._call_chain(*args)
File "C:\Program Files\Python310\lib\urllib\request.py", line 496, in _call_chain
result = func(*args)
File "C:\Program Files\Python310\lib\urllib\request.py", line 643, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
I have a project trying to imagescrape from a website. I use a csv file with all the urls. Some urls i dont have the premission to open(or they dont exist). I get a Http error 403 in phyton from those. I just want the try the next url in the csv file and ignore the error.
import urllib.request
import csv
with open ('urls_01.csv') as images:
images = csv.reader(images)
img_count = 1
for image in images:
urllib.request.urlretrieve(image[0],
'images/image_{0}.jpg'.format(img_count))
img_count += 1
This is the error
Traceback (most recent call last):
File "c:\Users\Heigre\Documents\Phyton\img_test.py", line 8, in <module>
urllib.request.urlretrieve(image[0],
File "C:\Program Files\Python310\lib\urllib\request.py", line 241, in urlretrieve
with contextlib.closing(urlopen(url, data)) as fp:
File "C:\Program Files\Python310\lib\urllib\request.py", line 216, in urlopen
return opener.open(url, data, timeout)
File "C:\Program Files\Python310\lib\urllib\request.py", line 525, in open
response = meth(req, response)
File "C:\Program Files\Python310\lib\urllib\request.py", line 634, in http_response
response = self.parent.error(
File "C:\Program Files\Python310\lib\urllib\request.py", line 563, in error
return self._call_chain(*args)
File "C:\Program Files\Python310\lib\urllib\request.py", line 496, in _call_chain
result = func(*args)
File "C:\Program Files\Python310\lib\urllib\request.py", line 643, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不确定是否需要导入,但是我根据您的错误输出提供了该导入。还提供了一个打印语句的想法,如果您只需要通过特定错误,可能会有所帮助...
Not sure if the import is needed, but I provided it based on your error output. Also provided an idea of a print statement that might help if you need only pass on specific errors…
使用尝试块捕获HTTP错误:
Use a try block to catch the http error:
使用尝试块捕获HTTP错误:
Use a try block to catch the http error: