美文网首页
pyhton学习笔记

pyhton学习笔记

作者: 实在想不出昵称丶 | 来源:发表于2017-04-07 20:32 被阅读0次
雾!

python 是一门面向对象的脚本语言,其语法类似于英语语法的特性受很多人喜欢,简洁的编码风格不像java需要多行的代码申明。(片面之谈)

清明节假期的漫长时间,窝在寝室中,看着Python的教学视频,枯燥乏味,故而牛刀小试网络爬虫,有记得过年时期在知乎上有看到基于python的微信自动回复拜年问好,做了个自动回复糗事百科上小笑话的小脚本。

python 的类属性是定义在init方法里的,在类里调用 self.属性
类方法 里的第一个参数 为 对象自身。

对于函数里的变量,用global 修饰可以使其扩展作用域为全局变量。


# *-*  coding:UTF-8  *-*

import itchat, time, re
from itchat.content import *

import urllib,re
from urllib.request import Request,urlopen,URLError

class WechatContent:
    def __init__(self):
        self.stories=[]  #存放段子
        self.pageIndex = 1
        self.user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
        self.headers={ 'User-Agent' : self.user_agent}  #初始化headers
        self.enable=True  #程序是否运行的变量
    
    #获取网页内容
    def getWebPage(self):
        url="http://www.qiushibaike.com/hot.page"+str(self.pageIndex)
        request=Request(url,headers=self.headers)
        try:
            response=urlopen(request)
            #print(response.read())
            return response.read().decode('utf-8')  #将页面转为utf-8
        except URLError as e:
            if hasattr(e,'reason'):
                print(u"连接错误",e.reason)
                return None

    #获取段子
    def getContent(self):
        content=self.getWebPage()
        if not content:
            return 0;
        else:
            pattern=re.compile('<div class="content">\s*<span>\s*(\S*?)\s*</span>\s*</div>',re.S)
            items=re.findall(pattern,content)
            for item in items:
                item.replace("\s*","")
                self.stories.append(item)

    #发送段子
    def postContent(self):
        story=self.stories.pop() 
        #print (story)
        return story
     
    def start(self):
       if  not self.stories:
           self.pageIndex+=1
           self.getContent()

wc=WechatContent()
wc.start()
#print(wc.postContent())

priortext='hello world'

@itchat.msg_register([TEXT])
def text_reply(msg):
    if not wc.postContent():
        wc.start()
    
    text=wc.postContent()
    friend=itchat.search_friends(userName=msg['FromUserName'])
    print(friend['RemarkName'])
    print(msg['Text'])
    print("回复:\t",text)
    itchat.send(text ,msg['FromUserName'])

@itchat.msg_register([PICTURE, RECORDING, VIDEO, SHARING])
def other_reply(msg):
    itchat.send(priortext, msg['FromUserName'])

itchat.auto_login(enableCmdQR=True,hotReload=True)
itchat.run()

哈哈,自动回复中我后来加了,收到消息中有“什么”字样时,回复“我给找的段子搞笑吧,哈哈”,然后朋友以为我给他找了好多段子。

相关文章

  • 2020-08-13 python 内置函数

    学习 pyhton 遇到内置函数,持续笔记中....... round(number ,[digits]) 返回 ...

  • Pyhton学习笔记

    Python数据类型 基本类型数字整型 int长整形 long浮点型 float字符串文本 str字节 bytes...

  • pyhton学习笔记

    python 是一门面向对象的脚本语言,其语法类似于英语语法的特性受很多人喜欢,简洁的编码风格不像java需要多行...

  • Pyhton学习笔记(二)

    欢迎关注零壹研究所 【微信号: Research_LAB】 从词云图开始第一个Python实例 一、引言 词云图其...

  • [百人计划]Pyhton-Requests之接口测试

    非常感谢[百人计划]五娃的分享!下面是整理的笔记: 一、环境准备: Pyhton 2.x或者Pyhton 3.x、...

  • pyhton学习

    1、print()函数也可以接受多个字符串,用逗号“,”隔开,就可以连成一串输出: >>>print('The q...

  • go基础语法学习,对比python

    因为以前用的语言是pyhton,所以笔记中会和python做对比,和大家一起探讨,希望多多评论,互相学习提高。 g...

  • 学习pyhton: argparse模块

    简介 当遇到需要参数的情况时,往往有以下三种处理方法[1]:直接给定这种方法实现起来方便,但是灵活性稍差,每次都需...

  • python(—)了解、和简单的数据类型转换

    一认识python image.png (1) 二、 pyhton运算: 三、 pyhton变量: 什么是变量对于...

  • Python脚本一件打包

    终端调用pyhton命令

网友评论

      本文标题:pyhton学习笔记

      本文链接:https://www.haomeiwen.com/subject/eagwwttx.html