您好,登錄后才能下訂單哦!
本篇內容介紹了“Python中文章保存為TXT文件圖片要怎么解決”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
第三方庫:
requests
parsel
pdfkit
開發環境:
版 本:anaconda5.2.0(python3.6.5)
編輯器:pycharm
1.導入工具
import pdfkit import requests import parsel
2.請求網站
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('h2::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 Files\\wkhtmltopdf\\bin\\wkhtmltopdf.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')
運行代碼:
“Python中文章保存為TXT文件圖片要怎么解決”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。