【python爬虫】爬取CSDN个页人主文章输出为htlm、pdf格式

代码运行:
在这里插入图片描述

pdf展示
在这里插入图片描述

需要下载wkhtmltopdf,html转换为pdf
wkhtmltopdf下载网址:https://wkhtmltopdf.org/downloads.html

代码:csdn.py

"""
step1:爬取博主的所有博文的article_ids
step2:根据article_ids,爬取这篇文章的html,拿到想要的部分
step3:保存为html格式,再保存pdf格式
"""
import os
import random
import time
import requests
from lxml import etree
import pdfkit
config = pdfkit.configuration(wkhtmltopdf=r"D:\wkhtmltopdf\bin\wkhtmltopdf.exe")

author_name = input("请输入博主ID:")
MAX_PAGE_NUM = 200
i = 1

sess = requests.session()
agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36'
sess.headers['User-Agent'] = agent

def crawler_blog_by(author_name,article_id,title):
    article_request_url = f'https://blog.csdn.net/{author_name}/article/details/{article_id}'
    response = sess.get(article_request_url)

    selector = etree.HTML(response.text)
    head_msg = selector.xpath(r"//head")[0]
    head_str = etree.tostring(head_msg,encoding='utf8',method='html').decode()
    body_msg = selector.xpath(r"//div[@id='content_views']")[0]
    body_str = etree.tostring(body_msg, encoding='utf8', method='html').decode()

    if not os.path.exists('c_articles'):
        os.mkdir('c_articles')

    title = title.replace("/","-").replace(":","")
    save_file_name = os.path.join('c_articles',f'{author_name}-{title}-{article_id}.html')
    with open(save_file_name,'w',encoding='utf8') as writer:
        writer.write(f"""<head><meta charset="utf8"></head>
                    {body_str}""")
        html_to_pdf(save_file_name)
        global i
        print(f'【INFO】:{author_name}{i}篇博文{title}-{article_id}.html保存文件成功')
        i += 1

def html_to_pdf(file_html_name):
    pre_file_name = os.path.splitext(file_html_name)[0]
    pdfkit.from_file(file_html_name,pre_file_name+'.pdf',configuration=config)

#循环爬取分页html

for page_no in range(MAX_PAGE_NUM):
    try:
        data = {"page": page_no,
                "size": 20,
                "businessType": "blog",
                "orderby": "",
                "noMore": False,
                "year": "",
                "month": "",
                "username": author_name}
        pages_dict = sess.get('https://blog.csdn.net/community/home-api/v1/get-business-list',
                              params=data).json()
        for article in pages_dict['data']['list']:
            article_id = article['articleId']
            title = article['title']
            crawler_blog_by(author_name,article_id,title)

        time.sleep(random.uniform(0.4,1.0))
    except Exception as e:
        print(e)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zz的学习笔记本

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值