有關itchat接口的知識,小編是初步學習,這里先給大家分享一段代碼用法示例。
sudo pip3 install itchat
今天用了下itchat接口,從url=”https://lvyou.baidu.com/”上爬了數據,可以根據對方發的城市拼音比如qingdao自動回復這個城市的旅游信息。
有很多地方還沒搞明白,但是程序照著數據分析那個公眾號的一篇文章敲得,是可以運行了。具體的代碼不到五十行:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
#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接口使用示例的全部內容,希望對大家有所幫助。有什么問題可以隨時留言,小編會及時回復大家的。感謝朋友們對本站的支持!
原文鏈接:http://blog.csdn.net/baidu_29894819/article/details/78068950