程序员开发实例大全宝库

网站首页 > 编程文章 正文

Python实现从CSV文件(数据库)批量提取图片链接

zazugpt 2024-10-20 15:54:09 编程文章 15 ℃ 0 评论

因为老网站内容使用了第三方的网站的图片外链,随时都可能有图片打不开的风险。于是需要把30多兆的数据库文件中的图片链接提取出来并批量下载下来到oss存储。



首先把网站数据库导出成CSV文件,通过PYthon脚本批量提取出csv文件里面的图片链接。(csv文件需要13M以内)


以下代码提取csv中的图片链接,并打印出来。

import csv

import re


def extract_image_links(csv_file):

image_links = []

with open(csv_file, 'r',encoding='utf-8') as file:

reader = csv.reader(file)

for row in reader:

for item in row:

# 使用正则表达式提取图片链接

matches = re.findall(r'(https?://\S+\.png|https?://\S+\.jpg|https?://\S+\.jpeg|https?://\S+\.gif)', item)

if matches:

image_links.extend(matches)

return image_links


csv_file = '你的文件.csv'

image_links = extract_image_links(csv_file)

for link in image_links:

print(link)


原创转载需要经过作者同意



本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表