欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python爬取csnd文章并轉(zhuǎn)為PDF文件

 更新時(shí)間:2022年01月25日 15:43:57   作者:松鼠愛吃餅干  
這篇文章主要介紹了Python爬取csnd文章并轉(zhuǎn)為PDF文件,通過開發(fā)者工具進(jìn)行抓包分析?分析數(shù)據(jù)從哪里來的,然后發(fā)送請(qǐng)求?對(duì)于文章列表頁面發(fā)送請(qǐng)求,獲取數(shù)據(jù)?獲取網(wǎng)頁源代碼,解析數(shù)據(jù)文章的url及文章標(biāo)題,發(fā)送請(qǐng)求?對(duì)于文章詳情頁url地址發(fā)送請(qǐng)求,需要的朋友可以參考一下

本篇文章流程(爬蟲基本思路):

數(shù)據(jù)來源分析 (只有當(dāng)你找到數(shù)據(jù)來源的時(shí)候, 才能通過代碼實(shí)現(xiàn))

  • 確定需求(要爬取的內(nèi)容是什么?)爬取CSDN文章內(nèi)容 保存pdf
  • 通過開發(fā)者工具進(jìn)行抓包分析 分析數(shù)據(jù)從哪里來的?

代碼實(shí)現(xiàn)過程:

  • 發(fā)送請(qǐng)求 對(duì)于文章列表頁面發(fā)送請(qǐng)求
  • 獲取數(shù)據(jù) 獲取網(wǎng)頁源代碼
  • 解析數(shù)據(jù) 文章的url 以及 文章標(biāo)題
  • 發(fā)送請(qǐng)求 對(duì)于文章詳情頁url地址發(fā)送請(qǐng)求
  • 獲取數(shù)據(jù) 獲取網(wǎng)頁源代碼
  • 解析數(shù)據(jù) 提取文章標(biāo)題 / 文章內(nèi)容
  • 保存數(shù)據(jù) 把文章內(nèi)容保存成html文件
  • 把html文件轉(zhuǎn)成pdf文件
  • 多頁爬取

1.導(dǎo)入模塊

import requests # 數(shù)據(jù)請(qǐng)求 發(fā)送請(qǐng)求 第三方模塊 pip install requests
import parsel # 數(shù)據(jù)解析模塊 第三方模塊 pip install parsel
import os # 文件操作模塊
import re # 正則表達(dá)式模塊
import pdfkit # pip install pdfkit

2.創(chuàng)建文件夾

filename = 'pdf\\' # 文件名字
filename_1 = 'html\\'
if not os.path.exists(filename): #如果沒有這個(gè)文件夾的話
? ? os.mkdir(filename) # 自動(dòng)創(chuàng)建一下這個(gè)文件夾

if not os.path.exists(filename_1): #如果沒有這個(gè)文件夾的話
? ? os.mkdir(filename_1) # 自動(dòng)創(chuàng)建一下這個(gè)文件夾

3.發(fā)送請(qǐng)求

for page in range(1, 11):
? ? print(f'=================正在爬取第{page}頁數(shù)據(jù)內(nèi)容=================')
? ? url = f'https://blog.csdn.net/qdPython/article/list/{page}'

? ? # python代碼對(duì)于服務(wù)器發(fā)送請(qǐng)求 >>> 服務(wù)器接收之后(如果沒有偽裝)被識(shí)別出來, 是爬蟲程序, >>> 不會(huì)給你返回?cái)?shù)據(jù)
? ? # 客戶端(瀏覽器) 對(duì)于 服務(wù)器發(fā)送請(qǐng)求 >>> 服務(wù)器接收到請(qǐng)求之后 >>> 瀏覽器返回一個(gè)response響應(yīng)數(shù)據(jù)
? ? # headers 請(qǐng)求頭 就是把python代碼偽裝成瀏覽器進(jìn)行請(qǐng)求
? ? # headers參數(shù)字段 是可以在開發(fā)者工具里面進(jìn)行查詢 復(fù)制
? ? # 并不是所有的參數(shù)字段都是需要的
? ? # user-agent: 瀏覽器的基本信息 (相當(dāng)于披著羊皮的狼, 這樣可以混進(jìn)羊群里面)
? ? # cookie: 用戶信息 檢測(cè)是否登錄賬號(hào) (某些網(wǎng)站 是需要登錄之后才能看到數(shù)據(jù), B站一些數(shù)據(jù)內(nèi)容)
? ? # referer: 防盜鏈 請(qǐng)求你的網(wǎng)址 是從哪里跳轉(zhuǎn)過來的 (B站視頻內(nèi)容 / 妹子圖圖片下載 / 唯品會(huì)商品數(shù)據(jù))
? ? # 根據(jù)不同的網(wǎng)站內(nèi)容 具體情況 具體分析
? ? headers = {
? ? ? ? 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36'
? ? }
? ? # 請(qǐng)求方式: get請(qǐng)求 post請(qǐng)求 通過開發(fā)者工具可以查看url請(qǐng)求方式是什么樣的
? ? # 搜索 / 登錄 /查詢 這樣是post請(qǐng)求
? ? response = requests.get(url=url, headers=headers)

4.數(shù)據(jù)解析

# 需要把獲取到的html字符串?dāng)?shù)據(jù)轉(zhuǎn)成 selector 解析對(duì)象
selector = parsel.Selector(response.text)
# getall 返回的是列表
href = selector.css('.article-list a::attr(href)').getall()

5.如果把列表里面每一個(gè)元素 都提取出來

for index in href:
? ? # 發(fā)送請(qǐng)求 對(duì)于文章詳情頁url地址發(fā)送請(qǐng)求
? ? response_1 = requests.get(url=index, headers=headers)
? ? selector_1 = parsel.Selector(response_1.text)
? ? title = selector_1.css('#articleContentId::text').get()
? ? new_title = change_title(title)
? ? content_views = selector_1.css('#content_views').get()
? ? html_content = html_str.format(article=content_views)
? ? html_path = filename_1 + new_title + '.html'
? ? pdf_path = filename + new_title + '.pdf'
? ? with open(html_path, mode='w', encoding='utf-8') as f:
? ? ? ? f.write(html_content)
? ? ? ? print('正在保存: ', title)

6.替換特殊字符

def change_title(name):
? ? mode = re.compile(r'[\\\/\:\*\?\"\<\>\|]')
? ? new_name = re.sub(mode, '_', name)
? ? return new_name

運(yùn)行代碼,即可下載HTML文件:

7.轉(zhuǎn)換成PDF文件

config = pdfkit.configuration(wkhtmltopdf=r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe')
pdfkit.from_file(html_path, pdf_path, configuration=config)

到此這篇關(guān)于Python爬取csnd文章并轉(zhuǎn)為PDF文件的文章就介紹到這了,更多相關(guān)Python爬取csnd文章內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論