爬取高考数据库数据实现中一些小方法

1,把‘--’转换成‘0’

def toZero(sth):
    if sth!='--' and sth!=None and sth!='':
       return sth
    else:
       return '0'

2,为构件sql语句中的引号添加转义符

def trmean(str):
    return re.sub('\'|\"',r'\"',str)

3,模拟页面操作selenium的实现

def getScore():
    print('start')
    arr=[307,318,596]
    driver = webdriver.Chrome()
    time.sleep(10)
    for id in arr:
        url='https://gkcx.eol.cn/school/{}/specialtyline'.format(id)
        driver.get(url)
        driver.implicitly_wait(20)
        titles = driver.find_element_by_class_name('scoreLine-table').text
        print(titles)
        print("页面{}完毕".format(id))

4,从数据库查询获取按照sql语句返回查询json格式的数据

def queryAsJson(sql):
    print('开始从数据库获取数据')
    # 获取查询的条目
    items=re.search(r'select.*from',sql,re.I).group()[6:-4].split(',')
    qjson=[]
    db = database.getConnect('whereto')
    cursor = db.cursor()
    cursor.execute(sql)
    results=cursor.fetchall()
    for row in results:
        item={}
        for idex,i in enumerate(items):
            it=str(i).strip()
            item[it]=row[idex]
        qjson.append(item)
    cursor.close()
    db.commit()
    db.close()
    print('查询完毕!')
    return qjson

 

selenium

转载于:https://www.cnblogs.com/tutuwowo/p/11064593.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值