Python3中BeautifulSoup爬取筆趣閣小說網


1.背景

  一般我們進行小說網的爬取,大致思路都是先獲取小說網頁的html內容,然后使用正則表達式找到對應的章節以及其對應的url。BeautifulSoup是用Python寫的一個HTML/XML的解析器,它可以很好的處理不規范標記並生成剖析樹(parse tree)。 它提供簡單又常用的導航(navigating),搜索以及修改剖析樹的操作。使用BeautifulSoup來爬取小說網,將大大減少正則表達式的使用,提高效率。

2.代碼解析

2.1導包

2.2函數獲取html頁面的內容

  向函數傳入一個url,函數會構造一個url請求,向目標url發送請求並獲取響應對象,然后讀取獲得的內容,再返回。

2.3函數獲取小說的章節目錄

  向函數傳入一個html頁面的內容,這里傳入應該是具體小說的首頁,也就是有章節目錄的那個頁面,函數獲取章節目錄頁面的內容后,先構建一個soup對象,緊接着運用soup對象里面的方法,分別獲取小數的標題和章節名稱以及其對應的url,最后以字典的形式返回。

2.4函數獲取小說的文本內容

  向函數傳入以章節名稱和對應的url為鍵值對的字典和小說標題,函數遍歷這個字典,調用gain_html_content 函數獲得小說的html頁面的內容,然后構建soup對象,並直接找到小說文章內容的標簽,並獲取其中的文本信息,最后調用write2file函數,寫入本地txt文件。

2.5將小說內容寫入本地文件

  這個就沒啥好說的了,直接將小說文本內容,寫入本地。

2.6代碼總覽

  1. # -*- coding: utf-8 -*-  
  2. # @Time    : 2018/5/21 09:08  
  3. # @Author  : daigua  
  4. # @File    : 12-筆趣-beautifulsoup.py  
  5. # @Software: PyCharm  
  6.     
  7.     
  8. from bs4 import BeautifulSoup  
  9. import re  
  10. import urllib.request  
  11. import os  
  12.     
  13.     
  14. def gain_html_content(url):  
  15.     """獲取網頁的html內容 
  16.         url:url地址 
  17.         content:返回的面內容 
  18.     """  
  19.     構建  
  20.     headers = {  
  21.         "User-Agent""Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36"  
  22.     }  
  23.     request = urllib.request.Request(url, headers=headers)  
  24.     發送請求  
  25.     response = urllib.request.urlopen(request)  
  26.     讀取文件  
  27.     content = response.read().decode('utf-8')  
  28.     return content  
  29.     
  30.     
  31. def get_chapter(content):  
  32.     先構建一個soup對象  
  33.     soup = BeautifulSoup(content, "lxml")  
  34.     獲取小說的標題  
  35.     title = soup.title.string  
  36.     找到小的內容(是在div標簽里面,並且這個div標簽的id"list"  
  37.     content = soup.find("div", id="list")  
  38.     獲取章節列表,列表里面的內容都是標簽對  
  39.     chapter_list = content.find_all("a", attrs={"style": "", "href": re.compile(r"/.*\.html")})  
  40.     一個空的字典,用於存放章名稱和對應url鍵值對  
  41.     chapter_dict = dict()  
  42.     for x in chapter_list:  
  43.         file_name = x.string  
  44.         file_url = x.attrs["href"]  獲取a標簽中href屬性里面的  
  45.         chapter_dict[file_name] = "https://www.xs.la" + file_url  
  46.     將章字典,和標題返回  
  47.     return chapter_dict, title  
  48.     
  49.     
  50. def get_text(chapter_dict, title):  
  51.     for name, url in chapter_dict.items():  
  52.         獲取頁面內容  
  53.         content = gain_html_content(url)  
  54.         soup_text = BeautifulSoup(content, "lxml")  
  55.         new_content = soup_text.find("div", id="content")  
  56.     
  57.         獲取soup對象中的文本信息text  
  58.         new_content = new_content.get_text()  
  59.         調用寫入本地的函數  
  60.         write2file(title, name, new_content)  
  61.     
  62.     
  63. def write2file(title, file_name, content):  
  64.     """將小寫入本地文件"""  
  65.     print("%s中。。。" % file_name)  
  66.     direction = title + "/" + file_name  
  67.     if not os.path.exists(title):  
  68.         os.mkdir(title)  
  69.     with open(direction + ".txt"'w') as f:  
  70.         f.write(content)  
  71.     print("%s!" % file_name)  
  72.     
  73.     
  74. def main():  
  75.     獲取頁面內容  
  76.     tar_url = input("請輸入小說網址:")  
  77.     content = gain_html_content(tar_url)  
  78.     獲取 名字:url 字典和小說標題  
  79.     dict1, title = get_chapter(content)  
  80.     獲取小說內容,並寫入本地txt文件  
  81.     get_text(dict1, title)  
  82.     
  83.     
  84. if __name__ == "__main__":  
  85.     main()  

4.一些說明

  • 本例中用到一些BeautifulSoup的方法,都可以在網上查得到,所以本例並未對其具體用法進行說明;
  • 本例只是一個小測試,所以還存在一些問題,比如爬到一半斷網,重新爬取的時候又得從頭開始,還有本例使用的是單任務,效率沒有多任務高,這些問題改進就由各位去做了,本例主要目的只是展現爬取小說網的思路。

 


免責聲明!

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



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