这一节我们讲商城 食谱 消息

其实商城没什么可说的,数据库加载,买东西都要先加入购物车,一起结算





食谱就是获取数据库的信息,那么说起来信息,那么是怎么获取的呢
附上Python爬虫思想
Python是搜索引擎非常核心的元素,爬虫技术经历了这么多年,技术已经百般变化,许多前人也开发了各种类型的爬虫以满足需求,本项目数据主要是通过Python爬虫来获取数据,通过数据更新来使微信云开发数据库内容更加丰富,也使小程序呈现的内容更加丰富。
网络爬虫是一种自动快速提取网页的软件程序,它也是搜索引擎的重要组成,在爬取的过程中,需要从网页上不断的抓取新的URL并放入队列,直到满足一定的要求才会停止,它主要是在爬取有用的信息并把新链接并放入队列的过程中重复执行,直到满足条件,现在网络爬虫面临许多问题,最主要的是爬取数据的分析筛选和对URL的搜索策略。现在因特网上的网页数量已经超出了人们的想象,但是研究表明,这些网站30%都是重复的,这就使得网络爬虫举步维艰。在给定时间里只能获取到少量信息。除了爬取数据的分析筛选,还有搜索策略也尤为重要,目前网络爬虫常用的三种为深度优先、广度优先和最佳优先,但是深度优先会使得爬虫陷入较深的地方,无法从全局考虑,所以目前网络爬虫常用的方法就是广度优先和最佳优先方法。广度优先在搜索过程中,会一层一层的搜索,当搜索完一层后,才会继续下一层的搜索,这就使得抓取的信息比较均匀,但是随着抓取的越来越多,大量垃圾网页会被爬取,这个时候,算法的效率也会变低,这个时候最近优先算法就显得尤为快捷了,它会预测URL和目标网站的相似度,并且,会选择评价好的进行访问,但是这个方法也存在很大的问题,也就是,它会漏掉一些有用的网址导致了局部搜索。
Python爬虫爬取到的JSON数据需要存储到微信云开发数据库中。集成的数据库非常的快捷,也拥有很多不错的功能。
本项目初始化部分如下。所有咨询以及食谱均通过爬虫获取,接下来就会将到本项目获取数据的详细情况,核心代码如下所示:
leftType = soup.find_all('标签名',{'class':'类名'})
for a0 in leftType: #遍历一级结构
allLeft = a0.find_all('标签名')
for a1 in allLeft: #部分省略,遍历二级结构
Allrecipe = BeautifulSoup(html,"html.parser")#通过函数解析html
for aa in Allrecipe.find_all('标签名',{'class':'类名'}):
getRecipe()#获取食谱函数
通过Python的Request或者Get请求获取特定网页,根据响应结果,使用BeautifulSoup解析,使用soup = BeautifulSoup(html,"html.parser")解析网站,data= soup.find_all()获取所有有关数据,最后将数据通过Python的JSON库转化为可操作的字典类型,进行的字符处理操作在下面会讲到,然后到微信小程序云开发数据库中。
本小节从食谱来讲获取来源,首先确定目标网站,该网站是关于菜谱的健康食疗。https://www.xinshipu.com/食谱大全.html是其网址,目标网站一级结构如下所示,从该结构中,获取到对应种类的名称和网页相对地址,循环遍历每个地址并记录。例如,获取到所有的a标签且rel为’cpdp’,获取到所有二级结构的地址,获取主要食材的名称,并进入二级结构进一步获取如下所示。进到二级结构,二级结构主要是该主要食材下的各类做法。二级结构HTML代码如下:
<div class="detail-cate">
<div class="bpannel">
<div class="c-name">
<div class="niu" id="食谱类型">食谱类型</div>
</div>
<ul class="line-list clearfix">
<li><a href="食谱网站地址" rel="cpdq">食谱类型</a></li>
<li><a href="食谱网站地址" rel="cpdq">食谱类型</a></li>
</div>
</div>(仅部分)
获取所有的a标签且class为‘shipu’,获取详情页面,且记录菜谱名称,进入详情页面,获取该网页的JSON结构。并通过Python解析JSON,做一些定制化的修改,其中JSON的结构如下所示,爬取的JSON格式数据写入文件,最后在云开发数据库导入。JSON结构如下所示:
{
"name": "这里是菜谱名称",
"image": "这里是图片地址",
"description": "这里是描述",
"recipeIngredient": ["这里是配料数组"],
"recipeInstructions": "这里是步骤"
}
爬虫前首先要做的是伪装请求头,把自己伪装为浏览器进行访问。其次,通过getHtml函数抓取页面的内容。
获取到一级目录下二级目录的网址,处理代码如下:
for second in Allrecipe.find_all('a',{'class':'shipu'})
最后获取所有的JSON和其菜谱名称,处理代码如下所示:
soup3 = soup2.find('script',{'type':'application/ld+json'})
jsonStr = soup3.string.replace("\r", "").replace("\n", "")
jsonData = eval(jsonStr)
recipeData = '"id":{},"image":"{}", "description":"{}", "name":"{}",\
"recipeIng":{},"recipeIns":"{}","recipeType":"{}"'.format(str(count),\
"https://6865-health-assistant-qvqzg-1301723400.tcb.qcloud.la/"+\
a0.h3.string+'-'+jsonData['name']+".jpg" ,jsonData['description'],\
jsonData['name'],jsonData['recipeIngredient'],
jsonData['recipeInstructions'],a0.h3.string)
recipeData = "{"+recipeData+"}"
获得的数据可以直接导入数据库,完美~
说到消息的实现

