<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>json模拟数据</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
.container {
width: 800px;
margin: 200px auto;
padding: 30px 0;
border: 1px dashed #272627;
}
.test {
width: 100%;
padding: 10px 0;
margin: 20px 0;
color: #4bade8;
font-size: 18px;
text-align: center;
}
#jsonList {
width: 500px;
margin: 0 auto;
}
#jsonTable th,
td {
padding: 6px 0;
line-height: 30px;
text-align: center;
}
</style>
</head>
<body>
<div class="container">
查询:<input id="shoppname">
<button onclick="getAir()">开始</button>
<div class="test">城市信息数据</div>
<table border="1" cellspacing="0" bordercolor="#4bade8" cellpadding="0" id="jsonList">
<tr>
<th>商品图片</th>
<th>标题</th>
<th>销量</th>
<th>价格</th>
</tr>
</table>
</div>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
});
function getAir() {
var temp = '';
var shoppname = $("#shoppname")
$.ajax({
type: "get",
url: "http://127.0.0.1:8888/login?name="+shoppname.val(),
crossDomain:true,
withCredentials: true,
dataType: "json",
success: function(res) {
var list = res.air;
console.log(list);
for(var $i = 0; $i < list.length; $i++) {
temp +=
'<tr>' +
'<td><img src=https://img10.360buyimg.com/n7/'+ list[$i].image_url+'></td>'+
'<td>' + list[$i].ad_title + '</td>' +
'<td>' + list[$i].ad_title + '</td>' +
'<td>' + list[$i].fuzzy_comment_num + '</td>' +
'<td>' + list[$i].pc_price + '</td>' +
'</tr>';
}
$("#jsonList tr:not(:first)").html("");
$("#jsonList").append(temp);
}
});
}
</script>
</body>
</html>
# This is a sample Python script.
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import requests
import flask, json
from flask import request
from functools import wraps
from flask import make_response
# 创建一个服务,把当前这个python文件当做一个服务
from flask_cors import *
server = flask.Flask(__name__)
CORS(server, supports_credentials=True)
headers = {
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36',
'Cookie': 'shshshfpa=5719f356-617a-a963-c125-b68ffb376a27-1655426903; shshshfpb=mc6Ix2qAeARfFCYp97pfRgg; __jdu=522058129; areaId=20; ipLoc-djd=20-1715-0-0; unpl=JF8EAKtnNSttW0wHDB0EGBsVQ14DW19dHB8AZ2AMBg5QTFQGTFUcFxd7XlVdXhRKEB9vYRRVVVNIUQ4bCysSEXteXVdZDEsWC2tXVgQFDQ8VXURJQlZAFDNVCV9dSRZRZjJWBFtdT1xWSAYYRRMfDlAKDlhCR1FpMjVkXlh7VAQrBhgREU9UVF1VOHsQM19XAlxUUUpkBCsDK1BEF15XWFsOBhMAbGYBXV1bQ2QEKwE; __jdv=76161171|baidu-pinzhuan|t_288551095_baidupinzhuan|cpc|0f3d30c8dba7459bb52f2eb5eba8ac7d_0_26b87728492f43df92968cb9612ff646|1655428913944; __jda=122270672.522058129.1655426897.1655426914.1655428914.2; __jdc=122270672; shshshfp=5daf55a5f6651ee4daeaeb350d688ac1; _gia_s_local_fingerprint=694e91f20a9c0034add669b38a1635be; mba_muid=522058129; wlfstk_smdl=9mxgmgoabz90ko50ikngzjv2ijvmiyds; ip_cityCode=1715; 3AB9D23F7A4B3C9B=WC647EA3LN4QKED7CK6UDPBV52MLI4PNMGDKRIHXZPKU3FBSIXYEEBOMEYKNWT2GWONSSZJ4PVXXXSXYNB5WC6DXPQ',
}
# server.route()可以将普通函数转变为服务 登录接口的路径、请求方式
@server.route('/login', methods=['get', 'post'])
def login():
# 获取通过url请求传参的数据
username = request.values.get('name')
print(username+"传传传传传")
url = 'https://search-x.jd.com/Search?callback=jQuery3783932&area=50&enc=utf-8&keyword=+' + username + '+&adType=7&page=1&ad_ids=291%3A33&xtest=new_search&_=1655429655358'
resp = requests.get(url, headers=headers)
respjson = resp.text.split("jQuery3783932(")[1]
respjson1 = respjson[0:len(respjson) - 1]
print(respjson1)
test = json.loads(respjson1.replace('291', 'air', 1))
# json_data = resp.json(test)
resu = test
return json.dumps(resu, ensure_ascii=False) # 将字典转换为json串, json是字符串
#
# https://cd.jd.com/description/channel?skuId=10025631728908&mainSkuId=100029662616&charset=utf-8&cdn=2&callback=showdesc
def allow_cross_domain(fun):
@wraps(fun)
def wrapper_fun(*args, **kwargs):
rst = make_response(fun(*args, **kwargs))
rst.headers['Access-Control-Allow-Origin'] = '*'
rst.headers['Access-Control-Allow-Methods'] = 'PUT,GET,POST,DELETE'
allow_headers = "Referer,Accept,Origin,User-Agent"
rst.headers['Access-Control-Allow-Headers'] = allow_headers
return rst
return wrapper_fun
if __name__ == '__main__':
server.run(debug=True, port=8888, host='0.0.0.0') # 指定端口,host,0.0.0.0代表不管几个网卡,任何ip都可访问