标签:art python3.x The ace 可以转化 用户名 登陆 print dir
https://www.cnblogs.com/sixrain/p/9138442.html
用Python如何写一个接口呢,首先得要有数据,可以用我们在网站上爬的数据,在上一篇文章中写了如何用Python爬虫,有兴趣的可以看看:
1
2
3
|
# 连接数据库,需指定charset否则可能会报错 db = pymysql.connect(host= "localhost" , user= "root" , password= "123" , db= "mysql" , charset= "utf8mb4" ) cursor = db.cursor() # 创建一个游标对象 |
1
2
3
4
5
6
7
8
9
10
11
|
cursor.execute( "DROP TABLE IF EXISTS meizi_meizis" ) # 如果表存在则删除 # 创建表sql语句 createTab = "" "create table meizi_meizis( id int primary key auto_increment, mid varchar(10) not null , title varchar(50), picname varchar(10), page_url varchar(50), img_url varchar(50) ); "" " cursor.execute(createTab) # 执行创建数据表操作 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
def html(self, href, title): lists = [] meiziid = href.split( ‘/‘ )[-1] html = self.request(href) max_span = BeautifulSoup(html.text, ‘lxml‘ ).find( ‘div‘ , class_= ‘pagenavi‘ ).find_all( ‘span‘ )[-2].get_text() for page in range(1, int (max_span) + 1): meizi = {} page_url = href + ‘/‘ + str(page) img_html = self.request(page_url) img_url = BeautifulSoup(img_html.text, ‘lxml‘ ).find( ‘div‘ , class_= ‘main-image‘ ).find( ‘img‘ )[ ‘src‘ ] picname = img_url[-9:-4] meizi[ ‘meiziid‘ ] = meiziid meizi[ ‘title‘ ] = title meizi[ ‘picname‘ ] = picname meizi[ ‘page_url‘ ] = page_url meizi[ ‘img_url‘ ] = img_url lists.append(meizi) # 保存到返回数组中 return lists |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
def all_url(self, url): html = self.request(url) all_a = BeautifulSoup(html.text, ‘lxml‘ ).find( ‘div‘ , class_= ‘all‘ ).find_all( ‘a‘ ) for index, a in enumerate(all_a): title = a.get_text() href = a[ ‘href‘ ] lists = self.html(href, title) for i in lists: # print(i[‘meiziid‘], i[‘title‘], i[‘picname‘], i[‘page_url‘], i[‘img_url‘]) # 插入数据到数据库sql语句,%s用作字符串占位 sql = "INSERT INTO `meizi_meizis`(`mid`,`title`,`picname`,`page_url`,`img_url`) VALUES(%s,%s,%s,%s,%s)" try : cursor.execute(sql, (i[ ‘meiziid‘ ], i[ ‘title‘ ], i[ ‘picname‘ ], i[ ‘page_url‘ ], i[ ‘img_url‘ ])) db.commit() print(i[0] + " is success" ) except: db.rollback() db.close() # 关闭数据库 |
然后开始写接口。我是通过Django+rest_framework来写的。
1
2
3
4
5
6
7
8
9
10
11
|
INSTALLED_APPS = [ ‘django.contrib.admin‘ , ‘django.contrib.auth‘ , ‘django.contrib.contenttypes‘ , ‘django.contrib.sessions‘ , ‘django.contrib.messages‘ , ‘django.contrib.staticfiles‘ , ‘rest_framework‘ , ‘meizi‘ , ] |
1
2
|
def index(request): return HttpResponse(u "你好" ) |
在工程目录urls.py配置
1
2
3
4
|
from learn import views as learn_views urlpatterns = [ url(r ‘^$‘ , learn_views.index), ] |
通过python manage.py runserver启动,就会看到我们输出的“你好”了
1
2
3
4
5
6
7
|
class Person(models.Model): name = models.CharField(max_length=30) age = models.IntegerField() def __unicode__(self): # 在Python3中使用 def __str__(self): return self.name |
运行命令,就可以生成对应的表
1
2
3
4
5
|
Django 1.7.1及以上 用以下命令 # 1. 创建更改的文件 python manage.py makemigrations # 2. 将生成的py文件应用到数据库 python manage.py migrate |
在views.py文件里就可以获取数据库的数据
1
2
3
4
5
6
7
8
9
10
11
12
|
def create(request): # 新建一个对象的方法有以下几种: Person.objects.create(name= ‘xiaoli‘ , age=18) # p = Person(name="WZ", age=23) # p = Person(name="TWZ") # p.age = 23 # p.save() # 这种方法是防止重复很好的方法,但是速度要相对慢些,返回一个元组,第一个为Person对象, # 第二个为True或False, 新建时返回的是True, 已经存在时返回False # Person.objects.get_or_create(name="WZT", age=23) s = Person.objects. get (name= ‘xiaoli‘ ) return HttpResponse(str(s)) |
接口使用rest_framework,rest_framework是一套基于Django 的 REST 框架,是一个强大灵活的构建 Web API 的工具包。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
DATABASES = { # ‘default‘: { # ‘ENGINE‘: ‘django.db.backends.sqlite3‘, # ‘NAME‘: os.path.join(BASE_DIR, ‘db.sqlite3‘), # } ‘default‘ : { ‘ENGINE‘ : ‘django.db.backends.mysql‘ , ‘NAME‘ : ‘mysql‘ , ‘USER‘ : ‘root‘ , ‘HOST‘ : ‘127.0.0.1‘ , ‘PASSWORD‘ : ‘123‘ , ‘PORT‘ : 3306, # show variables like ‘character_set_database‘; # 修改字段字符编码 # alter table spiders_weibo modify text longtext charset utf8mb4 collate utf8mb4_unicode_ci; ‘OPTIONS‘ : { ‘charset‘ : ‘utf8mb4‘ }, } } |
1
|
python manage.py inspectdb |
可以看到下图
取我们表的model拷贝到app下的models.py里
1
2
3
4
5
6
7
8
9
10
|
class Meizis(models.Model): mid = models.CharField(max_length=10) title = models.CharField(max_length=50, blank=True, null =True) picname = models.CharField(max_length=10, blank=True, null =True) page_url = models.CharField(max_length=50, blank=True, null =True) img_url = models.CharField(max_length=50, blank=True, null =True) class Meta: managed = False db_table = ‘meizi_meizis‘ |
创建一个序列化Serializer类
1
2
3
4
5
6
7
|
class MeiziSerializer(serializers.ModelSerializer): # ModelSerializer和Django中ModelForm功能相似 # Serializer和Django中Form功能相似 class Meta: model = Meizis # 和"__all__"等价 fields = ( ‘mid‘ , ‘title‘ , ‘picname‘ , ‘page_url‘ , ‘img_url‘ ) |
这样在views.py就可以来获取数据库的数据了
1
2
3
|
meizis = Meizis.objects.all() serializer = MeiziSerializer(meizis, many=True) return Response(serializer.data) |
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@api_view([ ‘GET‘ , ‘POST‘ ]) def getlist(request, format=None): if request.method == ‘GET‘ : meizis = Meizis.objects.all() serializer = MeiziSerializer(meizis, many=True) return Response(serializer.data) elif request.method == ‘POST‘ : serializer = MeiziSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) |
1
2
3
4
5
6
7
8
9
|
class StandardResultSetPagination(LimitOffsetPagination): # 默认每页显示的条数 default_limit = 20 # url 中传入的显示数据条数的参数 limit_query_param = ‘limit‘ # url中传入的数据位置的参数 offset_query_param = ‘offset‘ # 最大每页显示条数 max_limit = None |
在serializers.py创建俩个类,为什么是俩个?因为我们有俩个接口,一个明细,一个列表,而列表是不需要把字段的所有数据都返回的
1
2
3
4
5
6
7
8
9
10
|
class ListSerialize(serializers.ModelSerializer): class Meta: model = Meizis fields = ( ‘mid‘ , ‘title‘ ) class ListPicSerialize(serializers.ModelSerializer): class Meta: model = Meizis fields = "__all__" |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
@api_view([ ‘GET‘ ]) def getlist(request, format=None): if request.method == ‘GET‘ : meizis = Meizis.objects.values( ‘mid‘ , ‘title‘ ).distinct() # http: // 127.0.0.1:8000 / getlist?limit = 20 # http: // 127.0.0.1:8000 / getlist?limit = 20 & offset = 20 # http: // 127.0.0.1:8000 / getlist?limit = 20 & offset = 40 # 根据url参数 获取分页数据 obj = StandardResultSetPagination() page_list = obj.paginate_queryset(meizis, request) # 对数据序列化 普通序列化 显示的只是数据 ser = ListSerialize(instance=page_list, many=True) # 多个many=True # instance:把对象序列化 response = obj.get_paginated_response(ser.data) return response @api_view([ ‘GET‘ , ‘POST‘ ]) def getlispic(request, format=None): if request.method == ‘GET‘ : mid = request.GET[ ‘mid‘ ] if mid is not None: # get是用来获取一个对象的,如果需要获取满足条件的一些数据,就要用到filter meizis = Meizis.objects.filter(mid=mid) obj = StandardResultSetPagination() page_list = obj.paginate_queryset(meizis, request) ser = ListPicSerialize(instance=page_list, many=True) response = obj.get_paginated_response(ser.data) return response else : return Response(str( ‘请传mid‘ )) |
标签:art python3.x The ace 可以转化 用户名 登陆 print dir
原文地址:https://www.cnblogs.com/lgqboke/p/9931795.html