Python爬虫入门教程24:下载某网站付费文档保存PDF
前言💨
本文的文字及圖片來(lái)源于網(wǎng)絡(luò),僅供學(xué)習(xí)、交流使用,不具有任何商業(yè)用途,如有問(wèn)題請(qǐng)及時(shí)聯(lián)系我們以作處理。
前文內(nèi)容💨
Python爬蟲入門教程01:豆瓣Top電影爬取
Python爬蟲入門教程02:小說(shuō)爬取
Python爬蟲入門教程03:二手房數(shù)據(jù)爬取
Python爬蟲入門教程04:招聘信息爬取
Python爬蟲入門教程05:B站視頻彈幕的爬取
Python爬蟲入門教程06:爬取數(shù)據(jù)后的詞云圖制作
Python爬蟲入門教程07:騰訊視頻彈幕爬取
Python爬蟲入門教程08:爬取csdn文章保存成PDF
Python爬蟲入門教程09:多線程爬取表情包圖片
Python爬蟲入門教程10:彼岸壁紙爬取
Python爬蟲入門教程11:新版王者榮耀皮膚圖片的爬取
Python爬蟲入門教程12:英雄聯(lián)盟皮膚圖片的爬取
Python爬蟲入門教程13:高質(zhì)量電腦桌面壁紙爬取
Python爬蟲入門教程14:有聲書音頻爬取
Python爬蟲入門教程15:音樂(lè)網(wǎng)站數(shù)據(jù)的爬取
Python爬蟲入門教程17:音樂(lè)歌曲的爬取
Python爬蟲入門教程18:好看視頻的爬取
Python爬取入門教程19:YY短視頻的爬取
Python爬蟲入門教程20:IP代理的爬取使用
Python爬蟲入門教程21:付費(fèi)文檔的爬取
Python爬蟲入門教程22:百度翻譯JS解密
Python爬蟲入門教程23:A站視頻的爬取,解密m3u8視頻格式
PS:如有需要 Python學(xué)習(xí)資料 以及 解答 的小伙伴可以加點(diǎn)擊下方鏈接自行獲取
python免費(fèi)學(xué)習(xí)資料以及群交流解答點(diǎn)擊即可加入
基本開(kāi)發(fā)環(huán)境💨
- Python 3.6
- Pycharm
相關(guān)模塊的使用💨
import requests import parsel import re import os import pdfkit安裝Python并添加到環(huán)境變量,pip安裝需要的相關(guān)模塊即可。
需要使用到一個(gè)軟件 wkhtmltopdf 這個(gè)軟件的作用就是把html文件轉(zhuǎn)成PDF
(軟件可以點(diǎn)擊上方鏈接在學(xué)習(xí)交流群中即可獲取)
想要把文檔內(nèi)容保存成PDF, 首先保存成html文件, 然后把html文件轉(zhuǎn)PDF
💥需求數(shù)據(jù)來(lái)源分析
寫爬蟲程序,對(duì)于數(shù)據(jù)來(lái)源的分析,是比較重要的,因?yàn)橹挥挟?dāng)你知道數(shù)據(jù)的來(lái)源你才能通過(guò)代碼去實(shí)現(xiàn)
網(wǎng)站分類有比較多種, 也可以選擇自己要爬取的。
這個(gè)網(wǎng)站如果你只是正常直接去復(fù)制文章內(nèi)容的話,會(huì)直接彈出需要費(fèi)的窗口…
但是這個(gè)網(wǎng)站上面的數(shù)據(jù)內(nèi)容又非常好找, 因?yàn)榫W(wǎng)站本身僅僅只是靜態(tài)網(wǎng)頁(yè)數(shù)據(jù),可以直接獲取相關(guān)的內(nèi)容。
通過(guò)上述內(nèi)容,如果想要批量下載文章內(nèi)容, 獲取每篇文章的url地址即可, 想要獲取每篇文章的url地址,這就需要去文章的列表頁(yè)面找尋相關(guān)的數(shù)據(jù)內(nèi)容了。
💥整體思路
💥代碼實(shí)現(xiàn)
import requests import parsel import re import os import pdfkithtml_filename = 'html\\' if not os.path.exists(html_filename):os.mkdir(html_filename)pdf_filename = 'pdf\\' if not os.path.exists(pdf_filename):os.mkdir(pdf_filename)html_str = """ <!doctype html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title> </head> <body> {article} </body> </html> """def change_title(name):pattern = re.compile(r"[\/\\\:\*\?\"\<\>\|]") # '/ \ : * ? " < > |'new_title = re.sub(pattern, "_", name) # 替換為下劃線return new_titlefor page in range(1, 11):print(f'正在爬取第{page}頁(yè)數(shù)據(jù)內(nèi)容')url = f'https://www.chinawenwang.com/zlist-55-{page}.html'headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}response = requests.get(url=url, headers=headers)href = re.findall('<h2><a href="(.*?)" class="juhe-page-left-div-link">', response.text)for index in href:response_1 = requests.get(url=index, headers=headers)selector = parsel.Selector(response_1.text)title = selector.css('.content-page-header-div h1::text').get()title = change_title(title)content = selector.css('.content-page-main-content-div').get()article = html_str.format(article=content)html_path = html_filename + title + '.html'pdf_path = pdf_filename + title + '.pdf'try:with open(html_path, mode='w', encoding='utf-8') as f:f.write(article)# exe 文件存放的路徑config = pdfkit.configuration(wkhtmltopdf='C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe')# 把 html 通過(guò) pdfkit 變成 pdf 文件pdfkit.from_file(html_path, pdf_path, configuration=config)print(f'{title}保存成功...')except:pass💥實(shí)現(xiàn)效果
總結(jié)
以上是生活随笔為你收集整理的Python爬虫入门教程24:下载某网站付费文档保存PDF的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: IDEA解决Tomcat乱码问题
- 下一篇: python怎么根据度分布生成网络_py