这是我的之前写的代码,今天发布到博客园上,说不定以后需要用。

开始:

#coding:utf-8
import werobot
import pymongoclass Gongzhonghao():def __init__(self,token,APP_ID,ENCODING_AES_KEY,APP_SECRET):self.robot = werobot.WeRoBot(token = token)self.robot.config['HOST'] = '0.0.0.0'self.robot.config['PORT'] = 80self.robot.config['APP_ID'] = APP_IDself.robot.config['ENCODING_AES_KEY'] = ENCODING_AES_KEYself.robot.config['APP_SECRET'] = APP_SECRETdef _getNews_Count(self):"""获取公众号图文消息总数:return: Int"""mediacount = self.robot.client.get_media_count()news_count = mediacount['news_count']return news_countdef getNews(self):"""获取公众号所有的图文内容:return: Json"""i = 0items = []news_count = self._getNews_Count()while i < news_count:tempj = self.robot.client.get_media_list('news', i, 20)items  = tempj['item'] + itemsi = i + 20j = {'total_count': news_count,'items': items}return jdef echo(self):"""用于公众号后台初次配置的验证:return: null"""self.robot.run()if __name__ == '__main__':g = Gongzhonghao('1', '2', '3','4') j = g.getNews()client = pymongo.MongoClient('ip', 27017)db = client.gongzhonghaoxxx= db.xxxxxx.insert(j)

  然后连接数据库进行解析,数据库中包含图文消息html代码等信息。

# -*- coding:utf-8 -*-import os
import urllib.parse
from html.parser import HTMLParserimport requests
from bs4 import BeautifulSoup
from pymongo import MongoClientclass ContentHtmlParser(HTMLParser):"""过滤html标签"""def __init__(self):HTMLParser.__init__(self)self.text = ""def handle_data(self, data):self.text += datadef get_text(self):return self.textmongo_client = MongoClient("ip", 27017)
mongo_db = mongo_client["gongzhonghao"]def get_words():words = []with open("words.txt", encoding="utf-8") as words_file:for lines in words_file.readlines():if len(lines.strip()) == 0:continueif lines.find("、") != -1:for p in lines.split("、"):words.append(p.replace("\n", ""))else:words.append(lines.replace("\n", ""))return wordsdef get_articles(clt):articles = []collection = mongo_db[clt]doc = collection.find_one()items = doc["items"]for it in items:content = it["content"]["news_item"][0]articles.append(content)return articlesdef download(dir, file_name, url):if not os.path.exists(dir):os.mkdir(dir)try:resp = requests.get(url)path = dir + "\\" + file_nameif os.path.exists(path):returnwith open(path, "wb") as f:f.write(resp.content)except :print(url)def find_images(content):imgs = []c = urllib.parse.unquote(content)img_labels = BeautifulSoup(c, "html.parser").find_all("img")for img in img_labels:src = img.get("data-src")imgs.append(src)return imgsdef get_suffix(url):try:suffix = url[url.rindex("=") + 1:]if suffix == "jpeg" or suffix == "other":return ".jpg"return "." + suffixexcept:return ".jpg"def filter_content(content):parser = ContentHtmlParser()parser.feed(content)return parser.get_text()def check_jinyongci(content):fc = filter_content(content)words = get_words()invalids = []for w in words:if fc.find(w) != -1:invalids.append(w)return invalidsdef save_jinyongci(clt, title, invalids):if len(invalids) == 0:returnfile = clt + "\\invalid.txt"with open(file, "a+",encoding="utf-8") as f:f.write("标题:" + title)f.write("\r\n敏感词:")for iv in invalids:f.write(iv)f.write("、")f.write("\r\n\r\n")if __name__ == "__main__":clt = "xxx"if not os.path.exists(clt):os.mkdir(clt)articles = get_articles(clt)print(clt + ": 共" + str(len(articles)) + "个")for i in range(0, len(articles)):print("正在处理第 " + str(i) + " 个")title = articles[i]["title"]thumb_url = articles[i]["thumb_url"]content = articles[i]["content"]# 下载封面# path = os.path.join(clt, title)fname = str(i) + "_" + title.replace("|", "").replace("<", "").replace(">", "")download(clt, fname + get_suffix(thumb_url), thumb_url)# 找出文章中的图片imgs = find_images(content)index = 0for img in imgs:download(clt, fname + "_" + str(index) + get_suffix(img), img)index = index + 1# 找出文章中的敏感词invalids = check_jinyongci(content)print(invalids,'----',title)save_jinyongci(clt, title, invalids)

  附带极限词列表,进行过滤使用

最大程度、最高级、最高端、最奢侈、最低级、最便宜、史上最低价、最流行、最受欢迎、最先进科学、最新技术、最新科学中国第一、全网第一、销量第一、排名第一、第一品牌、NO.1、TOP1、独一无二、全国第一、最后一波、大品牌之一、销冠国家级、国际级、世界级、千万级、百万级、星级、5A、甲级、超甲级顶级、尖端、顶尖、顶级享受、完美、至尊、空前、绝后、绝版、非此莫属、巅峰、前所未有、完美、翘楚之作、不可再生、不可复制、绝无仅有、寸土寸金、淋漓尽致、无与伦比、唯一、卓越前无古人后无来者、绝版、珍稀、臻稀、稀少、绝无仅有、绝不在有、稀世珍宝、千金难求、世所罕见、不可多得、空前绝后、寥寥无几、屈指可数独家、独创、独据、开发者、缔造者、创始者、发明者首个、首选、独家、首发、首席、首府、首选、首屈一指、全国首家、国家领导人、国门、国宅、首次、填补国内空白、国际品质大牌、金牌、名牌、王牌、领先上市、巨星、著名、掌门人、至尊、冠军世界领先、领先、领导者、领袖、引领、创领、领航、耀领史无前例、前无古人、永久、万能、百分之百

  

