具体的设置可以参考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)
CSDN.py
-- coding=utf-8 --
import pdfkit
import parsel
import requests
html_template = “”"
{content}
“”"
headers = {
“User-Agent”: “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36”,
‘Host’: ‘blog.csdn.net’,
‘Referer’: ‘https://blog.csdn.net’,
}
def get_csdn_cookie():
response = requests.get(‘https://www.csdn.net/’, headers=headers)
return response.cookies
def get_html(url):
“”“获取索引页”“”
response = requests.get(url, headers=headers)
sel = parsel.Selector(response.text)
list_a = sel.css(‘.article-list a’)
for i in list_a[2:]:
article_index = i.css(‘a::attr(href)’).get()
yield article_index
def csdn(url: str, cookie=get_csdn_cookie()):
“”“下载 CSDN 文章html”“”
response = requests.get(url, headers=headers, cookies=cookie)
获取文章标题内容
sel = parsel.Selector(response.text)
print(response.text)
title = sel.css(‘.title-article::text’).get()
article = sel.css(‘article’).get()
return title, article
def html_to_pdf(filename_html, filename_pdf):
“”“HTML 2 PDF”“”
config = pdfkit.configuration(wkhtmltopdf=‘C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe’)
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’: [
最后
不知道你们用的什么环境,我一般都是用的Python3.6环境和pycharm解释器,没有软件,或者没有资料,没人解答问题,都可以免费领取(包括今天的代码),过几天我还会做个视频教程出来,有需要也可以领取~
给大家准备的学习资料包括但不限于:
Python 环境、pycharm编辑器/永久激活/翻译插件
python 零基础视频教程
Python 界面开发实战教程
Python 爬虫实战教程
Python 数据分析实战教程
python 游戏开发实战教程
Python 电子书100本
Python 学习路线规划
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!