Python爬取CSDN文章并制作成PDF

  1. requests

  2. css选择器

第三方库:

  1. requests

  2. parsel

  3. pdfkit

开发环境:

  1. 版 本:anaconda5.2.0(python3.6.5)

  2. 编辑器:pycharm

没有最好的编辑器,只有最合适的编辑器

下载 csdn 文章


万一作者的突然脑抽把号给删了,也会保存备份。那岂不泪流满面

首先需要获取 HTML 文件。

pdfkit


pdfkit是将html转换为pdf格式文档的python库。

安装wkhtmltopdf

下载:https://wkhtmltopdf.org/downloads.html

制作 PDF 的三种方法

form_url:传入的参数为url

def from_url(url, output_path, options=None, toc=None,cover=None,configuration=None, cover_first=False)

form_file:传入的参数为html文件

def from_file(input, output_path, options=None, toc=None, cover=None, css=None,configuration=None, cover_first=False)

form_string:传入的参数为字符串

def from_string(input, output_path, options=None, toc=None, cover=None, css=None,configuration=None, cover_first=False)

Configuration:Configuration中传入了wkhtmltopdf参数

https://blog.csdn.net/xc_zhou/article/details/80952168

指定pdf的格式

我们可以指定各种选项,就是上面三个方法中的options。

具体的设置可以参考https://wkhtmltopdf.org/usage/wkhtmltopdf.txt 里面的内容。

我们这里只举个栗子:

options = {

‘page-size’: ‘Letter’,

‘margin-top’: ‘0.75in’,

‘margin-right’: ‘0.75in’,

‘margin-bottom’: ‘0.75in’,

‘margin-left’: ‘0.75in’,

‘encoding’: “UTF-8”,

‘custom-header’ : [

(‘Accept-Encoding’, ‘gzip’)

]

‘cookie’: [

(‘cookie-name1’, ‘cookie-value1’),

(‘cookie-name2’, ‘cookie-value2’),

],

‘no-outline’: None

}

pdfkit.from_url(‘http://google.com’, ‘out.pdf’, options=options)

完整代码


selenium_page.py

import re

import requests

def get_songid():

“”“获取音乐的songid”“”

url = ‘http://music.taihe.com/artist/2517’

response = requests.get(url=url)

html = response.text

sids = re.findall(r’href=“/song/(\d+)”', html)

return sids

def get_music_url(songid):

“”“获取下载链接”“”

api_url = f’http://musicapi.taihe.com/v1/restserver/ting?method=baidu.ting.song.playAAC&format=jsonp&songid={songid}&from=web’

response = requests.get(api_url.format(songid=songid))

data = response.json()

print(data)

try:

music_name = data[‘songinfo’][‘title’]

music_url = data[‘bitrate’][‘file_link’]

return music_name, music_url

except Exception as e:

print(e)

def download_music(music_name, music_url):

“”“下载音乐”“”

response = requests.get(music_url)

content = response.content

save_file(music_name+‘.mp3’, content)

def save_file(filename, content):

“”“保存音乐”“”

with open(file=filename, mode=“wb”) as f:

f.write(content)

if name == “main”:

for song_id in get_songid():

music_name, music_url = get_music_url(song_id)

download_music(music_name, music_url)

现在能在网上找到很多很多的学习资源,有免费的也有收费的,当我拿到1套比较全的学习资源之前,我并没着急去看第1节,我而是去审视这套资源是否值得学习,有时候也会去问一些学长的意见,如果可以之后,我会对这套学习资源做1个学习计划,我的学习计划主要包括规划图和学习进度表。

分享给大家这份我薅到的免费视频资料,质量还不错,大家可以跟着学习

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里无偿获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值