def open_spider(self, spider):
     self.conn = pymysql.Connection(host='127.0.0.1',
                                    port=3306,
                                    user='******',
                                    password='******',
                                    db='Steam',
                                    charset='utf8')
     print(self.conn)
     self.cursor = self.conn.cursor()
     sql = """CREATE TABLE IF NOT EXISTS Steam_Data (
                 id int not null primary key auto_increment,
                 game_name VARCHAR(80),
                 detail_url VARCHAR(150),
                 release_date VARCHAR(15),
                 publisher VARCHAR(30),
                 developer VARCHAR(30),
                 tags VARCHAR(100),
                 game_price VARCHAR(8),
                 game_review VARCHAR(35),
                 price_discount VARCHAR(6));"""
     self.cursor.execute(sql)
Esempio n. 2
0
File: user.py Progetto: tzz2015/DTF
 def get(self):
     id = self.get_argument('id', 0)
     db = pymysql.Connection(host='127.0.0.1',
                             database='python_web',
                             user='******',
                             password='******',
                             charset='utf8')
     try:
         cursor = db.cursor()
         row = cursor.execute("DELETE FROM user_sysuser WHERE id=%s" % id)
         db.commit()
         if row > 0:
             self.write(result_format(row))
         else:
             self.write(result_format(None, msg='删除失败'))
         cursor.close()
     except Exception as e:
         db.rollback()
         self.write(result_format(None, msg='%s' % e))
     finally:
         db.close()
Esempio n. 3
0
def verify():
    fname = request.form['firstname']
    lname = request.form['lastname']
    email = request.form['mailid']
    mobile = request.form['mobile']
    ap = request.form['pass']

    #email validation

    host = "localhost"
    user = "******"
    password = "******"
    db = "mydb"
    con = pymysql.Connection(host=host,
                             user=user,
                             password=password,
                             database=db)
    cur = con.cursor()
    query = "select email from registrationapp"
    cur.execute(query)
    rows = cur.fetchall()
    con.commit()
    con.close()
    for row in rows:
        if row[0] == email:
            return "<h1>EMAIL ALREADY USED...</h1>"

    session['fname'] = fname
    session['lname'] = lname
    session['email'] = email
    session['mobile'] = mobile
    session['ap'] = ap

    try:
        msg = Message('OTP', sender='*****@*****.**', recipients=[email])
        msg.body = str(otp)
        mail.send(msg)
        return render_template('verify.html')
    except:
        return "<h1>Check your Email Id</h1>"
Esempio n. 4
0
 def __init__(self,
              host: str,
              port: int,
              user: str,
              password: str,
              db: str,
              charset: str = 'utf8',
              commit_num: int = 1000,
              cursor_str: str = 'dict') -> None:
     _cursor_dict = {
         'tuple': pymysql.cursors.Cursor,
         'tuple_ss': pymysql.cursors.SSCursor,
         'dict': pymysql.cursors.DictCursor,
         'dict_ss': pymysql.cursors.SSDictCursor,
     }
     if cursor_str not in _cursor_dict:
         raise Exception(f"参数cursor_str错误,仅支持选项:{', '.join(_cursor_dict)}")
     # 创建连接
     self.conn = pymysql.Connection(host=host,
                                    port=port,
                                    user=user,
                                    password=password,
                                    db=db,
                                    charset=charset,
                                    cursorclass=_cursor_dict[cursor_str])
     print(f"创建连接:地址:{host} 端口:{port} 用户名:{user} 数据库:{db}")
     self.cursor = self.conn.cursor()  # 获取游标
     self.cache_dict = {
         # 'insert': dict(),  # 增 缓存 字典
         # 'delete': dict(),  # 删 缓存 字典
         # 'update': dict(),  # 改 缓存 字典
         # 'select': dict(),  # 查 缓存 字典
     }
     self.count_dict = {
         'insert': 0,  # 增 数据统计
         'delete': 0,  # 删 数据统计
         'update': 0,  # 改 数据统计
         'select': 0,  # 查 数据统计
     }
     self.commit_num = commit_num  # 插入数据 提交数量
