def university_storage(universities): connection, cursor = None, None try: connection, cursor = mysql_util.get_connect_cursor() for university in universities: id = university.get("id") query_sql = "select * from spider_university where id = '%s'" % ( id, ) insert_sql = "insert into spider_university (code, name, type, level, nature, belong, central, department, " \ "doublehigh, f211, f985, is_recruitment, dual_class, address, province_name, single_province, " \ "city_name, county_name, view_total_number, create_date) values " \ "('%s', '%s', '%s', '%s', '%s', '%s', %s, %s, %s, %s, %s, %s, '%s', " \ "'%s', '%s', '%s', '%s', '%s', %s, '%s')" \ % (university.get("id", None), university.get("name", None), university.get("type_name", None), university.get("level_name", None), university.get("nature_name", None), university.get("belong", None), university.get("central", None), university.get("department", None), university.get("doublehigh", None), university.get("f211", None), university.get("f985", None), university.get("is_recruitment", None), university.get("dual_class_name", None), university.get("address", None), university.get("province_name", None), university.get("single_province", None), university.get("city_name", None), university.get("county_name", None), university.get("view_total_number", None), datetime.now().strftime("%Y-%m-%d %H:%M:%S")) country_temp = mysql_util.execute_query(cursor=cursor, sql=query_sql) if len(country_temp) == 0: mysql_util.execute_insert_update_delete(cursor, insert_sql) mysql_util.commit_(connection) except Exception as e: print(e) mysql_util.rollback_(connection) finally: mysql_util.close_connect_cursor(connection, cursor)
def wyxw_storage(news): connection, cursor = None, None try: connection, cursor = mysql_util.get_connect_cursor() query_sql = "select * from spider_news where title = '%s'" % news.get( "title") insert_sql = "insert into spider_news (title, digest, url, view_count, label, keywords, " \ "news_date, type, channel, source, image_url, detail, create_date) " \ "values ('%s', '%s', '%s', %s, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')" \ % (news.get("title"), news.get("digest"), news.get("url"), news.get("view_count"), news.get("label"), news.get("keywords"), news.get("news_date"), news.get("type"), news.get("channel"), news.get("source"), news.get("image_url"), news.get("detail"), datetime.now().strftime("%Y-%m-%d %H:%M:%S")) news_temp = mysql_util.execute_query(cursor, query_sql) if len(news_temp) == 0: print(insert_sql) mysql_util.execute_insert_update_delete(cursor, insert_sql) mysql_util.commit_(connection) except Exception as e: print(e) mysql_util.rollback_(connection) return False finally: mysql_util.close_connect_cursor(connection, cursor) return True
def storage_mysql(gzbd_data): connection, cursor = None, None; try: connection, cursor = mysql_util.get_connect_cursor(); for item in gzbd_data: coronavirus_date = item["时间"]; query_sql = "select * from spider_coronavirus where date = '%s' and diagnosis is null" % (coronavirus_date,); insert_sql = "insert into spider_coronavirus (date, region, diagnosis, overseas_import, cure, " \ "death, therapy, observation, create_date) values ('%s', '%s', %s, %s, %s, %s, %s, %s, '%s')" % \ (item["时间"], item["地区"], item.get("确诊数", None), item.get("境外输入数", None), item.get("治愈数", None), item.get("死亡数", None), item.get("隔离数", None), item.get("观察数", None), datetime.now().strftime("%Y-%m-%d %H:%M:%S")); insert_sql = insert_sql.replace("None", "Null"); result = mysql_util.execute_query(cursor, query_sql); if len(result) == 0: mysql_util.execute_insert_update_delete(cursor, insert_sql); mysql_util.commit_(connection); except Exception as e: print(e); mysql_util.rollback_(connection); finally: mysql_util.close_connect_cursor(connection, cursor);
def inset_resource(): conn, cur = mysql_util.get_connect_cursor() sql = "insert into resource (resource_uri, resource_name, permission) values ('aa', 'bb', 'cc')" mysql_util.execute_insert_update_delete(cur, sql) mysql_util.commit_(conn) mysql_util.close_connect_cursor(conn, cur)
def query_resource(): conn, cur = mysql_util.get_connect_cursor() sql = "select * from resource" result = mysql_util.execute_query(cur, sql) print(result) mysql_util.close_connect_cursor(conn, cur)