python -- 网络和数据库

网络

import urllib.request as req

path = "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 pm

conn = 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")])

你可能感兴趣的:(python -- 网络和数据库)