长文本上传的图片没法下载

用python读取到表格数据,获得表格中的长文本:
例如:
在每个产品TDS下有相关抗原的UniProt链接
SeaTable
Protein Database(简称 UniProt)是信息最丰富、资源最广的蛋白质序列数据库

我们检测出图片url, 但是下载出错了,错误发生在base.get_file_download_link,

参考代码来自:https://github.com/seatable/seatable-api-python/blob/master/demo/file_download.py

日志是:ConnectionError: [Errno 400] {“error_msg”:“asset not found.”}

这个还是 URL 地址的问题。你可以用 chrome debug 模式看下从网页上下载这个图片文件用的是什么地址。

那这个该怎么办呢?我用Python读取表格,给我返回的数据,就是这个:

我把这个图片,在chrome浏览器一贴,会转化为
https://cloud.seatable.cn/seafhttp/files/674c429d-cb49-46ad-870a-cc403cb3d088/image-1629164211446.png,这个转化,我估计和python的这个api做的是一样的事情:
base.get_file_download_link(path)

文件下载的参数是需要截取图片url中的一部分的, 请参照以下代码

img_url = "https://cloud.seatable.cn/workspace/35610/asset/8ce5b31b-835d-485a-9e98-d5f6bc5d1b98/images/auto-upload/image-1693193448840.png"
# 获取下载链接需要的内容
img_url_for_download = "images/auto-upload/image-1693193448840.png" 
download_url = base.get_file_download_link(img_url_for_download)
print(download_url)

感谢答复。确实要截取url的一部分,我日志也是截取成功的:
download_png, path /images/auto-upload/image-1631258572005.png

我的代码
def download_png(seatable_base, url, dir_name):
path = parse.unquote(url[url.find(‘/images/’):])
print("download_png, path ", path)

file_url = seatable_base.get_file_download_link(path)
file_name = parse.unquote(file_url.split('/')[-1])
print("download_png, file_url ", file_url, "file_name ", file_name)
try:
    response = requests.get(file_url)

这个代码,我尝试下载一个自己本地上传的图的url,可以下载,但是去下载一个大家一起共享的长文本中的图时,就出错了,所以我不知道是不是长文本下,是不是不能下载,还是有其他规则要遵守才能下载。

有没有可能带有auto-upload的图片,就不能下载呢? :joy:

通过 get_file_download_link的方法获取的file_url可以正确获取吗? 如果可以获取的话,用request.get应该是可以读取到这个链接返回的文件的, 你用file.write把这个文件写入本地就好了

不能呀,如我最初提的,错误就发生在这个get_file_download_link的函数里。
这个是堆栈:

url   https://cloud.seatable.cn/workspace/74243/asset/1a98a5ac-853e-480a-b258-2f9944f625b7/images/auto-upload/image-1631258572005.png
download_png, path  /images/auto-upload/image-1631258572005.png
Traceback (most recent call last):
  File "D:\work\python\seatable_to_wechat\test_seatable.py", line 86, in <module>
    result = download_png(base, url, LOCAL_SEATABLE_PATH)
  File "D:\work\python\seatable_to_wechat\test_seatable.py", line 40, in download_png
    file_url = seatable_base.get_file_download_link(path)
  File "C:\Users\chenxiaofeng\AppData\Local\Programs\Python\Python310\lib\site-packages\seatable_api\main.py", line 461, in get_file_download_link
    data = parse_response(response)
  File "C:\Users\chenxiaofeng\AppData\Local\Programs\Python\Python310\lib\site-packages\seatable_api\main.py", line 41, in parse_response
    raise ConnectionError(response.status_code, response.text)
ConnectionError: [Errno 400] {"error_msg":"path /images/auto-upload/image-1631258572005.png not found."}

这个是我的源码:

def download_png(seatable_base, url, dir_name):
    #example https://cloud.seatable.cn/workspace/74243/asset/1a98a5ac-853e-480a-b258-2f9944f625b7/images/auto-upload/image-1631245687860.png
    path = parse.unquote(url[url.find('/images/'):])
    print("download_png, path ", path)

    file_url = seatable_base.get_file_download_link(path)
    file_name = parse.unquote(file_url.split('/')[-1])
    print("download_png, file_url ", file_url, "file_name ", file_name)
    try:
        response = requests.get(file_url)
    except Exception as e:
        print('fail to download file/image')
        return ""
    if response.status_code != 200:
        print('fail to download  file/image')
        return ""

url我是python脚本,读取表格的长文本读取到的,长文本读取到的日志:

{'_id': 'bQI-Ve5pRCS0SyC7mEd_2A', '_mtime': '2023-08-26T03:51:16.456+00:00', '_ctime': '2023-08-23T08:47:21.957+00:00', 'create_day': '2021-08-17', 'brand': 'Biolegend', 'product_line': '流式相关', 'applicable_user': '技术', 'question': '如何寻找抗原信息?', 'answer': '### 答:\n\n在每个产品TDS下有相关抗原的UniProt链接
,如图所示:\n\n![](https://cloud.seatable.cn/workspace/74243/asset/1a98a5ac-853e-480a-b258-2f9944f625b7/images/auto-upload/image-1629164211446.png)\n\nUnified
Protein Database(简称 UniProt)是信息最丰富、资源最广的蛋白质序列数据库

那你去base表格的文件管理中 系统文件夹/images/auto-upload/下看看有没有这个文件吧

看了一下,没有呢。因为表格是多人共享的,图片是其他同学挺久以前上传的。 我打开长文本,能浏览这个图,但是你说的文件管理,看不到这个图。

不介意的话, 表格私信共享我一下吧, 顺便给我一个 api_token 我去给您看看

当然可以,不过好像去你个人主页,找不到私信入口,要么你给我个邮箱,我发给你表格链接?

邮箱我用私信发给您了