itchat接口使用示例

yipeiwu_com5年前Python基础

有关itchat接口的知识,小编是初步学习,这里先给大家分享一段代码用法示例。

sudo pip3 install itchat

今天用了下itchat接口,从url=”https://lvyou.baidu.com/”上爬了数据,可以根据对方发的城市拼音比如qingdao自动回复这个城市的旅游信息。

有很多地方还没搞明白,但是程序照着数据分析那个公众号的一篇文章敲得,是可以运行了。具体的代码不到五十行:

#Coding='utf-8'
from time import ctime
from bs4 import BeautifulSoup
import itchat
import urllib.request
from pandas import Series
itchat.login()
Help="""please input pinyin
such as 
巨野--juye
"""
itchat.send(Help,toUserName='filehelper')
@itchat.msg_register(itchat.content.TEXT)
def getcity(msg):
  print(msg['Text'])
  pinyin=msg["Text"]
  results=getTOUR(pinyin)
  itchat.send(results,msg["FromUserName"])
def getTOUR(pinyin):
  try:
    url="https://lvyou.baidu.com/"+pinyin#ba xiamen tihuancheng pinyin
    page=urllib.request.urlopen(url)
    html=page.read()
    soup=BeautifulSoup(html.decode('utf-8'),"html.parser")
    name=soup.find("h3",{'class',"title"}).text
    grade = soup.find("div", {'class', "main-score"}).text
    describe = soup.find("p", {'class', "main-desc-p"}).text
    season = soup.find("span", {'class', "main-besttime"}).text
    advice = soup.find("span", {'class', "main-dcnt"}).text
    output=name+'\n'+grade+'\n'+describe+'\n'+season+'\n'+advice+'*'*25+u'\n推荐:'
  except NameError:
    output="not found this place,please ensure that your input place is exist"
  return output
#sites=soup.find_all('div',{'class','unmis-li'})
if __name__== '__main__':
  itchat.run()

总结

以上就是本文关于itchat接口使用示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:python实现微信接口(itchat)详细介绍基于Python和Scikit-Learn的机器学习探索等,有什么问题可以随时留言,小编会及时回复大家的。感谢朋友们对本站的支持!

相关文章

python版简单工厂模式

python版简单工厂模式

什么是简单工厂模式 工厂模式有一种非常形象的描述,建立对象的类就如一个工厂,而需要被建立的对象就是一个个产品;在工厂中加工产品,使用产品的人,不用在乎产品是如何生产出来的。从软件开发的角...

python按行读取文件并找出其中指定字符串

python按行读取文件并找出其中指定字符串 #coding=utf-8 import os, time, sys, re #reload(sys) #sys.setdefaul...

Django错误:TypeError at / 'bool' object is not callable解决

使用 Django自带的 auth 用户验证功能,编写函数,使用 is_authenticated 检查用户是否登录,结果报错: TypeError at / 'bool' object...

Python入门篇之对象类型

Python入门篇之对象类型

Python使用对象模型来存储数据。构造任何类型的值都是一个对象 所有的Python对象都拥有三个特性:身份、类型、值 身份: 每一个对象都有一个唯一的身份来标志自己,任何对象的身份可以...

浅析Python中的多进程与多线程的使用

在批评Python的讨论中,常常说起Python多线程是多么的难用。还有人对 global interpreter lock(也被亲切的称为“GIL”)指指点点,说它阻碍了Python的...