[Python] 纯文本查看 复制代码# -*- coding:utf-8 -*-

import os

import urllib.parse

from html.parser import HTMLParser

import requests

from bs4 import BeautifulSoup

from pymongo import MongoClient

class ContentHtmlParser(HTMLParser):

"""

过滤html标签

"""

def __init__(self):

HTMLParser.__init__(self)

self.text = ""

def handle_data(self, data):

self.text += data

def get_text(self):

return self.text

mongo_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:

continue

if lines.find("、") != -1:

for p in lines.split("、"):

words.append(p.replace("\n", ""))

else:

words.append(lines.replace("\n", ""))

return words

def 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 articles

def download(dir, file_name, url):

if not os.path.exists(dir):

os.mkdir(dir)

try:

resp = requests.get(url)

path = dir + "\\" + file_name

if os.path.exists(path):

return

with 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 imgs

def get_suffix(url):

try:

suffix = url[url.rindex("=") + 1:]

if suffix == "jpeg" or suffix == "other":

return ".jpg"

return "." + suffix

except:

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 invalids

def save_jinyongci(clt, title, invalids):

if len(invalids) == 0:

return

file = 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 = 0

for 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)

python公众号接口_用Python实现微信公众号API素材库图文消息抓取相关推荐

  1. python微信api_用Python实现微信公众号API素材库图文消息抓取

    [Python] 纯文本查看 复制代码# -*- coding:utf-8 -*- import os import urllib.parse from html.parser import HTML ...

  2. python证券交易数据接口_实战 | Python获取股票交易数据

    项目介绍 看到标题大家的第一反应估计是怎么用爬虫来获取股票交易数据,要获取股票交易数据确实是需要用爬虫没错,不过还有比使用爬虫更加方便的方式.今天要给大家介绍的是一个专门用来获取国内股票交易数据的开源 ...

  3. python连接sap接口_基于Python的SAP流程自动化

    财务的自我修养,微信公众号:机智出品(jizhjchupin),文中各类数据文件均可于公众号内下载 痛点:SAP部分业务操作重复化.规律化: 目标:无意义的重复无脑操作全部自动化: 工具:Python ...

  4. python生成json接口_基于python的Flask框架写json接口并且结合eolinker进行接口测试

    一.前言 很多时候为了提高软件开发的效率,后端程序人员理想状态下应该编写程序测试接口供前端程序人员进行测试,以便前端的开发.笔者由于项目的需求近期尝试学习了一下测试接口的编写,以Python+Flas ...

  5. python编写登录接口_使用python编写一个登录接口

    需求: 编写登录接口 输入用户名密码 认证成功后 显示欢迎信息 输入三次后锁定 用户信息文件 黑名单的文件 黑名单里检查,不让登录 用户名密码判定 流程图: 代码:#!/usr/bin/env pyt ...

  6. python短信接口_基于 python 的短信接口调用代码示例模板

    阅信短信验证码平台最近会从新梳理基于不同语言的短信接口调用代码示例,是为了迎合市面上现在流行的各个语言代码实现,也是为了能够更好的服务满足客户的不同层次的需求. 下面的代码是基于 python 的短信 ...

  7. python实现restful接口_使用python为mysql实现restful接口

    最近在做游戏服务分层的时候,一直想把mysql的访问独立成一个单独的服务DBGate,原因如下: 请求收拢到DBGate,可以使DBGate变为无状态的,方便横向扩展 当请求量或者存储量变大时,mys ...

  8. python 知乎接口_用Python寻找知乎最美小姐姐

    原标题:用Python寻找知乎最美小姐姐 导读:最近知乎老是给我推送两个问答,一个是「长得好看是种什么体验?」,另一个是「女朋友长得好看是怎样的体验?」. 所以,本文将讲解如何爬取知乎这两个问题的回答 ...

  9. python简单实践作业_【Python】:简单爬虫作业

    使用Python编写的图片爬虫作业: #coding=utf-8 import urllib import re def getPage(url): #urllib.urlopen(url[, dat ...

  10. 微信公众号 餐饮 前端源码_成都餐饮茶楼微信公众号开发方案

    在很多人的印象中,传统的餐饮茶楼就应该做好自己的生意,和互联网是拉不上关系的,甚至格格不入.但是就用独立思考的餐饮茶楼客户提出了新的思路,能不能用微信公众号来吸引用户,让微信公众号成为用户的入口,而不 ...

最新文章

  1. 老段mysql,老段视频汇总
  2. python面试题之“该死的for循环系列”(二)
  3. 什么?你还不了解JVM !
  4. chrome浏览器模拟手机 地理定位
  5. Linux kernel block device 的 submit_bio 都做了什么?
  6. ITK:使用Canny边缘检测过滤器检测边缘
  7. 《图书管理系统——java》
  8. python可视化界面编程 pycharm_pycharm开发一个简单界面和通用mvc模板(操作方法图解)...
  9. 三星q90r如何升级系统_看尚电视强制升级风行系统,如何安装第三方软件?
  10. 第67课 选择排序 例67.1 《小学生C++编程入门》
  11. 编程语言---自己常用网站整理
  12. [转]java 输出流转输入流
  13. vue props 多类型_一个TypeScript简例,以及Vue支持TS的一些些事儿
  14. 安卓项目连接后台服务器,android云后端服务器
  15. Android Error:Some file crunching failed, see logs for details
  16. k8s calico 插件错误:Number of node(s) with BGP peering established = 0 calico/node is not ready: BIRD is
  17. windows下Typhon程序使用Hudson完成自动编译
  18. Fiddler is trying to download some required files to continue with the installation之解决方法
  19. 为什么你需要学 Go?
  20. docker报错docker.service: Start request repeated too quickly.

热门文章

  1. 计算机中的CPU主频是单位,计算机CPU主频单位是MHz和GHz,他们之间怎么换算?
  2. java把行政区划放到一个节点树形中
  3. 【逻辑思考】有时评论比内容更精彩
  4. 本题要求你写个程序把给定的符号打印成沙漏的形状。例如给定17个“*”,要求按下列格式打印
  5. 磁盘管理看不到硬盘的恢复办法
  6. 已知树节点获取树的节点路径(js树节点路径)
  7. hackthissite 闯关日记
  8. win10 修改c盘用户文件夹名称
  9. 什么是全文搜索引擎?
  10. nice计算机英语,不错的英文,nice的三种意思?