itchat 运行记录

对python不熟悉,跑项目时遇到的一些问题,做个记录。
可以通过本命令安装itchat:

pip install itchat

然后运行python文件,
报错:AttributeError: ‘module’ object has no attribute’xxx 原来是因为命名py脚本时,不要与python预留字,模块名等相同(所以名字不能叫itchat会有冲突),还要删除已经生成的pyc文件。

import itchat
itchat.auto_login()
itchat.send('Hello, filehelper', toUserName='filehelper')

运行文件后会弹出一个二维码,用手机扫描登录网页版的微信,然后你的手机微信文件传输助手就会收到“Hello, filehelper”的信息了。

然后看到一个编写的项目,输入城市的拼音名字,可以返回城市的天气结果。

 #-*-coding:utf-8 -*-
import urllib.request  
from time import ctime
from bs4 import BeautifulSoup
import itchat
def getPM25(cityname):
    site = 'http://www.pm25.com/' + cityname + '.html'
    page = urllib.request.urlopen(site)
    html = page.read();
    soup = BeautifulSoup(html.decode("utf-8"),"html.parser")
    city = soup.find(class_='bi_loaction_city')  # 城市名称
    aqi = soup.find("a", {"class", "bi_aqiarea_num"})  # AQI指数
    quality = soup.select(".bi_aqiarea_right span")  # 空气质量等级
    result = soup.find("div", class_='bi_aqiarea_bottom')  # 空气质量描述
    output=city.text + u'AQI指数:' + aqi.text + u'\n空气质量:' + quality[0].text + result.text
    print(output)
    print('*' * 20 + ctime() + '*' * 20)
    return output
itchat.auto_login(hotReload=True)
Help="""
友情提示:
请输入城市拼音获取天气结果,如果无法识别,自动返回首都记录
"""
itchat.send(Help,toUserName='filehelper')
@itchat.msg_register(itchat.content.TEXT)
def getcity(msg):
    if msg['ToUserName'] != 'filehelper': return
    print(msg['Text'])
    cityname=msg['Text']
    result=getPM25(cityname)
    itchat.send(result,'filehelper')
if __name__ == '__main__':
    itchat.run()

首先会遇到:ImportError: No module named request

  • The urllib.request module is part of the Python 3 standard library.原因是python命令是2.7版本,而urllib.request是python3里的。
  • 幸好之前装过python3,直接用python3 跑还是不行,bs4和itchat都在2.7版本里,还需要用pip3都安装一遍
  • 效果如图:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值