zoukankan      html  css  js  c++  java
  • python 正则提取img标签和src

    需要用python写个脚本出来提取img标签和src的内容,在存数据的时候,搞藤了很久,原因是把list类型的数据直接放入sql语句里面了,一直报下面这个错误

    脑子抽了,以为是src里面转义字符的问题,就一直往这个方向整

    后面才发现,是直接把list类型放sql format里面了,然后将[]一起转成了字符串  如 '[' http://www.baidu.com ']'

    执行的时候将'['作为了一个字符,后面的http.......就肯定识别不到了嘛,,哎呀,,,笨

    解决:将list的元素插入sql 占位符对应位置,而不是将 imgSrc直接放img占位的地方

     源码如下:

    # coding=utf-8
    import pymssql
    import re
    
    
    def connectDB():
        conn = pymssql.connect(server='****', user='User', password='****', database='*****',
                               charset='cp936')
        cur = conn.cursor()
        sql = 'select  ProductID,Content from Products WHERE (not Content IS NULL )'
        cur.execute(sql)
        row = cur.fetchone()
        resultList = []
        while row:
            # print("ProductID=%s,Content=%s" % (row[0], row[1]))
            result = parseContent(row[1])
            if result:
                tmp = []
                # print("解析出的img为:")
                # print(result)
                # tmp.append(int(row[0]))  # 将productID转成int类型,方便下面的比较
                tmp.append(row[0])
                tmp.append(result)
                resultList.append(tmp)
            try:
                row = cur.fetchone()
            except UnicodeDecodeError:
                continue
        conn.close()
        return resultList
    
    
    def parseContent(content):
        pattern = '<img[^>]*/>'
    
        result = re.findall(pattern, content)
        return result
    
    
    def saveImg(resultList):
        productIdList = getExtraBookProductIDList()
          conn = pymssql.connect(server='****', user='User', password='****', database='*****',
                               charset='cp936')
        cur = conn.cursor()
        for result in resultList:  # 遍历解析出来的imgList
    
            if result[0] in productIdList:
                # 提取src
                imgSrc = getImgSrc(result[1])
                for img in imgSrc:
                    sql_1 = """update ExtraBookInfo set YImage='{img}' WHERE ProductID='{pID}'""".format(
                        img=img, pID=result[0])
                    print(sql_1)
                    cur.execute(sql_1)
                    conn.commit()
            else:
                # sql_2 = """insert into ExtraBookInfo (ProductID,YImage) values( '{pID}','{img}')""".format(
                #     pID=result[0], img=tmp)
                for img in imgSrc:
                    cur.execute('insert into ExtraBookInfo ProductID,YImage values(%s,%s)', (result[0], img))
                    conn.commit()
    
        conn.close()
    
    
    def getExtraBookProductIDList():
        conn = pymssql.connect(server='****', user='User', password='****', database='*****',
                               charset='cp936')
        cur = conn.cursor()
        sql = 'select  ProductID from ExtraBookInfo'
        cur.execute(sql)
        productIdList = []
        row = cur.fetchone()
        while row:
            productIdList.append(row[0])
            try:
                row = cur.fetchone()
            except UnicodeDecodeError:
                continue
      
        conn.close()
        return productIdList
    
    
    def getImgSrc(result):
        for r in result:
            pattern_2 = 'http.*?.jpg'
            p2 = re.findall(pattern_2, r)
            print(p2)
        return p2
    
    
    
    resultList = connectDB()
    saveImg(resultList)

     *********

    *******

    不要轴。。。。。。。。

    知止而后有定,定而后能静,静而后能安,安而后能虑,虑而后能得
  • 相关阅读:
    win7连接l2tp报错789“L2TP连接尝试失败,因为安全层在初始化与远程计算机的协商时遇到了一个处理错误”
    Django内置Admin
    python-django(day19)
    常用模块-day06
    生成器、内置函数、模块与包的使用、正则表达式-day05
    Python 函数对象、 名称空间与作用域、闭包、装饰器、迭代器
    python字符编码、文件处理、函数
    2018-06-19——考试周肝完的我又回来了
    2018-06-11——pthyon类型整理
    2018-06-10——python基础整理
  • 原文地址:https://www.cnblogs.com/taoHongFei/p/9116902.html
Copyright © 2011-2022 走看看