python + 크롤링 + mysql
언어/python2018. 10. 9. 15:45
#-------------------------------
import requests
from bs4 import BeautifulSoup
import re
import mysql.connector
from mysql.connector import errorcode
#-------------------------------
class T1(object):
# 생성자____________________________________________________________
def __init__(self, URL):
self.targetURL = URL
self.resultHTML = None
self.resultBsObj= None
self.colorList = []
self.cnx = None # MySQL
# func (1)_________________________________________________________
def urlRequest(self):
self.resultHTML = requests.get(self.targetURL)
if self.resultHTML.ok and self.resultHTML.status_code == 200:
self.resultBsObj = BeautifulSoup(self.resultHTML.text,
"html.parser")
colorList = self.resultBsObj.find_all("div", {"class":"colordvcon"})
for i in colorList:
resultValue = re.sub("[\n, \t, #]", '', i.a.text)
self.colorList.append(resultValue)
# func (2)_________________________________________________________
def DBTableSetting(self):
try: # MySQL 서버 접근 시도 'mysql -u kjh -p'
self.cnx = mysql.connector.connect(user='kjh',
password='1234',
host='192.168.65.135')
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
else:
print(err)
else: # MySQL 서버 접근 성공
print ("[1] MySQL 서버 접근 성공 ... ~!!")
cursor = self.cnx.cursor()
try: # DB 생성시도 'CREATE DATABASE STU_DB;'
cursor.execute("CREATE DATABASE {}".format('STU_DB'))
except mysql.connector.Error as err:
if err.errno == errorcode.ER_DB_CREATE_EXISTS:
print ("DB create fail")
else:
print (err)
else: # DB 생성 성공
print("[2] DB 생성 성공 ... ~!!")
cursor.execute("USE STU_DB")
table_info = {} # type of dictionary
table_info['COLOR_INFO'] = (
"CREATE TABLE `HEX_COLOR`("
" `name` VARCHAR(20) NOT NULL"
")"
)
try: # table 생성 시도
cursor.execute(table_info['COLOR_INFO'])
except mysql.connector.Error as err:
if err.errno == errorcode.ER_TABLE_EXISTS_ERROR:
print ("table create error")
else:
print (err)
else:
print("[3] TABLE 생성 성공 ... ~!!")
cursor.close()
self.cnx.close()
# func (3)_________________________________________________________
def DataInsert(self):
try: # MySQL 서버 접근 시도 'mysql -u kjh -p'
self.cnx = mysql.connector.connect(user='kjh',
password='1234',
database='STU_DB',
host='192.168.65.135')
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print (err)
else:
cursor = self.cnx.cursor()
hex_data = ("INSERT INTO HEX_COLOR "
"(name) "
"VALUES (%(name)s)")
for i in self.colorList:
insert_data = {
'name': i
}
cursor.execute(hex_data, insert_data)
self.cnx.commit()
cursor.close()
self.cnx.close()
def main():
stuNode = T1("https://www.color-hex.com/") # 객체 생성
stuNode.urlRequest()
''' MySQL SERVER 접근 > DB 생성 > Table 생성 '''
# stuNode.DBTableSetting()
stuNode.DataInsert()
if __name__ == "__main__":
main()
'언어 > python' 카테고리의 다른 글
python winlog event (0) | 2018.10.10 |
---|---|
python + 사람인 (0) | 2018.10.09 |
PE 파싱 일부분 (0) | 2018.10.08 |
로컬 피시 ip 확인 - 파이썬 (0) | 2018.10.06 |
파이썬 에러 목록 (0) | 2018.09.30 |