Esempio n. 5
0
def subscription():
    email = request.form['email']
    db = yaml.load(open('db.yaml'))
    conn = pymysql.Connection(host=db['mysql_host'],
                              user=db['mysql_user'],
                              password=db['mysql_password'],
                              db=db['mysql_database'])
    try:
        with conn.cursor() as cursor:
            sql = "INSERT INTO USERS VALUES(%s)"
            print(cursor.execute(sql, (email)))
        conn.commit()

        with conn.cursor() as cursor:
            sql = "SELECT email FROM USERS"
            cursor.execute(sql)
            for row in cursor:
                if row[0] == email:
                    print('Successfully registered!!')

    finally:
        conn.close()
    def _connect_with_pymysql(self, ip_address: str, ctx: ssl.SSLContext,
                              **kwargs):
        """Helper function to create a pymysql DB-API connection object.

        :type ca_filepath: str
        :param ca_filepath: A string representing the path to the server's
            certificate authority.

        :type cert_filepath: str
        :param cert_filepath: A string representing the path to the ephemeral
            certificate.

        :type key_filepath: str
        :param key_filepath: A string representing the path to the private key file.

        :type ip_addresses: Dict[str, str]
        :param ip_addresses: A Dictionary containing the different IP addresses
            of the Cloud SQL instance.

        :rtype: pymysql.Connection
        :returns: A PyMySQL Connection object for the Cloud SQL instance.
        """
        try:
            import pymysql
        except ImportError:
            raise ImportError(
                'Unable to import module "pymysql." Please install and try again.'
            )

        # Create socket and wrap with context.
        sock = ctx.wrap_socket(socket.create_connection((ip_address, 3307)),
                               server_hostname=ip_address)

        # Create pymysql connection object and hand in pre-made connection
        conn = pymysql.Connection(host=ip_address,
                                  defer_connect=True,
                                  **kwargs)
        conn.connect(sock)
        return conn
Esempio n. 7
0
def get_redis_data():
    #创建数据库连接
    print('contact..')
    redisCli = redis.StrictRedis('192.168.15.110', 6379, db=0)
    mysqlCli = pymysql.Connection('192.168.15.110',
                                  'root',
                                  '18201423398Hh',
                                  'test',
                                  3306,
                                  charset='utf8')
    cursor = mysqlCli.cursor()

    while True:
        print('c1')
        source, data = redisCli.blpop(
            'mycrawler_redis:items')  #myspider_redis:items
        print(source.decode('utf-8'), data.decode('utf-8'))
        data = json.loads(data.decode('utf-8'))
        insert_sql, parmas = insert_data(data)
        cursor.execute(insert_sql, parmas)
        mysqlCli.commit()
        # cursor.close()
        print('=')
Esempio n. 8
0
def work1():
    global conn
    while True:
        new_conn, new_addr = tcp_sock.accept()
        connect = pymysql.Connection(host=_host,
                                     port=_post,
                                     user=_user,
                                     passwd=_passwd,
                                     db=_db,
                                     charset=_charset)
        cursorsql = connect.cursor()
        sql_data = "DELETE FROM `ZigBee_table` WHERE 1"
        cursorsql.execute(sql_data)
        connect.commit()
        cursorsql.close()
        try:
            conn.close()
        except Exception as e:
            print("work1 error:", e)
            pass
        conn = new_conn
        addr = new_addr
        print(addr)
Esempio n. 9
0
    def __init__(self, cfg):
        self.cfg = cfg
        self.cfg.read('./config.ini', encoding='utf-8')
        # 读取数据库相关配置
        db = self.cfg.get('mysql_data', 'db_name')
        host = self.cfg.get('mysql_data', 'host')
        mysql_port = self.cfg.get('mysql_data', 'port')
        user = self.cfg.get('mysql_data', 'user')
        passwd = self.cfg.get('mysql_data', 'passwd')
        self.low_score = int(self.cfg.get('proxy-setting', 'low_score'))
        self.heigh_score = int(self.cfg.get('proxy-setting', 'heigh_score'))
        # 连接mysql数据库
        self.connect = pymysql.Connection(host=host,
                                          port=int(mysql_port),
                                          db=db,
                                          user=user,
                                          passwd=passwd,
                                          charset='utf8',
                                          use_unicode=True)
        self.cursor = self.connect.cursor()

        # 操作的数据库的表名
        self.table_name = self.cfg.get('mysql_data', 'table_name')
