Python爬蟲之電子書爬取


python爬蟲學習01--電子書爬取

1.獲取網頁信息

import requests        #導入requests庫
''' 獲取網頁信息 '''
if __name__ == '__main__':          #主函數入口
    target = 'https://www.xsbiquge.com/78_78513/108078.html'#要爬取的目標地址
    req = requests.get(url=target)  #進行get請求
    req.encoding='utf-8'            #設置編碼
    print(req.text)                 #打印輸出

2.引入BeautifulSoup對網頁內容進行解析

import requests        #導入requests庫
from bs4 import BeautifulSoup  #引入BeautifulSoup庫

'''
引入BeautifulSoup對網頁內容進行解析
獲取網頁電子書文本信息
'''

if name == 'main': #主函數入口
target = 'https://www.xsbiquge.com/78_78513/108078.html'#要爬取的目標地址
req = requests.get(url=target) #發起請求,獲取html信息
req.encoding='utf-8' #設置編碼
html = req.text #將網頁的html信息保存在html變量中
bs = BeautifulSoup(html,'lxml') #使用lxml對網頁信息進行解析
texts = bs.find('div',id='content') #獲取所有<div id = "content">的內容
print(texts) #打印輸出

3.切分數據,去掉空格,提取文字

import requests        #導入requests庫
from bs4 import BeautifulSoup  #引入BeautifulSoup庫

'''
引入BeautifulSoup對網頁內容進行解析
獲取網頁電子書文本信息
最后一句texts.text 是提取所有文字,然后再使用 strip 方法去掉回車,
最后使用 split 方法根據 \xa0 切分數據,因為每一段的開頭,都有四個空格
'''

if name == 'main': #主函數入口
target = 'https://www.xsbiquge.com/78_78513/108078.html'#要爬取的目標地址
req = requests.get(url=target) #發起請求,獲取html信息
req.encoding='utf-8' #設置編碼
html = req.text #將網頁的html信息保存在html變量中
bs = BeautifulSoup(html,'lxml') #使用lxml對網頁信息進行解析
texts = bs.find('div',id='content') #獲取所有<div id = "content">的內容
print(texts.text.strip().split('\xa0'*4)) #打印輸出

4.查看章節列表

import requests        #導入requests庫
from bs4 import BeautifulSoup  #引入BeautifulSoup庫

'''
查看章節列表信息
引入BeautifulSoup對網頁內容進行解析
獲取網頁電子書文本信息

'''
if name == 'main': #主函數入口
target = 'https://www.xsbiquge.com/78_78513/'#要爬取的目標地址,《元尊》的章節目錄網址
req = requests.get(url=target) #發起請求,獲取html信息
req.encoding='utf-8' #設置編碼
html = req.text #將網頁的html信息保存在html變量中
bs = BeautifulSoup(html,'lxml') #使用lxml對網頁信息進行解析
chapters = bs.find('div',id='list') #獲取所有<div id = "list">的內容
chapters = chapters.find_all('a') #找到list中的a標簽中的內容
for chapter in chapters:
print(chapter) #打印章節列表

5.獲取章節目錄和章節鏈接

import requests        #導入requests庫
from bs4 import BeautifulSoup  #引入BeautifulSoup庫

'''
查看章節列表信息
引入BeautifulSoup對網頁內容進行解析
獲取網頁電子書文本信息

'''
if name == 'main': #主函數入口
server = 'https://www.xsbiquge.com'
target = 'https://www.xsbiquge.com/78_78513/'#要爬取的目標地址,《元尊》的章節目錄網址
req = requests.get(url=target) #發起請求,獲取html信息
req.encoding='utf-8' #設置編碼
html = req.text #將網頁的html信息保存在html變量中
bs = BeautifulSoup(html,'lxml') #使用lxml對網頁信息進行解析
chapters = bs.find('div',id='list') #獲取所有<div id = "list">的內容
chapters = chapters.find_all('a') #找到list中的a標簽中的內容
for chapter in chapters:
url = chapter.get('href') #獲取章節鏈接中的href
print("《"+chapter.string+"》") #打印章節名字
print(server+url) #將電子書網站與獲取到的章節連接進行拼接,得到每一個章節的鏈接

6.整合數據,下載電子書文檔

import requests        #導入requests庫
from bs4 import BeautifulSoup  #引入BeautifulSoup庫
import time
from tqdm import  tqdm

'''
查看章節列表信息
引入BeautifulSoup對網頁內容進行解析
獲取網頁電子書文本信息

'''
def get_content(target):
req = requests.get(url=target) # 發起請求,獲取html信息
req.encoding = 'utf-8' # 設置編碼
html = req.text # 將網頁的html信息保存在html變量中
bf = BeautifulSoup(html, 'lxml') # 使用lxml對網頁信息進行解析
texts = bf.find('div', id='content') # 獲取所有<div id = "content">的內容
content = texts.text.strip().split('\xa0' * 4)
return content

if name == 'main': #主函數入口
server = 'https://www.xsbiquge.com' #電子書網站地址
book_name = '《元尊》.txt'
target = 'https://www.xsbiquge.com/78_78513/'#要爬取的目標地址,《元尊》的章節目錄網址
req = requests.get(url=target) #發起請求,獲取html信息
req.encoding='utf-8' #設置編碼
html = req.text #將網頁的html信息保存在html變量中
chapter_bs = BeautifulSoup(html,'lxml') #使用lxml對網頁信息進行解析
chapters = chapter_bs.find('div',id='list') #獲取所有<div id = "list">的內容
chapters = chapters.find_all('a') #找到list中的a標簽中的內容
for chapter in tqdm(chapters):
chapter_name = chapter.string #章節名字
url = server + chapter.get('href') #獲取章節鏈接中的href
content = get_content(url)
with open(book_name,'a',encoding='utf-8') as f:
f.write("《"+chapter_name+"》")
f.write('\n')
f.write('\n'.join(content))
f.write('\n')

ps:下載的時候可能會有點慢,下載一本書大概十幾分鍾,在以后學到新的方法會改善的


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM