python實(shí)現(xiàn)爬蟲抓取小說功能示例【抓取金庸小說】
本文實(shí)例講述了python實(shí)現(xiàn)爬蟲抓取小說功能。分享給大家供大家參考,具體如下:
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
from urllib import request
import re
import os,time
#訪問url,返回html頁面
def get_html(url):
req = request.Request(url)
req.add_header('User-Agent','Mozilla/5.0')
response = request.urlopen(url)
html = response.read()
return html
#從列表頁獲取小說書名和鏈接
def get_books(url):#根據(jù)列表頁,返回此頁的{書名:鏈接}的字典
html = get_html(url)
soup = BeautifulSoup(html,'lxml')
fixed_html = soup.prettify()
books = soup.find_all('div',attrs={'class':'bbox'})
book_dict = {}
for book in books:
book_name = book.h3.a.string
book_url = book.h3.a.get('href')
book_dict[book_name] = book_url
return book_dict
#根據(jù)書名鏈接,獲取具體的章節(jié){名稱:鏈接} 的字典
def get_parts(url):
html = get_html(url)
soup = BeautifulSoup(html,'lxml')
fixed_html = soup.prettify()
part_urls = soup.find_all('a')
host = "http://www.xiaoshuotxt.org"
part_dict = {}
for p in part_urls:
p_url = str(p.get('href'))
if re.search(r'\d{5}.html',p_url) and ("xiaoshuotxt" not in p_url):
part_dict[p.string] = host + p_url
return part_dict
#根據(jù)章節(jié)的url獲取具體的章節(jié)內(nèi)容
def get_txt(url):
html = get_html(url)
soup = BeautifulSoup(html,'lxml')
fixed_html = soup.prettify()
title = soup.h1.string #獲取文章標(biāo)題
content = soup.find('div',attrs={'class':'zw'})
txt = BeautifulSoup.get_text(content) #正文內(nèi)容
return txt
if __name__ == "__main__":
root_dir= r'e:\books'
#url = 'http://www.xiaoshuotxt.org/mingzhu/index_2.html' #第2頁的小說
url = "http://www.xiaoshuotxt.org/writer/58" #金庸的小說
books = get_books(url)
for book_name,book_url in books.items():
os.mkdir(os.path.join(root_dir,book_name))
part_dict = get_parts(book_url)
print(book_name,"共:",len(part_dict),"章節(jié)")
for part_name,part_url in part_dict.items():
print("正在保存:",part_name)
f1 = open(r'e:\books\%s\%s.txt'%(book_name,part_name),'w',encoding='utf-8')#以utf-8編碼創(chuàng)建文件
part_txt = get_txt(part_url)
f1.write(str(part_txt))
f1.close()
time.sleep(2)
運(yùn)行效果:

更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python正則表達(dá)式用法總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python進(jìn)階之遞歸函數(shù)的用法及其示例
本篇文章主要介紹了Python進(jìn)階之遞歸函數(shù)的用法及其示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01
Python數(shù)據(jù)處理之pd.Series()函數(shù)的基本使用
Series是帶標(biāo)簽的一維數(shù)組,可存儲整數(shù)、浮點(diǎn)數(shù)、字符串、Python 對象等類型的數(shù)據(jù),軸標(biāo)簽統(tǒng)稱為索引,下面這篇文章主要給大家介紹了關(guān)于Python數(shù)據(jù)處理之pd.Series()函數(shù)的基本使用,需要的朋友可以參考下2022-06-06
python django事務(wù)transaction源碼分析詳解
這篇文章主要介紹了python django事務(wù)transaction源碼分析詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03
python使用pyshark庫捕獲數(shù)據(jù)包的示例詳解
PyShark是一個基于Python的網(wǎng)絡(luò)數(shù)據(jù)包分析工具庫,它允許用戶捕獲、解碼和分析實(shí)時網(wǎng)絡(luò)流量,特別是Wi-Fi和TCP/IP協(xié)議的數(shù)據(jù),所以本文給大家介紹了python使用pyshark庫捕獲數(shù)據(jù)包的示例,需要的朋友可以參考下2024-08-08
Python使用BeautifulSoup解析并獲取圖片的實(shí)戰(zhàn)分享
這篇文章主要介紹了Python使用BeautifulSoup解析并獲取圖片的實(shí)戰(zhàn)分享,文中通過代碼和圖文結(jié)合的方式給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-06-06

