python -- 网络和数据库

来源:互联网 发布:金山软件安全卫士 编辑:程序博客网 时间:2024/06/01 09:19

网络

import urllib.request as reqpath = "http://www.baidu.com/"res = req.urlopen(path)help(res)bytes = res.read()    #获取字节流html = bytes.decode("UTF-8")        #转码with open("./file1.html", "bw") as f:    f.write(bytes)

数据库

import pymysql as pmconn = pm.connect(host="localhost", user="xxx", password="xxx", database="xxx")cursor = conn.cursor()rows = cursor.execute("select * from xxx")for i in range(rows):    one = cursor.fetchone()#获取游标所在第一行数据    #print(one[0], one[1])cursor.close()conn.close()#参数化查询,防止SQL注入cursor.mogrify("select a,b from xxx where user=%s and pass=%s",(user,passwd))####################effect_rows = cursor.execute(“select * from user”)effect_row = cursor.execute("update test set pass = '123' where nid = %s", (11,'stu'))effect_row = cursor.executemany("insert into test(user,pass,licnese)values(%s,%s,%s)",[("u1","u1pass","11111"),("u2","u2pass","22222")])
原创粉丝点击