Esempio n. 10
0
def __write_into_mysql__(content, host, port, user, password):
    """Write the law contents into MySQL 
    Args:
        content: tuple or list. e.g. ('行政院消費者保護委員會志願服務獎勵辦法', ' 1 ', '本辦法依志願服務法第十九條第六項規定訂定之。')
        host: host of db
        port: port of db
        user: account
        password: password
    Returns:
        None
    """
    conn = pymysql.Connection(host=host,
                              port=port,
                              user=user,
                              password=password,
                              charset='utf8')  # utf8 to correct encoding
    cur = conn.cursor()
    cur.execute("USE legally;")
    cur.execute("INSERT INTO Law(LawName,ActNO,ActContent) VALUES (%s, %s,%s)",
                (content[0], content[1], content[2]))
    cur.connection.commit()
    print('Inserted:', content[0], content[1])
    return
Esempio n. 11
0
 def __init__(self,
              host=MYSQL_HOST,
              username=MYSQL_USER,
              password=MYSQL_PASSWORD,
              port=MYSQL_PORT,
              database=DATABASE):
     """
     mysql 初始化
     :param host:
     :param username:
     :param password:
     :param port:
     """
     try:
         self.db = pymysql.Connection(host=host,
                                      user=username,
                                      password=password,
                                      database=database,
                                      port=port)
         self.cursor = self.db.cursor(
             cursor=pymysql.cursors.DictCursor)  # TODO 最主要的一行。。。
     except pymysql.MySQLError as e:
         print(e.args)
Esempio n. 12
0
def index():
    semail = request.form['email']
    spasswd = request.form['password']
    sql = """ select email,passwd from zhuce where email='%s' and passwd='%s' """ % (
        semail, spasswd)
    cur.execute(sql)
    results = cur.fetchone()
    if results:
        db1 = pymysql.Connection('localhost', 'root', '123456', 'test')
        cur1 = db1.cursor()
        sql_shuju = """ select name,salary_min,salary_max,job_address,work_years,Enducation,job_type,job_advantage,job_bts,field from lagou ORDER BY RAND() limit 5"""
        cur1.execute(sql_shuju)
        results = cur1.fetchall()
        db1.commit()
        print(results)
        return render_template('bootstrap/boke.html', u=results)
        db.close()
        db1.close()
    else:
        return render_template(
            'bootstrap/login.html',
            message='密码或账号错误',
        )
        db.close()
Esempio n. 13
0
def readOneLogFile_PC(filename):
    cn = pymysql.Connection(host="localhost",
                            user="******",
                            password="******",
                            database="3618med",
                            charset="utf8")
    cursor = cn.cursor()
    for line in open(filename):
        #print(line)
        list_str = line.split(" ")
        md5str = md5(line)
        if len(list_str) > 13:
            useragent = " ".join(list_str[12:len(list_str) - 1])
            useragent = useragent.replace("\"", "").strip()
            if useragent.lower().find("spider") < 0:
                continue
            ip = list_str[0].strip()
            day_str = list_str[3]
            day_list_str = day_str.split(":")
            day = day_list_str[0].replace("[", "")
            time = day_list_str[1] + ":" + day_list_str[
                2] + ":" + day_list_str[3]
            time = time.strip()
            domain = ""
            method = list_str[5].replace("\"", "").strip()
            uri = list_str[6].strip()
            http = list_str[7].replace("\"", "").strip()
            status = list_str[8].strip()
            datasize = list_str[9].strip()
            referer = list_str[10].replace("\"", "").strip()
            cdnip = list_str[len(list_str) - 1]
            cdnip = cdnip.replace('"', '').strip()

            #print("ip:" + ip)
            #print("day:" + day)
            #print("time:" + time)
            #print("domain:" + domain)
            #print("method:" + method)
            print("uri:" + uri)
            #print("http:" + http)
            #print("status:" + status)
            #print("datasize:" + datasize)
            #print("referer:" + referer)
            #print("cdnip:" + cdnip)
            #print("useragent:" + useragent)
            #break

            sql = "SELECT count(*) FROM logtable  WHERE md5str='" + md5str + "'"
            count = 0
            try:
                cursor.execute(sql)
                results = cursor.fetchall()
                count = results[0][0]
            except:
                print("Error: select data error! sql:" + sql)

            if count < 1:
                sql = "INSERT INTO logtable(ip, \
                          day,time, domain, method,uri,http,status,datasize,referer,cdnip,useragent,md5str) \
                          VALUES ('%s','%s','%s','%s','%s','%s','%s','%d','%d','%s','%s','%s','%s')"                                                                                                   % \
                          (ip,day,time,domain,method,uri,http,int(status),int(datasize),referer,cdnip,useragent,md5str)

            try:
                cursor.execute(sql)

            except:
                print("Error: insert into data error! sql:" + sql)
    cn.commit()
    cn.close()
