爬取微信公眾號文章並儲存為PDF檔案(Python方法)
【相關學習推薦:#微信公眾號開發教學】
#前言
第一次寫博客,主要內容是爬取微信公眾號的文章,將文章以PDF格式保存在本地。
爬取微信公眾號文章(使用wechatsogou)
1.安裝
pip install wechatsogou --upgrade
登入後複製
wechatsogou是一個基於搜狗微信搜尋的微信公眾號爬蟲介面
2.使用方法
使用方法如下所示
import wechatsogou # captcha_break_time为验证码输入错误的重试次数,默认为1 ws_api = wechatsogou.WechatSogouAPI(captcha_break_time=3) # 公众号名称 gzh_name = '' # 将该公众号最近10篇文章信息以字典形式返回 data = ws_api.get_gzh_article_by_history(gzh_name)
登入後複製
data資料結構:
{ 'gzh': { 'wechat_name': '', # 名称 'wechat_id': '', # 微信id 'introduction': '', # 简介 'authentication': '', # 认证 'headimage': '' # 头像 }, 'article': [ { 'send_id': int, # 群发id,注意不唯一,因为同一次群发多个消息,而群发id一致 'datetime': int, # 群发datatime 10位时间戳 'type': '', # 消息类型,均是49(在手机端历史消息页有其他类型,网页端最近10条消息页只有49),表示图文 'main': int, # 是否是一次群发的第一次消息 1 or 0 'title': '', # 文章标题 'abstract': '', # 摘要 'fileid': int, # 'content_url': '', # 文章链接 'source_url': '', # 阅读原文的链接 'cover': '', # 封面图 'author': '', # 作者 'copyright_stat': int, # 文章类型,例如:原创啊 }, ... ] }
登入後複製
這裡需要得到兩個資訊:文章標題,文章url。
得到文章url以後,就可以依照url將html頁面轉換成pdf檔了。
產生PDF檔案
1.安裝wkhtmltopdf
下載位址:https://wkhtmltopdf.org/downloads.html
2.安裝pdfkit
pip install pdfkit
登入後複製
3.使用方法
import pdfkit # 根据url生成pdf pdfkit.from_url('http://baidu.com','out.pdf') # 根据html文件生成pdf pdfkit.from_file('test.html','out.pdf') # 根据html代码生成pdf pdfkit.from_string('Hello!','out.pdf')
登入後複製
如果直接用上面得到的文章url去產生pdf,會出現pdf檔不顯示文章圖片的問題。
解決方法:<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'># 该方法根据文章url对html进行处理,使图片显示
content_info = ws_api.get_article_content(url)
# 得到html代码(代码不完整,需要加入head、body等标签)
html_code = content_info[&#39;content_html&#39;]</pre><div class="contentsignin">登入後複製</div></div>
然後根據html_code建構完整的html程式碼,呼叫
方法產生pdf文件,這時候會發現文章中的圖片在pdf檔中顯示出來了。
完整程式碼相關學習推薦:python教學######import os import pdfkit import datetime import wechatsogou # 初始化API ws_api = wechatsogou.WechatSogouAPI(captcha_break_time=3) def url2pdf(url, title, targetPath): ''' 使用pdfkit生成pdf文件 :param url: 文章url :param title: 文章标题 :param targetPath: 存储pdf文件的路径 ''' try: content_info = ws_api.get_article_content(url) except: return False # 处理后的html html = f''' <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>{title}</title> </head> <body> <h2 style="text-align: center;font-weight: 400;">{title}</h2> {content_info['content_html']} </body> </html> ''' try: pdfkit.from_string(html, targetPath + os.path.sep + f'{title}.pdf') except: # 部分文章标题含特殊字符,不能作为文件名 filename = datetime.datetime.now().strftime('%Y%m%d%H%M%S') + '.pdf' pdfkit.from_string(html, targetPath + os.path.sep + filename) if __name__ == '__main__': # 此处为要爬取公众号的名称 gzh_name = '' targetPath = os.getcwd() + os.path.sep + gzh_name # 如果不存在目标文件夹就进行创建 if not os.path.exists(targetPath): os.makedirs(targetPath) # 将该公众号最近10篇文章信息以字典形式返回 data = ws_api.get_gzh_article_by_history(gzh_name) article_list = data['article'] for article in article_list: url = article['content_url'] title = article['title'] url2pdf(url, title, targetPath)登入後複製以上是爬取微信公眾號文章並儲存為PDF檔案(Python方法)的詳細內容。更多資訊請關注PHP中文網其他相關文章!
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章
<🎜>:種植花園 - 完整的突變指南
3 週前
By DDD
<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
如何修復KB5055612無法在Windows 10中安裝?
3 週前
By DDD
北端:融合系統,解釋
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前
By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)