吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4874|回复: 14
收起左侧

[Python 转载] 微信自动回复相关图片基于itchat

  [复制链接]
内裤超人 发表于 2019-6-18 15:41
本帖最后由 内裤超人 于 2019-6-18 15:43 编辑

自己写来玩的,没什么技术
[Python] 纯文本查看 复制代码
# -*- coding: utf-8 -*
#!/usr/bin/env python

import re
import json
import os
import logging
import time
import itchat
import requests
from urllib import parse
from bs4 import BeautifulSoup

logging.basicConfig(level=logging.INFO, format='\n%(asctime)s - %(levelname)s: %(message)s')

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36',
    'Connection': 'keep-alive',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Referer': 'https://www.baidu.com/',
    'Accept-Encoding': 'gzip, deflate, sdch',
    'Accept-Language': 'en-US,en;q=0.8'
}

URL = "https://image.baidu.com/search/index?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=&st=-1&fm=result&fr=&sf=1&fmq=1560827301042_R&pv=&ic=&nc=1&z=&hd=&latest=©right=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&ctd=1560827301044%5E00_1902X431&sid=&word="

'''
    获取好友信息
'''
@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
    if '图片@' in msg['Text']:
        itchat.send('正在获取相关图片中...,请稍等!',toUserName=msg['FromUserName'])
        text = msg['Text'][msg['Text'].find('@') + 1:]
        get_img_url(text)
        send_img_data = read_img(make_dir())
        if len(send_img_data) > 0:
            for i in range(0,5):
                res = itchat.send_image(send_img_data[i],toUserName=msg['FromUserName'])
                if res['BaseResponse']['Ret'] == 1205:
                    return '获取过于频繁'
                else:    
                    return '获取'+msg['Text']+'成功'
        else:
            return "暂无"+msg['Text']+"相关图片"
    else:
        return '获取图片请发送 图片@搞笑,@后面为获取相关图片的描述'     

'''
    获取表情包图片路径
'''
def get_img_url(text):
    url = URL+parse.quote(text)
    rep = requests.get(url,headers=headers).text
    soup = BeautifulSoup(rep,'lxml')
    data = soup.select('li.imgitem > div.imgbox > a > img')
    url_list = []
    for url in data:
        img_url = url.get('data-imgurl')
        if re.search(r"\.(jpg|png)$",img_url):
            url_list.append(img_url)
    download_img(url_list)

'''
    下载文件
'''
def download_img(links):
    for link in links:
        img = requests.get(link,headers=headers)
        real_img = img.content
        with open(make_dir()+str(round(time.time() * 1000))+'.jpg', "wb") as img:
            img.write(real_img)
            logging.info('正在下载文件:'+link)
        logging.info('下载完成')

'''
    读取文件夹内容
'''    
def read_img(path):
    files = os.listdir(path)
    img_name = []
    for file in files:
        if not os.path.isdir(file):
            file_time = int(file[:-4])
            now_time = round(time.time() * 1000)
            if  now_time - file_time < 15000:
                img_name.append(path+file)
    return img_name  

'''
    创建文件夹
'''
def make_dir():
    path = "F:/www/Python/image/"
    isexists = os.path.exists(path)
    if not isexists:
        os.makedirs(path)
    else:
        pass
    return path
        

itchat.auto_login()
itchat.run()

免费评分

参与人数 1吾爱币 +3 热心值 +1 收起 理由
苏紫方璇 + 3 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

won-der-ful 发表于 2019-6-20 09:03

在腾讯云的centos系统按照https://www.jianshu.com/p/7aa4a427e7f1这个网址进行python3环境安装,想按照https://www.cnblogs.com/sunshine-long/p/8522175.html进行配置,结果输入命令查询版本一直找不到安装的python。
 楼主| 内裤超人 发表于 2019-6-20 09:18
won-der-ful 发表于 2019-6-20 09:03
在腾讯云的centos系统按照https://www.jianshu.com/p/7aa4a427e7f1这个网址进行python3环境安装,想按照h ...

centos 里面默认安装的是python2点多的,好像是因为yum 命令依赖python,我之前也安装过py3,会出现问题
jiang196771 发表于 2019-6-18 16:10
wenyuanzh 发表于 2019-6-18 17:00
不错不错,感谢楼主分享
头像被屏蔽
王星星 发表于 2019-6-18 17:13
提示: 作者被禁止或删除 内容自动屏蔽
won-der-ful 发表于 2019-6-18 22:05
我环境配置都弄好久了,没接触过python,也不知道是哈问题
 楼主| 内裤超人 发表于 2019-6-19 09:16
won-der-ful 发表于 2019-6-18 22:05
我环境配置都弄好久了,没接触过python,也不知道是哈问题

什么问题?
G1000 发表于 2019-7-16 14:34
好像有点问题
 楼主| 内裤超人 发表于 2019-7-16 14:42

什么问题?有些地方是要修改下
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-7 09:54

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表