Esempio n. 14
0
                    if author in line:
                        keyword_dict['author'] = line.split(
                            ":")[-1].lstrip().replace('\n', '')
                    if create_date in line:
                        keyword_dict['create_time'] = line.split(
                            " ")[1].lstrip().split(" ")[0].replace('\n', '')
                    if comment in line:
                        keyword_dict['comment'] = line[3:].split(
                            "(")[-1].split(")")[0].replace('\n', '')
                    #    keyword_dict['comment'] = re.findall(r'[(](.*?)[)]', line, 0)
    final_list.append(keyword_dict)

#链接数据库
conn = pymysql.Connection(host='172.16.30.130',
                          port=3306,
                          user='******',
                          passwd='8lysZM6#m$UiHlf6',
                          db='data_exchange',
                          charset='utf8mb4')
cur = conn.cursor()

#truncate table warehouse_tl_monitor
truncate_sql = "truncate table warehouse_tl_monitor;"
cur.execute(truncate_sql)
#insert into table warehouse_tl_monitor
insert_sql = "insert into warehouse_tl_monitor(table_name,database_name,table_comment,source_table,ip,user_name,password,author,create_time,start_time,end_time) " \
             "VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);"

for i in final_list:
    table_name = i['table_name']
    name = i.setdefault('name', 'hive')
    database_name = i.setdefault('database_name', 'tl')
Esempio n. 15
0
import pymysql

connection = pymysql.Connection(host='localhost',
                                user='******',
                                password='******',
                                db='hotel',
                                charset='utf8mb4',
                                cursorclass=pymysql.cursors.DictCursor)


def close_database():
    with connection.cursor():
        print("Database status: 'Closed'")
        connection.close()


def get_log_info():
    with connection.cursor() as cursor:
        sql = "SELECT user_login, user_password, user_ID, user_type FROM `users`"
        cursor.execute(sql)
        result = cursor.fetchall()
        return result


