【相關(guān)學(xué)習(xí)推薦:微信公眾號開發(fā)教程】
前言
第一次寫博客,主要內(nèi)容是爬取微信公眾號的文章,將文章以PDF格式保存在本地。
爬取微信公眾號文章(使用wechatsogou)
1.安裝
pip install wechatsogou --upgrade
wechatsogou是一個基于搜狗微信搜索的微信公眾號爬蟲接口
2.使用方法
使用方法如下所示
import wechatsogou # captcha_break_time為驗證碼輸入錯誤的重試次數(shù),默認(rèn)為1 ws_api = wechatsogou.WechatSogouAPI(captcha_break_time=3) # 公眾號名稱 gzh_name = '' # 將該公眾號最近10篇文章信息以字典形式返回 data = ws_api.get_gzh_article_by_history(gzh_name)
data數(shù)據(jù)結(jié)構(gòu):
{ 'gzh': { 'wechat_name': '', # 名稱 'wechat_id': '', # 微信id 'introduction': '', # 簡介 'authentication': '', # 認(rèn)證 'headimage': '' # 頭像 }, 'article': [ { 'send_id': int, # 群發(fā)id,注意不唯一,因為同一次群發(fā)多個消息,而群發(fā)id一致 'datetime': int, # 群發(fā)datatime 10位時間戳 'type': '', # 消息類型,均是49(在手機端歷史消息頁有其他類型,網(wǎng)頁端最近10條消息頁只有49),表示圖文 'main': int, # 是否是一次群發(fā)的第一次消息 1 or 0 'title': '', # 文章標(biāo)題 'abstract': '', # 摘要 'fileid': int, # 'content_url': '', # 文章鏈接 'source_url': '', # 閱讀原文的鏈接 'cover': '', # 封面圖 'author': '', # 作者 'copyright_stat': int, # 文章類型,例如:原創(chuàng)啊 }, ... ] }
這里需要得到兩個信息:文章標(biāo)題,文章url。
得到文章url以后,就可以根據(jù)url將html頁面轉(zhuǎn)換成pdf文件了。
生成PDF文件
1.安裝wkhtmltopdf
下載地址:https://wkhtmltopdf.org/downloads.html
2.安裝pdfkit
pip install pdfkit
3.使用方法
import pdfkit # 根據(jù)url生成pdf pdfkit.from_url('http://baidu.com','out.pdf') # 根據(jù)html文件生成pdf pdfkit.from_file('test.html','out.pdf') # 根據(jù)html代碼生成pdf pdfkit.from_string('Hello!','out.pdf')
如果直接用上面得到的文章url去生成pdf,會出現(xiàn)pdf文件不顯示文章圖片的問題。
解決辦法:
# 該方法根據(jù)文章url對html進行處理,使圖片顯示 content_info = ws_api.get_article_content(url) # 得到html代碼(代碼不完整,需要加入head、body等標(biāo)簽) html_code = content_info['content_html']
然后根據(jù)html_code構(gòu)造完整的html代碼,調(diào)用pdfkit.from_string()
方法生成pdf文件,這時候會發(fā)現(xiàn)文章中的圖片在pdf文件中顯示出來了。
完整代碼
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: 文章標(biāo)題 :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: # 部分文章標(biāo)題含特殊字符,不能作為文件名 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 # 如果不存在目標(biāo)文件夾就進行創(chuàng)建 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)
相關(guān)學(xué)習(xí)推薦:python教程