访问数据库中的图片地址,判断是否能正常打开

假设有A表,包含图片地址,需要判断图片是否能正常打开,并返回错误代码。

import psycopg2
import psycopg2.extras
import requests
from config.load_config import get_connect_info
from functools import lru_cache
from concurrent.futures import ThreadPoolExecutor



class Link_Postgres():
    def __init__(self):
        conf1, conf2 = get_connect_info()
        self.conn1 = psycopg2.connect(
            dbname=conf1["dbname"],
            user=conf1["user"],
            password=conf1["password"],
            host=conf1["host"],
            port=conf1["port"]
        )
        self.cur1 = self.conn1.cursor()


    def deal_head_picture_thread_pool(self):
        self.cur1.execute("select asinid,goods from A表")
        rows = self.cur1.fetchall()

        executor = ThreadPoolExecutor(max_workers=10)
        for row in rows:
            id = row[0]
            head_url = row[1]
            executor.submit(self.deal_head_picture, id,head_url)
        executor.shutdown(wait=True)
        print('***读取完成***')


    @lru_cache(maxsize=128)
    def deal_head_picture(self, id,head_url):
        if head_url != None:
            print(head_url)
            headers = {
                'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36'
            }
            response = requests.get(url=head_url, headers=headers)
            if response.status_code != 200 :
                self.cur1.execute("INSERT INTO B表(asinid,goods,remark)VALUES(%s,%s,%s)",(id,head_url,response.status_code))
                self.conn1.commit()
                print(f'{head_url}----{response.status_code}图片打不开----')





lp = Link_Postgres()
lp.deal_head_picture_thread_pool()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值