def add_user(user_name, user_surname, user_email, user_telephone, user_PESEL,
             user_login, user_password, user_type):
    with connection.cursor() as cursor:
        sql = "INSERT INTO users (user_name, user_surname, user_email, user_telephone, user_PESEL, " \
              "user_login, user_password, user_type)" \
              "VALUES (%s, %s, %s, %s, %s, %s, %s, %s)"
        cursor.execute(sql,
import requests, json, pymysql

coon = pymysql.Connection(host='127.0.0.1',
                          database="wangzherongyao",
                          user="******",
                          password="******",
                          charset="utf8")
cursor = coon.cursor()
headers = {
    #此处需要自己修改,因存在个人信息,所以下行连接内容不全!
    'Referer':
    'http://h5.kohsocialapp.qq.com/app/yxzj/herov2/herodetail/?  后面自己抓包填在这',
}
#经过爬取得知只有105——200  501---504有英雄
for i in [x for x in range(103, 200)] + [501, 502, 503, 504]:
    data = {
        #以下XXX换成自己抓包得到的数据
        'uin': 'XXXXXXXX',
        'userId': 'XXXXXXXXX',
        'token': 'XXXXXXXXX',
        'heroId': i,
        'skillIdStr':
        str(i) + '00|' + str(i) + '10|' + str(i) + '20|' + str(i) + '30',
        'channelId': '16000' + str(i),
    }

    response = requests.post(
        'http://h5.kohsocialapp.qq.com/app/wzry/ajax/getPointInfoById',
        headers=headers,
        data=data)
    jsdata = json.loads(response.text)
Esempio n. 17
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from pyecharts import Bar
import pymysql
import webbrowser

try:
    conn = pymysql.Connection(host="localhost",
                              port=3306,
                              user="******",
                              passwd="123456",
                              db="librarysystem")
    cur = conn.cursor(pymysql.cursors.DictCursor)
    cur.execute("select number from sort;")
    rows = cur.fetchall()
    # rows = numpy.array(rows)
    attr = ["编程", "文学", "金融", "心理学", "医学", "历史", "小说", "军事", "法律", "体育"]
    lists = [[]]
    for row in rows:
        lists[0].append(row["number"])
        # lists[1].append(row["price"])

    print(lists[0])
    # print(lists[1])

    v1 = lists[0]
    # v2 = lists[1]

    print(v1)
    # print(v2)
Esempio n. 18
0
import pymysql

# Connection 생성
conn = pymysql.Connection(host="192.168.0.3",
                          port=3306,
                          user="******",
                          passwd="1234",
                          db="example")

# Cursor 생성
cursor = conn.cursor(pymysql.cursors.DictCursor)

# 기존의 데이터를 삭제하고 AUTO_INCREMENT를 1로 설정
cursor.execute("DELETE FROM items")
cursor.execute("ALTER TABLE items AUTO_INCREMENT = 1")

# 단일 데이터 추가
sql = "INSERT INTO items (name, qty, price) VALUES (%s, %s, %s)"
item = ("item1", 10, 2.5)
cursor.execute(sql, item)
print(cursor.rowcount, "record inserted, ID:", cursor.lastrowid)
print()

# 멀티 데이터 추가
sql = "INSERT INTO items (name, qty, price) VALUES (%s, %s, %s)"
items = [("item2", 4, 3.1), ("item3", 3, 2.5), ("item4", 6, 0.5)]
cursor.executemany(sql, items)
print("insert count = ", cursor.rowcount)
print()

# 데이터 조회
Esempio n. 19
0
 def create(self):
     try:
         import pymysql
     except ImportError:
         raise ImportError("PyMySQL>=0.8.1 is required")
     return pymysql.Connection(**self.config)
Esempio n. 20
0
import math
from wenshu.utils.wenshu_log import getLogger
from wenshu.utils.wenshu_session import getSess
from wenshu.utils.yundama import result_captcha
import time
from wenshu.utils.captcha_local import retrive_img, process_img, recognize
import random
from scrapy.selector import Selector
import requests
sess, vl5x = getSess()

logger = getLogger(__name__)

conn = pymysql.Connection(host='127.0.0.1',
                          user='******',
                          password='******',
                          database='crawl',
                          charset='utf8mb4',
                          use_unicode=True)
cursor = conn.cursor()

headers = {
    'User-Agent':
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0',
    'X-Forwarded-For':
    '{}.{}.{}.{},113.88.176.160'.format(random.randint(1, 254),
                                        random.randint(1, 254),
                                        random.randint(1, 254),
                                        random.randint(1, 254))
}

with open('ua_list.json', 'r') as f:
Esempio n. 21
0
 def __init__(self, *args, **kwargs):
     self.conn = pymysql.Connection(*args, **kwargs)
     self.cursor = self.conn.cursor()
     self.cursor_dict = self.conn.cursor(pymysql.cursors.DictCursor)
Esempio n. 22
0
main=tk.Tk()


   

       
       
    
#img
bg=img.open("bg.jpg")
bg_show=imgtk.PhotoImage(bg)
tk.Label(image=bg_show).pack(expand=True)
tk.Label(text="هذه هي مبيعاتك اليوم",font=33).place(x=500,y=530)
#objects
#listbox
conn=mysql.Connection(host="localhost",user="******",password="",db="sgda")
exe=conn.cursor()
exe.execute("""SELECT * FROM sell_today """)
results=exe.fetchall()
list=tk.Listbox(font=33, width=110, height=20)
list.place(x=100,y=50)
for row in results:
    list.insert("end",("الاسم\{}".format(row[0])))
    list.insert("end",("كم قطعه\{}".format(row[1])))
    list.insert("end",("كم وحده\{}".format(row[2])))
    list.insert("end",("السعر\{}".format(row[3])))
    list.insert("end",("وقت الصرف\{}".format(row[4])))
    list.insert("end",("رقم الفاتوره\{}".format(row[6])))
    list.insert("end",("---------------------------"))
tk.Label(text="المبلغ الكلي للبيع",font=50).place(x=180,y=600)
exe.execute("""SELECT `price` FROM sell_today """)
Esempio n. 23
0
# -*- coding:utf-8 -*-
import os

from tornado.web import Application, RequestHandler, StaticFileHandler
from tornado.ioloop import IOLoop
import pymysql

db = pymysql.Connection(host='127.0.0.1',
                        database='django_db',
                        user='******',
                        password='******',
                        charset='utf8')


class IndexHandler(RequestHandler):
    def initialize(self):
        self.db = db
        self.cursor = db.cursor()

    def get(self):
        self.write('index')
        print(os.path.dirname(__file__))

    def post(self):
        title = self.get_argument("btitle")
        pub_date = self.get_argument("bpub_date")
        read = self.get_argument("bread")
        comment = self.get_argument("bcomment")
        delete = self.get_argument("is_delete")

        try:
Esempio n. 24
0
# Import library
from main import bot, dp
from aiogram import types
from aiogram.types import Message
import pymysql
from config import dbHost, dbUser, dbPassword, dbName

keyboard_markup = types.ReplyKeyboardMarkup(row_width=3)

connection = pymysql.Connection(dbHost, dbUser, dbPassword, dbName)

# Array for keyboard
array_keyboard = ['Button1', 'Button2']


# Send message to admin
async def send_to_admin(dp):
    await bot.send_message(chat_id=admin_id, text="Bot start")


# Function of start bot
@dp.message_handler(commands=['start'])
async def send_welcome(message: types.Message):
    keyboard_markup.add(*(types.KeyboardButton(text)
                          for text in array_keyboard))
    await message.answer(text='Привет, как у тебя дела?',
                         reply_markup=keyboard_markup)


def analytic(nickname, tgid, fullname, command):
    cursor = connection.cursor.cursor()
Esempio n. 25
0
__author__ = 'Administrator'

import pymysql

conn = pymysql.Connection(host='127.0.0.1', user='******', passwd='123456')
cur = conn.cursor()

try:
    cur.execute("drop database mpdb")
except Exception as e:
    print(e)
finally:
    pass

cur.execute("create database mpdb")
cur.execute("use mpdb")

cur.execute("create table users(id int,name varchar(8))")
cur.execute(
    "insert into users values(1,'www'),(2,'cnblogs'),(3,'com'),(4,'peter')")

cur.execute('select * from users')
for row in cur.fetchall():
    print(row)

cur.close()
conn.commit()
conn.close()
Esempio n. 26
0
#!/usr/bin/python3
# coding: utf-8

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

import pymysql

Base = declarative_base()
metadata = Base.metadata

engine = create_engine('mysql+pymysql://root:[email protected]:3306/fmss')
engine.connect()
Session = sessionmaker(bind=engine)
session = Session()

db_conn = pymysql.Connection(host='47.105.137.19',
                             port=3306,
                             user='******',
                             password='******',
                             db='fmss',
                             charset='utf8',
                             cursorclass=pymysql.cursors.DictCursor)
Esempio n. 27
0
@contact: [email protected]
@file: mont.py
@datetime: 2018-4-2 15:25
"""

from __future__ import unicode_literals
import sys

reload(sys)
sys.setdefaultencoding('utf-8')
import pymysql

import pymysql
import time
import datetime
conn = pymysql.Connection(host='192.168.0.186', port=3306, user='******', password='******',
                          charset='utf8', db='test', autocommit=True)

tmp = 0
while True:
    with conn.cursor() as cursor:
        cursor.execute('SELECT count(*) from test_update')
        print(id(cursor))

        num = cursor.fetchall()
        if num[0][0] != tmp:
            print datetime.datetime.now(), num[0][0]
            tmp = num[0][0]
    time.sleep(10)


Esempio n. 28
0
def main(host, port, user, password):
    """
    Args: 
        host: host of db
        port: port of db
        user: account
        password: password
    Returns: None
    """
    targets = __read_targets_from_json__()
    print('Load targets from json')
    links_list = [__get_links_list__(url) for url in targets]

    # links_list = [ __get_links_list__(url) for url in ['https://law.moj.gov.tw/LawClass/LawClassListN.aspx?TY=04018003']]

    contents = []
    for links in links_list:
        for link in links:
            print('Link found', link)
            try:
                contents.extend(__get_law_content__(link))
            except requests.exceptions.InvalidSchema:
                pass

    # Create databse
    print('Connecting database')
    conn = pymysql.Connection(host=host,
                              port=port,
                              user=user,
                              password=password,
                              charset='utf8')  # utf8 to correct encoding
    cur = conn.cursor()
    print('Create database if not exists.')
    if not cur.execute("SHOW DATABASES LIKE 'legally';"):
        __create_database__()

    for content in contents:
        cur = conn.cursor()
        cur.execute('USE legally;')
        cur.execute("SELECT DISTINCT LawName FROM Law;")
        stored = [i[0] for i in cur.fetchall()]
        if content[0] in stored:
            cur.execute('USE legally;')
            cur.execute(
                "SELECT DISTINCT ActNO FROM Law WHERE LawName = '{}'".format(
                    content[0]))
            try:
                if int(content[1]) in [i[0] for i in cur.fetchall()]:
                    print('Whole or part of law is already stored:',
                          content[0], content[1])
                else:
                    __write_into_mysql__(content=content,
                                         host=host,
                                         port=port,
                                         user=user,
                                         password=password)
            except ValueError:
                pass
        else:
            __write_into_mysql__(content=content,
                                 host=host,
                                 port=port,
                                 user=user,
                                 password=password)

    print('Done inserting to DB')
    conn = pymysql.Connection(host=host,
                              port=port,
                              user=user,
                              password=password,
                              charset='utf8')  # utf8 to correct encoding
    cur = conn.cursor()
    cur.execute('USE legally;')
    cur.execute('SELECT COUNT(*) FROM Law')
    print('The number of documnets in database now is:', cur.fetchall()[0][0])
    conn.close()
    os._exit(1)
Esempio n. 29
0
 def reconnect(self):
     """Closes the existing database connection and re-opens it."""
     self.close()
     self._db = pymysql.Connection(**self._db_args)
     self._db.autocommit(False)
Esempio n. 30
0
def crawl():
    headers = {
        'User-Agent': ('Mozilla/5.0 (compatible; MSIE 9.0; '
                       'Windows NT 6.1; Win64; x64; Trident/5.0)'),
    }
    res = requests.get('https://movie.douban.com/cinema/nowplaying/xiamen/',
                       headers=headers)
    doc = pq(res.text)
    move_list = doc('#nowplaying ul .list-item').items()
    for i in move_list:
        url = i('li .ticket-btn').attr('href')
        # print(i)
        r = requests.get(url, headers=headers)
        c = pq(r.text)
        name1 = c('#content h1 span').text().split(' ')[0]
        # a = c('.subject.clearfix #info').text()
        director1 = c('.subject.clearfix #info').text().split(
            '导演: ')[-1].split('编剧')[0].strip()
        writers1 = c('.subject.clearfix #info').text().split('编剧:')[-1].split(
            '主演')[0].strip()
        starring1 = c('.subject.clearfix #info').text().split('主演:')[-1].split(
            '类型:')[0].strip()
        type1 = c('.subject.clearfix #info').text().split('类型:')[-1].split(
            '制片国家/地区:')[0].split('官方网站:')[0].strip()
        imdb_url1 = c('.subject.clearfix #info').text().strip().split(
            "IMDb链接:")[-1].strip()
        if '\n' or ':' in imdb_url1:
            imdb_url1 = ''
        socre1 = c('#interest_sectl .ll.rating_num').text().strip()
        buy_url1 = c('.aside .ticket a').attr('href')
        time1 = c('.subject.clearfix #info').text().split('上映日期:')[-1].split(
            '片长:')[0].strip()
        run_time1 = c('.subject.clearfix #info').text().split('片长:')[-1].split(
            '又名:')[0].strip()
        made1 = c('.subject.clearfix #info').text().split(
            '制片国家/地区:')[-1].split('语言:')[0].strip()
        language1 = c('.subject.clearfix #info').text().split('语言:')[-1].split(
            '上映日期:')[0].strip()
        nickname1 = c('.subject.clearfix #info').text().split('又名:')[-1].split(
            'IMDb链接:')[0].strip()

        # data = {
        #     'name': name1,
        #     'director': director1,
        #     'writers': writers1,
        #     'starring': starring1,
        #     'type': type1,
        #     'made': made1,
        #     'language': language1,
        #     'time': time1,
        #     'run_time': run_time1,
        #     'nickname': nickname1,
        #     'imdb_url': imdb_url1,
        #     'socre': socre1,
        #     'buy_url': buy_url1,
        #
        # }
        # print(data)
        # Student.objects.create(data['name'])

        conn = pymysql.Connection(host='127.0.0.1',
                                  port=3306,
                                  db='crawl_data',
                                  user='******',
                                  password='******')
        cursor = conn.cursor()
        sql = 'insert into movie_info(name_s,director,writers,starring,type_s,made,language_s,time_s,run_time,nickname,imdb_url,socre,buy_url)' \
              'values("%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s")'%(str(name1),str(director1),str(writers1),str(starring1),str(type1),str(made1),str(language1),str(time1),str(run_time1),str(nickname1),str(imdb_url1),str(socre1),str(buy_url1))
        # print(sql)
        cursor.execute(sql)
        conn.commit()
        cursor.close()
        conn.close()