首页 > 其他分享 >读书网入库练习

读书网入库练习

时间:2023-10-05 15:33:36浏览次数:34  
标签:settings self 练习 DB spider item conn 读书网 入库

settings.py

DB_HOST = 'localhost'
DB_PORT = 3306
DB_USER = 'root'
DB_PWD = '1234'
DB_NAME = 'guli'
DB_CHARSET = 'utf8'
# Configure item pipelines
# See https://docs.scrapy.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
   'readPro.pipelines.ReadproPipeline': 300,
}

  pipelines.py

from scrapy.utils.project import get_project_settings
import pymysql

class ReadproPipeline:
    def open_spider(self, spider):
        settings = get_project_settings()
        self.host = settings['DB_HOST']


        self.port = settings['DB_PORT']
        self.user = settings['DB_USER']
        self.pwd = settings['DB_PWD']
        self.name = settings['DB_NAME']
        self.charset = settings['DB_CHARSET']
        print(self.host)
        print(self.pwd)
        self.connect()

    def connect(self):
        self.conn = pymysql.connect(host=self.host,
                                    port=self.port,
                                    user=self.user,
                                    password=self.pwd,
                                    db=self.name,
                                    charset=self.charset)

        self.cursor = self.conn.cursor()

    def process_item(self, item, spider):
        sql = 'insert into book(name,src) values("{}","{}")'.format(item['name'],item['src'])
        self.cursor.execute(sql)
        self.conn.commit()
        return item

    def close_spider(self, spider):
        self.conn.close()
        self.cursor.close()

  

标签:settings,self,练习,DB,spider,item,conn,读书网,入库
From: https://www.cnblogs.com/sgj191024/p/17743410.html

相关文章

  • 函数练习错题
    A函数的返回值不能一次返回两个数例如inttest(){}return3,4;           intmain(){          最后的结果不是3,4而是4,因为这是个逗号表达式,整个表达式会从}intret=test();     左到右依次计算,但是最后的结果只会返回最......
  • c语言代码练习17
    需求:判断用户输入的一个数字是否为一个素数。#define_CRT_SECURE_NO_WARNINGS1#include<stdio.h>voidayue(int*pinput){inti=0;for(i=2;i<*pinput;i++){if(*pinput%i==0){printf("您输入的数字不是一个......
  • c语言代码练习--函数
    函数:一,概念:1,在计算科学中,子程序(英语:Subroutione,procedure,function,rotine,method.subprogram,callableunit),是一个大型程序中的某部分代码,由一个或多个语句块组成。它负责完成某项特定任务,而且相较于其它代码,具备相对的独立性。2,一般会由输入参数并有返回值,提供对过程的封......
  • python练习3| 测试类
    点击查看代码#shopping_list.pyclassShoppingList:#shopping_list是一个字典结构def__init__(self,shopping_list):self.shopping_list=shopping_listdefget_item_count(self):returnlen(self.shopping_list)defget_total_price(......
  • scrapy电影天堂练习
    movie.pyimportscrapyfrommovieProject.itemsimportMovieprojectItemclassMovieSpider(scrapy.Spider):name='movie'allowed_domains=['www.ygdy8.net']start_urls=['https://www.ygdy8.net/html/gndy/china/index.......
  • c语言代码练习16
    //计算a,b间的最大值#define_CRT_SECURE_NO_WARNINGS1#include<stdio.h>intayue(inta,intb){if(a>b){returna;}else{returnb;}}intmain(){inta=10;intb=20;intmax=ayue(a,......
  • c语言代码练习15
    //使用togo跳转代码,阻止关机#define_CRT_SECURE_NO_WARNINGS1#include<stdio.h>#include<string.h>intmain(){charinput[20]={0};system("shutdown-s-t60");printf("注意!注意!,您的电脑将在1分钟后关机,请输入:我是帅哥。才能取消关机。\n请输入:......
  • c语言代码练习14
    //设计一个猜数字游戏,需要提示猜大了还是小了,直到猜对为止#define_CRT_SECURE_NO_WARNINGS1#include<stdio.h>#include<time.h>#include<stdlib.h>voidmenu(){printf("###############################\n");printf("######1.play0.......
  • python练习2 | 类的继承
    点击查看代码#类继承练习:人力系统#员工分为两类,全职员工FullTimeEmployee、兼职员工PartTimeEmployee#全职和兼职都有”姓名,name,工号:id属性#都具备打印信息print_info(打印姓名、工号)方法#全职有月薪monthly_salary属性#兼职有日薪daily_salary属性,每月工作天数......
  • c语言代码练习13
    \\打印九九乘法表#define_CRT_SECURE_NO_WARNINGS1#include<stdio.h>intmain(){intx=0;inty=0;intz=0;for(x=1;x<10;x++){for(y=1;y<=x;y++){z=x*y;print......