這里是 基于Django耘柱、WeRoBot的微信公眾平臺開發(fā)(二) 的后續(xù)如捅,主要記錄其它功能的開發(fā)。
“優(yōu)美詩句”功能
原理:爬取優(yōu)美詩句调煎,存入數(shù)據(jù)庫镜遣,封裝接口,調(diào)用接口
作用:在公眾號里回復(fù)“來個優(yōu)美的句子”或者“來句詩”,它便會回復(fù)給你一條優(yōu)美的詩句悲关。
過程:
實現(xiàn)過程和“講個笑話”功能類似谎僻。
1、創(chuàng)建數(shù)據(jù)庫表
作用:在數(shù)據(jù)庫(mysql)中創(chuàng)建用于存儲詩句的表
create table `poetrys` (
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`content` varchar(1024) NOT NULL
) DEFAULT CHARSET=utf8;
2寓辱、編寫段子抓取腳本
作用:抓取 句子迷中“句集:小明”中的句子艘绍,存儲在數(shù)據(jù)庫中(只存儲之前沒出現(xiàn)過的詩句,避免重復(fù))秫筏。
#coding=utf-8
import urllib
import urllib2
import re
import MySQLdb
timeout=5 # 超時時間
host = 'http://www.juzimi.com' # 句子迷主頁面
target = 'album/1572107' # 句子迷“句集:小明”欄目
def get_html(url,timeout=None):
# 獲取指定url的html源碼
try:
headers = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6' }
request = urllib2.Request(url,headers=headers)
response = urllib2.urlopen(request,timeout=timeout)
except Exception,e:
raise '[Error] get_html()獲取源碼失敗\n' + e
return response.read()
def connectMySQL():
# 連接mysql數(shù)據(jù)庫
conn = MySQLdb.connect(
host='localhost',
port=3306,
user='YOUR_USERNAME',
passwd='YOUR_PASSWORD',
db='blog',
charset='utf8',
)
return conn
def getPoetry():
# 抓取詩句
# 獲取總頁數(shù)
try:
pagesum = 7
except Exception,e:
print e
return []
poetry_list = []
# 開始爬取
for page in range(0,pagesum):
print '當(dāng)前頁數(shù):',page
url = '%s/%s?page=%d'%(host,target,page)
try:
html = get_html(url,timeout).decode('utf8')
except Exception,e:
print e
print '抓取出錯诱鞠,跳過第%s頁'%page
continue
print '正在匹配……'
pattern = re.compile("<div id=\"bdshare\" class=\"bdshare_t bds_tools get-codes-bdshare\" data=\"{'text':'.*?','desc':'(.*?)','url':'.*?','pic':'.*?'}\">",re.S)
items = re.findall(pattern,html)
# 匹配到詩句
for item in items:
print item
poetry_list.append(item)
return poetry_list
def save2mysql(poetry_list):
# 將抓取的段子存入數(shù)據(jù)庫
conn = connectMySQL()
cur = conn.cursor()
for i,poetry in enumerate(poetry_list):
print '正在插入第%d句詩……'%(i+1)
sql = 'select 1 from poetrys where content = "%s" limit 1; '%(poetry)
isExist = cur.execute(sql)
if isExist==1:
print '-> 該詩句已存在于數(shù)據(jù)庫!放棄插入这敬!'
else:
sql = 'insert into poetrys (`content`) values ("%s")'%( poetry )
cur.execute(sql)
print '正在提交以上所有操作……'
conn.commit()
def main():
# 主程序
try:
poetry_list = getPoetry()
save2mysql(poetry_list)
except Exception,e:
print e
if __name__=='__main__':
main()
3航夺、接口函數(shù)實現(xiàn)
作用:每次調(diào)用,從數(shù)據(jù)庫中隨機取出一條詩句崔涂,以json的格式返回阳掐。
前言:代碼集成在django中,不想在django中使用的可以適當(dāng)修改代碼冷蚂。
代碼:
from django.http import HttpResponse
import MySQLdb
import random
import json
# 公共部分
# 數(shù)據(jù)庫設(shè)置
username = 'YOUR_USERNAME' # 你的數(shù)據(jù)庫用戶名
password = 'YOUR_PASSWORD' # 你的數(shù)據(jù)庫密碼
dbname = 'YOUR_DB' # 你創(chuàng)建的表所在的數(shù)據(jù)庫
dbport = 3306
# 數(shù)據(jù)庫連接函數(shù)
def connectMySQL():
# 連接mysql數(shù)據(jù)庫
conn = MySQLdb.connect(
host='localhost',
port=dbport,
user=username,
passwd=password,
db=dbname,
charset='utf8',
)
return conn
# 接口部分
# 返回一條詩句
def get_poetry(request):
response = ''
try:
# 連接數(shù)據(jù)庫
conn = connectMySQL()
cur = conn.cursor()
# 生成隨機抓取id
sql = 'select count(*) from poetrys'
cur.execute(sql)
poetry_sum = cur.fetchone()[0]
poetry_idx = random.randint(1,poetry_sum)
# 抓取該id的段子數(shù)據(jù)
sql = 'select * from poetrys where id=%d'%poetry_idx
cur.execute(sql)
poetry = {}
poetry['id'],poetry['content'] = cur.fetchone()
response = json.dumps(poetry,ensure_ascii=False)
# 關(guān)閉數(shù)據(jù)庫連接
cur.close()
conn.close()
except Exception as e:
print e
logger.error(e)
return HttpResponse(response)
前端接口封裝好之后缭保,可以在瀏覽器中輸入以下url測試這個接口:
http://www.yangyingming.com/api/get_poetry/
每次刷新都會返回不同的詩句。
4蝙茶、集成在微信機器人中
作用:將“優(yōu)美詩句”功能集成到微信機器人的聊天功能中涮俄,用戶在聊天窗口發(fā)送“來個句子”類似的消息時,隨機回復(fù)一條詩句尸闸。
代碼:
@robot.text
def echo(message):
if re.compile(".*?詩.*?").match(msg) or\
re.compile(".*?句.*?").match(msg):
apiurl = "http://www.yangyingming.com/api/get_poetry"
response = get_html(apiurl,timeout=timeout)
joke = json.loads(response)
return joke['content'].encode('utf8')
運行效果:
運行效果