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

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:下載的時候可能會有點慢,下載一本書大概十幾分鍾,在之後學到新的方法會改善的

相關文章
相關標籤/搜索