1、外网穿透工具下载与注册【http://ngrok.ciqiuwl.cn/】
2、公众号审请
3、代码编写
糗事百科接口 # _*_ coding:utf-8 _*_ import requests from lxml import etree from random import randint def get_joke(): url="http://www.qiushibaike.com/text/page/"+ str(randint(1,5)) r = requests.get(url) tree = etree.HTML(r.text) contentlist = tree.xpath(\'//div[@class="content"]/span\') jokes = [] for content in contentlist: content = content.xpath(\'string(.)\') # string() 函数将所有子文本串联起来,# 必须传递单个节点,而不是节点集。 if \'查看全文\' in content: # 忽略包含“查看原文”笑话 continue jokes.append(content) joke = jokes[randint(1, len(jokes))].strip() return joke if __name__ == "__main__": content = get_joke() print(content)
结果:
看到好友动态一个治脚气的偏方晚上我准备调配好给老公泡脚,刚放姜片,盐和醋,我女儿跑过来正好看见我拉着她爸爸的脚往盆里放,
盆里还有调料淌着哈喇子蹲下来就问我:麻麻,我们晚上吃爸爸脚丫子呀