python微信公众号文章转pdf
从doxc中提取链接,转换pdf

https://www.bbsmax.com/A/Ae5RRb7m5Q/

import os
import zipfileimport docx
import requests
import pdfkit
import pypandoc
import re
import json
import base64# import wechatsogou
import wechatsogou
from IPython.lib.deepreload import reload
from fpdf import FPDF
from bs4 import BeautifulSoup
from docx import Document
from os.path import basename
from docx.opc.constants import RELATIONSHIP_TYPE as RT
from cachelib import SimpleCache
from bs4 import BeautifulSoupdef get_linked_text(soup):links = []# This kind of link has a corresponding URL in the _rel file.for tag in soup.find_all("hyperlink"):# try/except because some hyperlinks have no id.try:links.append({"id": tag["r:id"], "text": tag.text})except:pass# This kind does not.for tag in soup.find_all("instrText"):# They're identified by the word HYPERLINKif "HYPERLINK" in tag.text:# Get the URL. Probably.url = tag.text.split('"')[1]# The actual linked text is stored nearby tags.# Loop through the siblings starting here.temp = tag.parent.next_siblingtext = ""while temp is not None:# Text comes in <t> tags.maybe_text = temp.find("t")if maybe_text is not None:# Ones that have text in them.if maybe_text.text.strip() != "":text += maybe_text.text.strip()# Links end with <w:fldChar w:fldCharType="end" />.maybe_end = temp.find("fldChar[w:fldCharType]")if maybe_end is not None:if maybe_end["w:fldCharType"] == "end":breaktemp = temp.next_siblinglinks.append({"id": None, "href": url, "text": text})return linksif __name__ == '__main__':ws_api = wechatsogou.WechatSogouAPI(captcha_break_time=3)i=0j=0file_name="1公告消息.docx"archive = zipfile.ZipFile(file_name, "r")file_data = archive.read("word/document.xml")doc_soup = BeautifulSoup(file_data, "xml")linked_text = get_linked_text(doc_soup)# print(linked_text)a = [0]*len(linked_text)for url in linked_text:a[i]=url['href']i=i+1# print(a)path_wkthmltopdf = r'D:\\soft\\wkhtmltopdf\\bin\\wkhtmltopdf.exe'config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)for b in a:# pdfkit.from_url(b, str(j)+'.pdf')# document = docx.Document()# document.add_run("")# html = requests.get(b).content# soup = BeautifulSoup(html, 'html.parser')# pdfkit.from_url(b,str(1)+'.pdf',configuration=config)# print(b)# break# pdfkit.from_file(b,str(1)+'.pdf',configuration=config)# targetPath = os.getcwd() + os.path.sep +str(j)try:content_info = ws_api.get_article_content(b)except:print("ValueError")html = f'''<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>{j}</title></head><body><h2 style="text-align: center;font-weight: 400;">{j}</h2>{content_info['content_html']}</body></html>'''pdfkit.from_string(html,'D:/a/'+str(j)+'.pdf',configuration=config)# pypandoc.convert_file(b, 'docx', str(1)+'.docx')# print(b)j=j+1# print(len(a))