当加载这个页面是,需要监听数据库的变化
onLoad: function (options) {
this.setData({ myId:getApp().globalData.user_id})
var that = this
db.collection('messagesData').where(
cc.or([
{
user_1: getApp().globalData.user_id
},
{
user_2: getApp().globalData.user_id
}
])
).watch({
onChange: function (snapshot) {
if (snapshot.type == 'init') {
that.setData({ lookLook: false })
}
console.log("聊天列表更改... = ", snapshot)
if (snapshot.type != 'init') {
wx.cloud.callFunction({
name: 'searchMyMsg',
data: {
userid: getApp().globalData.user_id
},
complete: res => {
console.log(res.result.data)
that.setData({ messageData: res.result.data })
}
})
}
},
onError: (err) => {
console.error(err)
}
})
this.searchMyMsg()
}
当加载到个人页面时
wx.cloud.init()
wx.cloud.callFunction({
name: 'searchMsgDataById',
data:{
user1: otherId > myId ? myId : otherId,
user2: otherId < myId ? myId : otherId
},
complete: res => {
console.log(res)
if (res.result.data.length != 0){
that.setData({ msgInfo: res.result.data[0] })
const db = wx.cloud.database()
db.collection('messagesData').where({
user_1: otherId > myId ? myId : otherId,
user_2: otherId < myId ? myId : otherId
}).watch({
onChange: function (snapshot) {
console.log("聊天更改... = ", snapshot)
if (snapshot.type == 'init') {
that.setData({afterLook:false})
}
if (snapshot.type != 'init') {
if (snapshot.docChanges[0].dataType == "update") {
console.log("聊天更新")
var msgInfo = that.data.msgInfo
msgInfo.info = snapshot.docs[0].info
that.setData({ msgInfo: msgInfo })
}
}
},
onError: (err) => {
console.error(err)
}
})
}else{
wx.cloud.callFunction({
name: 'addMsgCode',
data: {
user_1: otherId > myId ? myId : otherId,
user_2: otherId < myId ? myId : otherId,
userImage1: otherId > myId ? that.data.myImage : otherImage,
userImage2: otherId < myId ? that.data.myImage : otherImage
},
complete: res => {
this.onLoad({id:that.data.otherId,image:that.data.otherImage})
// wx.switchTab({
// url: '/pages/message/message',
// })
// wx.showToast({
// icon:'success',
// title: '会话创建完成'
// })
// const db = wx.cloud.database()
// db.collection('messagesData').where({
// user_1: otherId > myId ? myId : otherId,
// user_2: otherId < myId ? myId : otherId
// }).watch({
// onChange: function (snapshot) {
// if (snapshot.type == 'init') {
// that.setData({ firstLook: false })
// }
// console.log("聊天更改111... = ", snapshot)
// if (snapshot.type != 'init') {
// if (snapshot.docChanges[0].dataType == "update") {
// console.log("聊天更改111")
// var msgInfo = that.data.msgInfo
// msgInfo.info = snapshot.docs[0].info
// that.setData({ msgInfo: msgInfo })
// }
// }
// },
// onError: (err) => {
// console.error(err)
// }
// })
}
})
}
}
})
最终实现了。。。。
本文介绍了一种利用Python爬虫技术抓取商城食谱数据的方法,详细解析了如何从目标网站爬取食谱信息,包括菜谱名称、图片、描述、配料及步骤,以及如何将JSON数据存储至微信云开发数据库。
--- 其中食谱的获取(Python向)&spm=1001.2101.3001.5002&articleId=107533751&d=1&t=3&u=bc509d8e5eeb48708b19032dc65d2342)

被折叠的 条评论
为什么被折叠?



