Guest User

Untitled

a guest
Apr 10th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. import MySQLdb
  2.  
  3.  
  4. class Database:
  5.  
  6. host = 'localhost'
  7. user = 'root'
  8. password = 'test123'
  9. db = 'scraping_db'
  10.  
  11. def __init__(self):
  12. self.connection = MySQLdb.connect(self.host, self.user, self.password, self.db,use_unicode=True, charset="utf8")
  13. self.cursor = self.connection.cursor()
  14.  
  15. def insert(self, query,params):
  16. try:
  17. self.cursor.execute(query,params)
  18. self.connection.commit()
  19. except Exception as ex:
  20. self.connection.rollback()
  21.  
  22.  
  23. def __del__(self):
  24. self.connection.close()
  25.  
  26. from con import Database
  27.  
  28. class LinkPipeline(object):
  29.  
  30. def __init__(self):
  31. self.db=Database()
  32.  
  33. def process_item(self, item, spider):
  34. query="""INSERT INTO links (title, location,company_name,posted_date,status,company_id,scraped_link,content,detail_link,job_id) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s,%s)"""
  35. params=(item['title'], item['location'], item['company_name'], item['posted_date'], item['status'], item['company_id'], item['scraped_link'], item['content'], item['detail_link'],item['job_id'])
  36. self.db.insert(query,params)
  37. return item
Add Comment
Please, Sign In to add comment