Python学习笔记-WXPY初识

项目地址:https://github.com/youfou/wxpy

帮助文档:https://wxpy.readthedocs.io/zh/latest/index.html

安装:pip install -U wxpy

简单使用代码记录

# -*- coding:utf-8 -*-

import os
import sys
import json
import urllib
import requests
from wxpy import *
from bs4 import BeautifulSoup

default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)


def handle_dt(keyword, max_size=10):
    resp = requests.get('https://www.doutula.com/search', {'keyword': keyword})
    b_soup = BeautifulSoup(resp.content, 'lxml')
    a_tags = b_soup.select('#search-result-page > div > div > div > div > div.search-result.list-group-item > div > div > a > img')
    picture_name_list = []
    i = 0
    for a_tag in a_tags:
        link = a_tag.get('data-original')
        if link is not None and str(link).endswith('jpg'):
            link = str(link).strip()
            picture_name = link[link.rindex('/') + 1:]
            parent_dir = os.path.join(os.path.dirname(__file__), 'picture')
            urllib.urlretrieve(link, os.path.join(parent_dir, picture_name))
            picture_name_list.append(picture_name)
            i += 1
            if i == max_size:
                break
    return picture_name_list


def print_user_info(user):
    print '{} {} {} {} {} {} {} {}'.format(user.is_friend, user.name, user.nick_name,
        user.remark_name, user.sex, user.signature, user.province, user.city)


if __name__ == '__main__':
    bot = Bot(cache_path=True)
    # 设置历史消息的最大保存数量
    bot.messages.max_history = 1000

    friends = bot.friends()

    t_friend_1 = friends.search(u'朋友1')[0]
    print_user_info(t_friend_1)

    t_friend_2 = friends.search(u'朋友2')[0]
    print_user_info(t_friend_2)

    groups = bot.groups()

    t_group_1 = ensure_one(groups.search(u'组1'))
    for member in t_group_1.members:
        print_user_info(member)

    t_person_1 = ensure_one(t_group_1.search(u'组员1'))
    print_user_info(t_person_1)

    history_msgs = bot.messages.search(keywords=u'关键字', sender=bot.self)
    for history_msg in history_msgs:
        print history_msg

    @bot.register(msg_types=FRIENDS)
    def listen_and_accept_friends(msg):
        a_friend = bot.accept_friend(msg.card)
        a_friend.send('hello')

    @bot.register(chats=[t_friend_1, t_friend_2])
    def listen_and_reply_friends(msg):
        print 'received msg:[{}] {}'.format(msg.type, msg.text)
        if msg.type == 'Picture':
            parent_dir = os.path.join(os.path.dirname(__file__), 'picture')
            print 'picture dir {} name {}'.format(parent_dir, msg.file_name)
            msg.get_file(save_path=os.path.join(parent_dir, msg.file_name))
        elif msg.type == 'Video':
            parent_dir = os.path.join(os.path.dirname(__file__), 'video')
            print 'video dir {} name {}'.format(parent_dir, msg.file_name)
            msg.get_file(save_path=os.path.join(parent_dir, msg.file_name))
        elif msg.type == 'Recording':
            parent_dir = os.path.join(os.path.dirname(__file__), 'recording')
            print 'recording dir {} name {}'.format(parent_dir, msg.file_name)
            msg.get_file(save_path=os.path.join(parent_dir, msg.file_name))
        else:
            msg.reply('thank you ! i have received it')

        parent_dir = os.path.join(os.path.dirname(__file__), 'picture')
        picture_name_list = handle_dt(msg.text, max_size=2)
        for picture_name in picture_name_list:
            msg.reply_image(os.path.join(parent_dir, picture_name))

    @bot.register(Group)
    def listen_and_reply_groups(msg):
        msg_member = msg.member
        print 'received member {} {} {}'.format(msg_member.group, msg_member.name, msg_member.display_name)
        print 'received msg:[{}] {}'.format(msg.type, msg.text)
        sender = msg.sender
        if isinstance(sender, Group):
            print_user_info(sender.owner)
        receiver = msg.receiver
        if isinstance(receiver, User):
            print_user_info(receiver)
        if isinstance(msg.chat, Group) and msg.is_at:
            msg.reply('thank you ! i have received it')

    @bot.register(t_group_1)
    def listen_and_reply_t_group_1(msg):
        parent_dir = os.path.join(os.path.dirname(__file__), 'picture')
        picture_name_list = handle_dt(msg.text, max_size=4)
        for picture_name in picture_name_list:
            msg.reply_image(os.path.join(parent_dir, picture_name))
        if msg.member == t_person_1:
            msg.forword(bot.file_helper, prefix=u'留言')

    embed()

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python学习笔记》是由皮大庆编写的一本关于Python语言学习的教材。在这本书中,作者详细介绍了Python语言的基础知识、语法规则以及常用的编程技巧。 首先,作者简要介绍了Python语言的特点和优势。他提到,Python是一种易于学习和使用的编程语言,受到了广大程序员的喜爱。Python具有简洁、清晰的语法结构,使得代码可读性极高,同时也提供了丰富的库和模块,能够快速实现各种功能。 接着,作者详细讲解了Python的基本语法。他从变量、数据类型、运算符等基础知识开始,逐步介绍了条件语句、循环控制、函数、模块等高级概念。同时,作者通过大量的示例代码和实践案例,帮助读者加深对Python编程的理解和应用。 在书中,作者还特别强调了编写规范和良好的编程习惯。他从命名规范、注释风格、代码缩进等方面指导读者如何写出清晰、可读性强的Python代码。作者认为,良好的编程习惯对于提高代码质量和提高工作效率非常重要。 此外,作者还介绍了Python的常用库和模块。他提到了一些常用的库,如Numpy、Pandas、Matplotlib等。这些库在数据处理、科学计算、可视化等领域有广泛的应用,帮助读者更好地解决实际问题。 总的来说,《Python学习笔记》是一本非常实用和全面的Python学习教材。通过学习这本书,读者可以系统地学习和掌握Python编程的基础知识和高级应用技巧,为以后的编程学习和工作打下坚实的基础。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值