前言

用 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 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('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 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')

 

运行代码:

内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/hhh188764/p/13471752.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!