您好,欢迎来到知库网。
搜索
您的当前位置:首页scrapy 实例保存抓取图片结果到mysql

scrapy 实例保存抓取图片结果到mysql

来源:知库网
twitter
import scrapy
from downloadimg.items import DownloadimgItem
import MySQLdb

class DownloadingImg(scrapy.Spider):
    name = 'dlimg'
    # start_urls = 
    start_urls = 

    # DB info
    db = MySQLdb.connect(host="192.168.1.107",user="lizhe",passwd="", db="test") 
    cur = db.cursor()

    def parse(self, response):
        imagelist = response.css('img::attr(src)').extract()
        imagelistnew = [ x for x in imagelist if 'blank.gif' not in x ]

        print str(len(imagelistnew)) + 'record will be inserted'
        for full_url in imagelistnew:
            # full_url = response.urljoin(imageurl)
            print full_url
            name_arr = full_url.split('/')[-1].split('-')
            del name_arr[-1]
            name = " ".join(name_arr)
            res = self.insert_data(name, full_url)    

    def insert_data(self, name, full_url):    
        # print 'name ' + name + 'full_url ' + full_url
        statement = "INSERT INTO imgstore (imgname, imgurl) VALUES ('{}', '{}');".format(name, full_url)
        # print statement

        # self.cur.execute("SELECT * FROM employee")
        self.cur.execute(statement)
        


提供ahk, python 自动化脚本服务:

本文如未解决您的问题请添加抖音号:51dongshi(抖音搜索懂视),直接咨询即可。

热门图文

Copyright © 2019-2025 zicool.com 版权所有 湘ICP备2023022495号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务