cookies={}
with open(r'cookies.txt','r') as f:
for line in f.read().split(';'):
#其設(shè)置為1就會(huì)把字符串拆分成2份
name,value = line.strip().split('=', 1)
cookies[name] = value
爬取網(wǎng)頁(yè)上的圖片
import urllib.request
import re
def getHtml(url):
page = urllib.request.urlopen(url)
html = page.read().decode('utf-8')
return html
def getImg(html):
reg = r'src="(.+?\.jpg)" width'
imgre = re.compile(reg)
imglist = re.findall(imgre,html)
x = 0
for imgurl in imglist:
urllib.request.urlretrieve(imgurl,'images/%s.jpg' % x)
x+=1
html = getHtml('http://tieba.baidu.com/p/741081023')
getImg(html)
使用pymysql操縱MySQL
import pymysql
try:
conn= pymysql.connect(host='localhost', port=3306, user='DBSAdmin', passwd='admin', charset='UTF8', db='dbs')
cur=conn.cursor() #獲取一個(gè)游標(biāo)對(duì)象
cur.execute("INSERT INTO nameage VALUES('小明', 15),('小洪', 17),('小高', 16),('小剛', 15)")#插入數(shù)據(jù)
cur.execute("SELECT * FROM nameage")
data=cur.fetchall()
for row in data:
print('%s\t%s' %row)
except Exception as e:
print("發(fā)生異常")
finally:
cur.close() #關(guān)閉游標(biāo)
conn.commit() #向數(shù)據(jù)庫(kù)中提交任何未解決的事務(wù)龄广,對(duì)不支持事務(wù)的數(shù)據(jù)庫(kù)不進(jìn)行任何操作
conn.close() #關(guān)閉到數(shù)據(jù)庫(kù)的連接枕稀,釋放數(shù)據(jù)庫(kù)資源