相同的功能对相同类型的数据有不同的作用
Same function acting differently same type of data
我可能已经在这上面花了太长时间,但我发现很难理解为什么我会收到 FileNotFoundError: [Errno 2] No such file or directory: when the only difference I can see is link。
使用美汤
Objective:
下载图像并放置在不同的文件夹中,除了某些 .jpg 文件外,该文件夹工作正常。
我尝试了不同类型的路径和文件名条带化,但它是同样的问题。
测试图片:
http://img2.rtve.es/v/5437650?w=1600&preview=1573157283042.jpg # 不工作
http://img2.rtve.es/v/5437764?w=1600&preview=1573172584190.jpg #工作完美
函数如下:
def get_thumbnail():
'''
Download image and place in the images folder
'''
soup = BeautifulSoup(r.text, 'html.parser')
# Get thumbnail image
for preview in soup.findAll(itemprop="image"):
preview_thumb = preview['src'].split('//')[1]
# Download image
url = 'http://' + str(preview_thumb).strip()
path_root = Path(__file__).resolve().parents[1]
img_dir = str(path_root) + '\static\images\'
urllib.request.urlretrieve(url, img_dir + show_id() + '_' + get_title().strip()+ '.jpg')
使用的其他函数:
def show_id():
for image_id in soup.findAll(itemprop="image"):
preview_id = image_id['src'].split('/v/')[1]
preview_id = preview_id.split('?')[0]
return preview_id
def get_title():
title = soup.find('title').get_text()
return title
我所能解决的问题是必须找到第一张图像的图像文件夹,但第二张图像完美无缺。
这是我不断遇到的错误,它似乎在 request.py
感谢任何意见。
很可能图像文件名中的 "special characters" 正在抛出 urlretrieve()
(以及其中使用的 open()
):
>>> from urllib import urlretrieve # Python 3: from urllib.request import urlretrieve
>>> url = "https://i.stack.imgur.com/1RUYX.png"
>>> urlretrieve(url, "test.png") # works
('test.png', <httplib.HTTPMessage instance at 0x10b284a28>)
>>> urlretrieve(url, "/tmp/test 07/11/2019.png") # does not work
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 98, in urlretrieve
return opener.retrieve(url, filename, reporthook, data)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 249, in retrieve
tfp = open(filename, 'wb')
IOError: [Errno 2] No such file or directory: '/tmp/test 07/11/2019.png'
换句话说,您用作文件名的图像标题必须正确 pre-formatted 才能用作文件名进行保存。我只是 "slugify" 他们 以避免出现问题。一种方法是简单地使用 slugify
module:
import os
from slugify import slugify
image_filename = slugify(show_id() + '_' + get_title().strip()) + '.jpg'
image_path = os.path.join(img_dir, image_filename)
urllib.request.urlretrieve(url, image_path)
例如,这就是 slugify 对 test 07/11/2019
图片名称所做的:
>>> slugify("test 07/11/2019")
'test-07-11-2019'
另请参阅:
我可能已经在这上面花了太长时间,但我发现很难理解为什么我会收到 FileNotFoundError: [Errno 2] No such file or directory: when the only difference I can see is link。 使用美汤
Objective: 下载图像并放置在不同的文件夹中,除了某些 .jpg 文件外,该文件夹工作正常。 我尝试了不同类型的路径和文件名条带化,但它是同样的问题。
测试图片:
http://img2.rtve.es/v/5437650?w=1600&preview=1573157283042.jpg # 不工作
http://img2.rtve.es/v/5437764?w=1600&preview=1573172584190.jpg #工作完美
函数如下:
def get_thumbnail():
'''
Download image and place in the images folder
'''
soup = BeautifulSoup(r.text, 'html.parser')
# Get thumbnail image
for preview in soup.findAll(itemprop="image"):
preview_thumb = preview['src'].split('//')[1]
# Download image
url = 'http://' + str(preview_thumb).strip()
path_root = Path(__file__).resolve().parents[1]
img_dir = str(path_root) + '\static\images\'
urllib.request.urlretrieve(url, img_dir + show_id() + '_' + get_title().strip()+ '.jpg')
使用的其他函数:
def show_id():
for image_id in soup.findAll(itemprop="image"):
preview_id = image_id['src'].split('/v/')[1]
preview_id = preview_id.split('?')[0]
return preview_id
def get_title():
title = soup.find('title').get_text()
return title
我所能解决的问题是必须找到第一张图像的图像文件夹,但第二张图像完美无缺。
这是我不断遇到的错误,它似乎在 request.py
感谢任何意见。
很可能图像文件名中的 "special characters" 正在抛出 urlretrieve()
(以及其中使用的 open()
):
>>> from urllib import urlretrieve # Python 3: from urllib.request import urlretrieve
>>> url = "https://i.stack.imgur.com/1RUYX.png"
>>> urlretrieve(url, "test.png") # works
('test.png', <httplib.HTTPMessage instance at 0x10b284a28>)
>>> urlretrieve(url, "/tmp/test 07/11/2019.png") # does not work
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 98, in urlretrieve
return opener.retrieve(url, filename, reporthook, data)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 249, in retrieve
tfp = open(filename, 'wb')
IOError: [Errno 2] No such file or directory: '/tmp/test 07/11/2019.png'
换句话说,您用作文件名的图像标题必须正确 pre-formatted 才能用作文件名进行保存。我只是 "slugify" 他们 以避免出现问题。一种方法是简单地使用 slugify
module:
import os
from slugify import slugify
image_filename = slugify(show_id() + '_' + get_title().strip()) + '.jpg'
image_path = os.path.join(img_dir, image_filename)
urllib.request.urlretrieve(url, image_path)
例如,这就是 slugify 对 test 07/11/2019
图片名称所做的:
>>> slugify("test 07/11/2019")
'test-07-11-2019'
另请参阅: