用python将网上的文章转存为PDF文档,保存电脑上慢慢看

import requests
import parsel
import pdfkit
import os
import re

html_str = """



    
    Document


{article}


"""

def change_title(title):
    """
    python学习交流群:279199867
    替换标题中的特殊字符
    :param title: 传入文章标题
    :return: 返回一个替换掉特殊字符的标题
    """
    """
    使用re.compile()将正则表达式的字符串形式编译为一个对象,通过该对象提供的一些列方法对文本
    进行匹配查找
    re.sub() 第一个参数对应的正则表达式,第二个参数为要替换成的字符串, 第三个参数为源字符串
    """
    pattern = re.compile(r"[\/\\\:\*\?\"\\|]")  # '/ \ : * ? " < > |'
    new_title = re.sub(pattern, "_", title)  # 替换为下划线
    return new_title

for page in range(1, 11):
    """
    发送请求的url地址,唯一资源定位符
    headers: 请求头 把python伪装成浏览器对服务器发送请求, 然后服务器会给我们返回一个响应数据
        请求头所加的参数都是可以在开发者工具中的headers里面的request headers中找到的
        比如 user-agent:代表着浏览器的信息
            cookies:用户的信息 常用于检测是否有登陆账号
            host:域名
            referer:常说的防盗链,告诉服务器是从哪个网页跳转过来的
    请求方式:可以通过开发者工具中headers里面的数据看到是什么样的请求方式
        get请求: 是可以直接从服务器上面获取数据
        post请求:需要向服务器发送一个数据 比如说(搜索/登陆)
    response:响应对象
    状态码: 200表示请求成功 300:重定向 跳转 400:通常是url网址不对 500 一般是服务器问题
    获取网页文本数据 response.text 获取网页json字典数据 response.json() 获取网页二进制数据 response.content
    """
    url = 'https://blog.csdn.net/qdPython/article/list/{page}'
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'
    }
    response = requests.get(url=url, headers=headers)
    """
    把 html 字符串数据转换成一个 Selector 对象
    Selector 就具有一系列数据解析的方法  css/xpath/re
    类选择器 都是使用圆点.开头
    ID选择器 是使用#开头
    属性选择器:
        ::text获取标签里面的文本数据
        ::attr(xxx) 获取标签内某一个属性的数据
        get() 从 Selector 对象中提取第一个数据, 直接返回字符串数据给我们
        getall() 从 Selector 对象中提取提取所有数据, 返回一个列表
    """
    selector = parsel.Selector(response.text)
    href = selector.css('.article-list div.article-item-box a::attr(href)').getall()
    for link in href:
        response_1 = requests.get(url=link, headers=headers)
        selector_1 = parsel.Selector(response_1.text)
        title = selector_1.css('#articleContentId::text').get()
        content = selector_1.css('#content_views').get()
        new_title = change_title(title)
        # 创建文件保存地址以及保存文件的名字 和格式
        pdf_path = 'pdf\\' + new_title + '.pdf'
        html_path = 'pdf\\' + new_title + '.html'
        # str.format() 字符串格式化方法
        html = html_str.format(article=content)
        """
        with open   打开文件时, 当文件对象引用完毕之后会自动关闭文件
        html_path:文件保存路径以及名字格式
        mode:保存方式 w 写入 如果你不写mode默认是r 读
        encoding: 编码
        as f 重命名 可以自定义
        f = open()
        f.writer()
        f.close()
        """
        with open(html_path, mode='w', encoding='utf-8') as f:
            f.write(html)
            print('正在保存:', title)
        # exe 文件存放的路径
        config = pdfkit.configuration(wkhtmltopdf='C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe')
        # 把 html 通过 pdfkit 变成 pdf 文件
        pdfkit.from_file(html_path, pdf_path, configuration=config)
        os.remove(html_path)

Original: https://www.cnblogs.com/hahaa/p/16309447.html
Author: 轻松学Python
Title: 用python将网上的文章转存为PDF文档,保存电脑上慢慢看

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/499497/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球