Python developers dedicated personal expression package website

"Expression package" is a use of images to represent a way of feeling. Package is a popular expression after culture in social software active form of popular expression package on top of the Internet, everyone will leave the basic situation.

Once you have had the time could not find expression package to deal with others.

Today small share how to develop personal and exclusive use of the Python expression package site, think about what expression package found there!

 

 

 

Benpian divided into two parts

1, crawling expression package stored in the database

2, build a personal and exclusive personal expression site

Crawling feeling pack package stored in the database

Environment: Windows + Python3.6

IDE: personal preferences

Module

1 import requests
2 import re
3 import pymysq
The complete code
. 6  Import Requests
 . 7  Import Re
 . 8  Import pymysql
 . 9  # connect to the database 
10 DB = pymysql.connect (Host = ' 127.0.0.1 ' , Port = 3306, DB = ' DB ' , = User ' the root ' , the passwd = ' the root ' , = charset ' UTF8 ' )
 . 11  # create a cursor 
12 is cursor = db.cursor ()
 13 is  # the cursor.execute ( 'SELECT * from Images') 
14  #Print (cursor.fetchall ()) 
15  # small hump 
16  # comment acquired image list 
. 17  DEF getImagesList (Page):
 18 is      # Get source network bucket FIG 
. 19      HTML requests.get = ( ' http://www.doutula.com / Photo / List /? Page = {} ' .format (Page)). text
 20 is      # regular expression wildcard. *? match all matching packets 
21 is      REG R & lt = ' Data-Original = "(. *?)." * ? alt = "(. *?)" ' 
22      # to increase the efficiency of matching multi-line matching S 
23      REG = re.compile (REG, re.S)
 24-      imagesList = re.findall (REG, HTML)
 25      for i in imagesList:
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('第{}页'.format(i))
35     getImagesList(i)

 

Renderings

 

 

 

The framework uses a Flask

. 1  from Flask Import the Flask
 2  from Flask Import the render_template
 . 3  from Flask Import Request
 . 4  Import pymysql
 . 5  # 404 page not found 
. 6 App = the Flask ( the __name__ )
 . 7  # decorator 
. 8 @ app.route ( ' / ' )   # route route 
. 9  DEF index ():
 10      # return "Hello World" 
. 11      return the render_template ( 'index.html')
12 @app.route('/search')
13 def search():
14     # 接收用户关键字
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 is  # entry program 
21 is  IF  the __name__ == ' __main__ ' :
 22 is      DB = pymysql.connect (Host = ' 127.0.0.1 ' , Port = 3306, DB = ' DB ' , = User ' the root ' , = the passwd ' the root ' , charset = ' UTF8 ' , cursorClass = pymysql.cursors.DictCursor)
 23 is      # create a cursor 
24      cursor = db.cursor ()
 25      # debug mode 
26     # Port default port number 5000 
27      app.run (Debug = True, Port = 8000)

Run renderings

 

 


Many beginners, the concept is vague Python, Python can do, when to learn, in accordance with what the line to learn, learn where to complete the development, want to learn more details, you can click on the proper way cloud notes link Learn: http: //note.youdao.com/noteshare id = e4fa02e7b56d7909a27674cdb3da08aa?

Guess you like

Origin www.cnblogs.com/ITbiancheng/p/12114252.html