转载于:https://www.cnblogs.com/68xi/p/9294332.html

Python爬取微信公众号素材库相关推荐

  1. html如何获取请求头变量的值。_如何使用 Python 爬取微信公众号文章

    我比较喜欢看公众号,有时遇到一个感兴趣的公众号时,都会感觉相逢恨晚,想一口气看完所有历史文章.但是微信的阅读体验挺不好的,看历史文章得一页页的往后翻,下一次再看时还得重复操作,很是麻烦. 于是便想着能 ...

  2. python 微信公众号发文章_如何使用 Python 爬取微信公众号文章

    我比较喜欢看公众号,有时遇到一个感兴趣的公众号时,都会感觉相逢恨晚,想一口气看完所有历史文章.但是微信的阅读体验挺不好的,看历史文章得一页页的往后翻,下一次再看时还得重复操作,很是麻烦. 于是便想着能 ...

  3. 如何用python爬取公众号文章_如何使用 Python 爬取微信公众号文章

    我比较喜欢看公众号,有时遇到一个感兴趣的公众号时,都会感觉相逢恨晚,想一口气看完所有历史文章.但是微信的阅读体验挺不好的,看历史文章得一页页的往后翻,下一次再看时还得重复操作,很是麻烦. 于是便想着能 ...

  4. python爬虫爬取微信_如何使用 Python 爬取微信公众号文章

    我比较喜欢看公众号,有时遇到一个感兴趣的公众号时,都会感觉相逢恨晚,想一口气看完所有历史文章.但是微信的阅读体验挺不好的,看历史文章得一页页的往后翻,下一次再看时还得重复操作,很是麻烦. 于是便想着能 ...

  5. 如何使用 Python 爬取微信公众号文章

    我比较喜欢看公众号,有时遇到一个感兴趣的公众号时,都会感觉相逢恨晚,想一口气看完所有历史文章.但是微信的阅读体验挺不好的,看历史文章得一页页的往后翻,下一次再看时还得重复操作,很是麻烦. 于是便想着能 ...

  6. python爬取微信公众号文章(包含文章内容和图片)

    之前虽然做过网页爬取,但微信爬取一直没做过,因为我一直不知道网页可以进微信公众平台,只用过微信客户端进微信公众号.既然可以通过网页进微信公众平台,那么爬取微信公众号文章就流程上就没太多难度了. 自己在 ...

  7. python爬取微信公众号_python使用webdriver爬取微信公众号

    本文实例为大家分享了python使用webdriver爬取微信公众号的具体代码,供大家参考,具体内容如下 # -*- coding: utf-8 -*- from selenium import we ...

  8. Python爬取微信公众号文章、点赞数

    代码还是热乎的,只要你细心一步步的慢慢调试,绝壁没问题 前期准备 订阅号: Python: Fiddler: 微信账号: 流程 使用用微信公众号生成cookie 使用Fiddler抓取微信公众号数据, ...

  9. python爬取微信公众号文章(携带cookie)

      哈喽,大家好呀,这里是滑稽研究所.本期我们想要爬取微信公众号的文章内容.首先你想要有自己的微信公众号来登录平台.在个人编辑发布文章的界面,我们能使用上方的超链接功能来搜索文章,可以按关键字,也可以 ...

最新文章

  1. jquery设置div高度和easyui的dialog动态的指定高度
  2. java语法基础(总结)
  3. Laravel POST请求API接口 使用validate表单验证返回欢迎页
  4. 10.3 在线学习与 MapReduce-机器学习笔记-斯坦福吴恩达教授
  5. Istio Pilot 源码分析(二)
  6. 今天的快乐从何而来的飞鸽传书
  7. SVO实时全局光照优化(里程碑MK2):Sparse Voxel Octree based Global Illumination (SVO GI)...
  8. python getopt_python 5种 statsPython中的getopt函数使用详解
  9. bottle框架学习(四)之模版进阶使用
  10. 面试之C#--垃圾回收器什么时候回收?
  11. ARGOX 力象 OS-214Plus 条码打印机 B/S 打印
  12. java实现分页打印功能_分页功能的java实现
  13. keras实现注意力机制
  14. 利用地球同步卫星在一个1Mbps的信道上发送长度为1000位的帧,该信道的传播延时为27Frames of 1000 bits are sent over a 1-Mbps channel using
  15. Element UI中Steps 步骤条description描述换行展示
  16. CUDA out of memory解决办法
  17. nginx(静态资源部署)linux版
  18. [电影]《指环王》新老三部曲完全赏析(意外之旅)
  19. 三、深度学习基础1(构成、模型)
  20. com.sec.android.ofviewer是什么,Android动画之萌萌哒蜡烛吹蜡烛动画

热门文章

  1. matlab将矩阵数据归一化到[0,255]
  2. 处理器架构——多发射处理器技术
  3. win10账号锁定计算机,win10如何设置账户锁定阈值
  4. 数据库连接池,几种开源的数据库连接池
  5. CentOS7.4安装教程
  6. mysql 优化总结
  7. 微机原理与接口技术复习笔记(1)——微型计算机概述
  8. 漫画绘制技法大放送(上)
  9. PWorld2016大会演讲PPT+访谈视频大合集,总有一款是你在找的!
  10. 乐队设备--功放的学习和使用