python开发专属表情包_Python开发个人专属表情包网站
“表情包”是一種利用圖片來(lái)表示感情的一種方式。表情包是在社交軟件活躍之后,形成的一種流行文化,表情包流行于互聯(lián)網(wǎng)上面,基本人人都會(huì)發(fā)表情。
曾經(jīng)你是否也有過(guò)找不到表情包去應(yīng)對(duì)別人的時(shí)候。
今天小編分享如何用Python開(kāi)發(fā)個(gè)人專屬的表情包網(wǎng)站,想用什么表情包搜一下就有了!
本篇分為兩部分
1、爬取表情包存入數(shù)據(jù)庫(kù)
2、搭建個(gè)人個(gè)人專屬表情網(wǎng)站
爬取包情包存入數(shù)據(jù)庫(kù)
環(huán)境:Windows+Python3.6
IDE:個(gè)人喜好
模塊
1 importrequests2 importre3 import pymysq
完整代碼
6 importrequests7 importre8 importpymysql9 #連接數(shù)據(jù)庫(kù)
10 db = pymysql.connect(host = "127.0.0.1",port = 3306,db = "db",user = "root",passwd = "root",charset = "utf8")11 #創(chuàng)建游標(biāo)
12 cursor =db.cursor()13 #cursor.execute("select * from images")
14 #print(cursor.fetchall())
15 #小駝峰
16 #注釋 獲取圖片列表
17 defgetImagesList(page):18 #獲取斗圖網(wǎng)源代碼
19 html = requests.get("http://www.doutula.com/photo/list/?page={}".format(page)).text20 #正則表達(dá)式 通配符 .*? 匹配所有 分組匹配
21 reg = r"data-original="(.*?)".*?alt="(.*?)""
22 #增加匹配效率的 S 多行匹配
23 reg =re.compile(reg,re.S)24 imagesList =re.findall(reg,html)25 for i inimagesList:26 image_url =i[0]27 image_title = i[1]28 #format 字符串格式化 %s
29 cursor.execute("insert into images(`name`,`imageUrl`) values("{}","{}")".format(image_title,image_url))30 print("正在保存 %s"%image_title)31 db.commit()32 #range 范圍 1<=X<1000
33 for i in range(1,1001):34 print("第{}頁(yè)".format(i))35 getImagesList(i)
效果圖
使用的框架是Flask
1 from flask importFlask2 from flask importrender_template3 from flask importrequest4 importpymysql5 #404 頁(yè)面未找到
6 app = Flask(__name__)7 #裝飾器
8 @app.route("/") #route 路由
9 defindex():10 #return "hello world"
11 return render_template("index.html")12 @app.route("/search")13 defsearch():14 #接收用戶關(guān)鍵字
15 keyword = request.args.get("kw")16 count = request.args.get("count")17 cursor.execute("select * from images where name like "%{}%"".format(keyword))18 data =cursor.fetchmany(int(count))19 return render_template("index.html",images =data)20 #程序的入口
21 if __name__ == "__main__":22 db = pymysql.connect(host="127.0.0.1", port=3306, db="db", user="root", passwd="root", charset="utf8",cursorclass =pymysql.cursors.DictCursor)23 #創(chuàng)建游標(biāo)
24 cursor =db.cursor()25 #調(diào)試模式
26 #port 端口號(hào) 默認(rèn)5000
27 app.run(debug=True,port=8000)
運(yùn)行效果圖
很多初學(xué)者,對(duì)Python的概念都是模糊不清的,Python能做什么,學(xué)的時(shí)候,該按照什么線路去學(xué)習(xí),學(xué)完往哪方面發(fā)展,想深入了解,詳情可以點(diǎn)擊有道云筆記鏈接了解:http://note.youdao.com/noteshare?id=e4fa02e7b56d7909a27674cdb3da08aa
總結(jié)
以上是生活随笔為你收集整理的python开发专属表情包_Python开发个人专属表情包网站的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: mysql 5.7 centos 7_C
- 下一篇: python文件命名文件夹怎么删除_py