Python中文章保存为TXT文件图片要怎么解决
本篇内容介绍了“Python中文章保存为TXT文件图片要怎么解决”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
创新互联成立与2013年,先为盐亭等服务建站,盐亭等地企业,进行企业商务咨询服务。为盐亭企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
第三方库:
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 = """Document {article}
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文件图片要怎么解决”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!
新闻名称:Python中文章保存为TXT文件图片要怎么解决
本文URL:http://scyanting.com/article/gdjeoh.html