python 下载PDF学习笔记

python下载PDF

前置步骤同普通下载txt等文件一致,在数据抓取后需要转为二进制字节流形式保存,写入也要用二进制写入到新的pdf文件。

示例1 利用io转二进制
import io
import requests
def download_pdf(save_path,pdf_name,pdf_url):
    send_headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36",
        "Connection": "keep-alive",
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
        "Accept-Language": "zh-CN,zh;q=0.8"}
    response = requests.get(pdf_url, headers=send_headers)
    bytes_io = io.BytesIO(response.content)#转二进制
    with open(save_path + "%s.PDF" % pdf_name, mode='wb') as f:
        f.write(bytes_io.getvalue())#写入
        print('%s.PDF,下载成功!' % (pdf_name))
示例2 利用response.iter_content()遍历response.content
def download_all_pdf_files(url, pdfs):
    print("The following pdf files are downloaded from", url)
    for index, pdf in enumerate(pdfs, 1):
        print("%d. %s" %(index, pdf))
        #stream
        response = requests.get(url + pdf , stream=True)
    try:
        new_pdf_file = str(index)+'. '+pdf
        with open(new_pdf_file, 'wb') as handle:
            for block in response.iter_content(1024):#response.iter_content()遍历response.content
                handle.write(block)
    except:
        print("writing pdf file %s failed." % new_pdf_file)

代码参考:

Python怎么下载链接里的PDF?Python如何下载PDF文件

【转】Python编程: 多个PDF文件合并以及网页上自动下载PDF文件

你可能感兴趣的:(pdf,python)