-
Notifications
You must be signed in to change notification settings - Fork 1
/
pipelines.py
55 lines (45 loc) · 1.74 KB
/
pipelines.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# -*- coding: utf-8 -*-
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import mysql.connector
class GlassdoorPipeline(object):
def __init__(self):
self.create_connection() # whenevr the class is initialized the two methods are automatically called
self.create_table()
def create_connection(self):
self.conn = mysql.connector.connect(host="localhost",
user="root",
passwd="keepcalm",
database="glassdoor")
self.curr = self.conn.cursor()
def create_table(self):
self.curr.execute("""DROP TABLE IF EXISTS jobtable_tb""")
self.curr.execute("""create table jobtable_tb(
title text,
empname text,
location text,
size text,
salarylow text,
salaryhigh text,
jobId text,
year text,
rating text
)""")
def process_item(self, item, spider):
self.store_db(item)
return item
def store_db(self, item):
self.curr.execute("""insert into jobtable_tb values(%s,%s,%s,%s,%s,%s,%s,%s,%s)""", (
item['title'],
item['empname'],
item['location'],
item['size'],
item['salarylow'],
item['salaryhigh'],
item['jobId'],
item['year'],
item['rating']
))
self.conn.commit()