python微信公众号文章转pdf相关推荐

  1. PHP 公众号文章 转 pdf,如何将微信公众号文章另存为pdf文件 微信公众号文章另存为pdf文件的方法...

    如何将微信公众号文章另存为pdf文件?现在很多人都有自己的微信公众号,会在上面写一些文章.很多用户说想要将微信公众号文章另存为pdf文件,但是却不知道怎么操作.下面就是微信公众号文章另存为pdf文件的 ...

  2. python微信爬取教程_[python]微信公众号文章爬取

    [python]微信公众号文章爬取 需求 爬取一些微信公众号的文章 数据来源 1.搜狗微信搜索,可以搜索微信公众号文章,但只能显示该公众号最近十篇的文章 2.通过个人微信公众号中的素材管理,查看其他微 ...

  3. 微信公众号文章转pdf下载,不难也不太容易,磕磕绊绊倒是不少如何用xpath保存网站源码;如何精简你的文章请求链接;如何将文章转化为pdf文件,不乱码,不报错

    目录 敲黑板 抓包分析 看碟下菜,确认分析思路 锁定有效数据包 分析响应信息和请求连接 黑板报 如何用xpath保存网站源码 如何精简你的文章请求链接 如何将文章转化为pdf文件,不乱码,不报错 总结 ...

  4. Python 微信公众号的文章爬取

    Python 微信公众号文章爬取 一.思路 二.接口分析 三.实现 第一步: 第二步: 1.请求获取对应公众号接口,取到我们需要的fakeid 2.请求获取微信公众号文章接口,取到我们需要的文章数据 ...

  5. python爬取正确但不出文件_使用Python爬取微信公众号文章并保存为PDF文件(解决图片不显示的问题)...

    前言 第一次写博客,主要内容是爬取微信公众号的文章,将文章以PDF格式保存在本地. 爬取微信公众号文章(使用wechatsogou) 1.安装 pip install wechatsogou --up ...

  6. python write非法字符报错_Python爬虫实现的微信公众号文章下载器

    平时爱逛知乎,收藏了不少别人推荐的数据分析.机器学习相关的微信公众号(这里就不列举了,以免硬广嫌疑).但是在手机微信上一页页的翻阅历史文章浏览,很不方便,电脑端微信也不方便. 所以我就想有什么方法能否 ...

  7. python下载微信公众号文章_python如何导出微信公众号文章

    [相关学习推荐:python教程] 1.安装wkhtmltopdf 下载地址:https://wkhtmltopdf.org/downloads.html 我测试用的是windows的,下载安装后结果 ...

  8. python下载微信公众号文章_python如何导出微信公众号文章方法详解

    1.安装wkhtmltopdf 下载地址:https://wkhtmltopdf.org/downloads.html 我测试用的是windows的,下载安装后结果如下 2 编写python 代码导出 ...

  9. python公众号文章_Python 抓取微信公众号文章

    起因是刷微信的时候看到一篇文章,Python 抓取微信公众号文章保存成pdf,很容易搜到,就不贴出来了 先用chrome登陆微信公众号后台,先获取一下自己的cookie,复制下来就行,解析一下转换成 ...

最新文章

  1. Log4j日志管理的用法
  2. Android APP全面屏适配技术要点
  3. 生成全1矩阵_全矩阵捕获技术:一种最大限度提升便携式相控阵超声检测设备功能的新技术...
  4. 基于SQL on Hadoop的数据仓库技术
  5. 零基础学python
  6. 英宝通4.0公开课---致力于提供最新的Unity3D技术
  7. servlet 和 struts2 同时使用 以及 使用struts2标签库时报错
  8. mysql整理_MySQL 日常整理
  9. 【Linux】Ubuntu下C语言访问MySQL数据库入门
  10. spring boot 教程(五)使用JdbcTemplate访问数据库
  11. Python元组 ()
  12. 为Mac任意命令设置快捷键
  13. dsh linux,通过dsh批量管理Linux服务器(一)【感谢作者的无私分享】
  14. caffe---测试模型分类结果并输出(python )
  15. 通过一个简单例子看懂遗传算法,附MATLAB代码
  16. 算法设计与分析期末复习
  17. tssd加载lisp_几个有用的CAD的加载程序LISP
  18. 坚果pro2刷原生android,坚果Pro2普通版(U3 Pro)魔趣OS 安卓10 纯净完美 原生极简 纯净推荐...
  19. openssl源代码方式安装以及简单的实验
  20. vue3 接入网易易盾(webpack,vite)

热门文章

  1. h5活动是什么意思_H5活动页能给你带来什么?
  2. 【小程序开发之微信登录】
  3. 微信公众号机器人详细教程
  4. 电平转换小结(避免翻车指南)
  5. Re: 客户协作 OVER 合同谈判
  6. 敏捷软件开发实践-客户合作胜过合同谈判
  7. (附源码)音乐播放小程序 毕业设计 031306
  8. 腾讯云服务器安装win10
  9. 如果iis的配置文件 applicationHost.config坏掉了, 会在 C:\inetpub\history\ 中存储历史备份。复制过去还原就可以了-摘自网络...
  10. NLTK语料库nltk.download()安装失败及下载很慢的解决方法