android 数据存储怎么保存图片_文章要保存为TXT文件,其中的图片要怎么办?Python帮你解决...
生活随笔
收集整理的這篇文章主要介紹了
android 数据存储怎么保存图片_文章要保存为TXT文件,其中的图片要怎么办?Python帮你解决...
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
用 python 爬取你喜歡的 CSDN 的原創文章,保存為TXT文件,不僅查看不方便,而且還無法保存文章中的代碼和圖片。
今天教你制作成 PDF 慢慢看。萬一作者的突然把號給刪了,也會保存備份。
本篇文章視頻案例教程的鏈接地址:
https://www.bilibili.com/video/BV1A54y1U78U/
知識點:
- requests
- css選擇器
第三方庫:
- requests
- parsel
- pdfkit
開發環境:
- 版 本:anaconda5.2.0(python3.6.5)
- 編輯器:pycharm
代碼如下:
1.導入工具
import pdfkit import requests import parsel2.請求網站
headers = {"Host": "blog.csdn.net","Referer": "https://blog.csdn.net/qq_41359265/article/details/102570971","User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36", }3.打印標簽字符串
html_str = """ <!doctype html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title> </head> <body> {article} </body> </html>4.用戶信息
cookie = {'Cookie': 'uuid_tt_dd=10_6143182820-1560085972444-562851; Hm_ct_6bcd52f51e9b3dce32bec4a3997715ac=6525*1*10_6143182820-1560085972444-562851!1788*1*PC_VC!5744*1*weixin_40327641; smidV2=20190402161159283d81caefd878407944f56385d88f5200c18151eb7b63ec0; UN=weixin_40327641; dc_session_id=10_1560780458204.785546; __yadk_uid=dJcgMxYLzl35t9gmGc6bEnRxWhpZGZjq; Hm_ct_26c6581897cb7113caba3941e5aa57b0=5744*1*weixin_40327641!6525*1*10_6143182820-1560085972444-562851; Hm_ct_e5ef47b9f471504959267fd614d579cd=6525*1*10_6143182820-1560085972444-562851!5744*1*weixin_40327641; Hm_ct_62052699443da77047734994abbaed1b=5744*1*weixin_40327641!6525*1*10_6143182820-1560085972444-562851; Hm_lvt_62052699443da77047734994abbaed1b=1568382389,1568384316; Hm_lvt_26c6581897cb7113caba3941e5aa57b0=1567222806,1569331239; Hm_lvt_e5ef47b9f471504959267fd614d579cd=1569495260,1570722031; UserName=weixin_40327641; UserInfo=5efb72806ec7429fb885f8cf12233b54; UserToken=5efb72806ec7429fb885f8cf12233b54; UserNick=%E5%A1%AB%E5%9D%91%E5%B0%8F%E6%87%B5%E9%80%BC; AU=DA1; BT=1570886763298; p_uid=U000000; notice=1; Hm_lvt_85a6e71063e38ed893de1d8b6a71f5fe=1570889956; Hm_ct_85a6e71063e38ed893de1d8b6a71f5fe=5744*1*weixin_40327641!6525*1*10_6143182820-1560085972444-562851; acw_tc=2760823a15710394714692918e17ecbdca6dba528441074c2c8e1ad8ebea5e; announcement=%257B%2522announcementUrl%2522%253A%2522https%253A%252F%252Fblogdev.blog.csdn.net%252Farticle%252Fdetails%252F102605809%2522%252C%2522announcementCount%2522%253A1%252C%2522announcementExpire%2522%253A535744931%257D; firstDie=1; Hm_lvt_6bcd52f51e9b3dce32bec4a3997715ac=1571375632,1571376263,1571474096,1571481979; Hm_lvt_3fc28b5205f6aa5f3b16547ffddad367=1571481982; remove=true; Hm_lpvt_3fc28b5205f6aa5f3b16547ffddad367=1571481988; Hm_ct_3fc28b5205f6aa5f3b16547ffddad367=5744*1*weixin_40327641!6525*1*10_6143182820-1560085972444-562851; acw_sc__v2=5dab061ff4d5b7f68cb6b4fdff578b2c8e4b0add; dc_tos=pzmgx6; Hm_lpvt_6bcd52f51e9b3dce32bec4a3997715ac=1571489323' }5.爬取文章數據,轉化為PDF格式
def get_html(url):# 發送一個請求(網址)# 響應體response = requests.get(url, headers=headers, cookies=cookie)# text 文本(字符串)# 遭遇了反扒# print(response.text)"""如何把 HTML 變成 PDF 格式"""# 提取文章部分sel = parsel.Selector(response.text)# css 選擇器article = sel.css('article').get()title = sel.css('h1::text').get()print(title)print(article)html = html_str.format(article=article)with open(f'{title}.html', mode='w', encoding='utf-8') as f:f.write(html)# exe 文件存放的路徑config = pdfkit.configuration(wkhtmltopdf='C:Program Fileswkhtmltopdfbinwkhtmltopdf.exe')# 把 html 通過 pdfkit 變成 pdf 文件pdfkit.from_file(f'{title}.html', f'{title}.pdf', configuration=config)get_html('https://blog.csdn.net/nosprings/article/details/102609296')運行代碼:
總結
以上是生活随笔為你收集整理的android 数据存储怎么保存图片_文章要保存为TXT文件,其中的图片要怎么办?Python帮你解决...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python3中map函数_解决Pyth
- 下一篇: gpg加密命令 linux_加密方案 G