生活随笔
收集整理的這篇文章主要介紹了
用python抓取小说,以龙王传说为例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 目標網站:89文學網(https://www.89wxw.com)
2. python環境:IDLE
import requests
from bs4
import BeautifulSoup
def get_novel_chapters():data_1
= [] root_url
= 'https://www.89wxw.com/read/3440/'headers
= {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'}r
= requests
.get
(url
= root_url
, headers
= headers
)soup
= BeautifulSoup
(r
.text
,'html.parser')infos
= soup
.find_all
('dd')for info
in infos
:link
= info
.find
('a')['href']link
=str( 'https://www.89wxw.com'+link
)chapter
= str(info
.get_text
())data_1
.append
((link
,chapter
)) return data_1
def get_chapter_content(url
):data_2
= []headers
= {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'}r
= requests
.get
(url
= url
, headers
= headers
)soup
= BeautifulSoup
(r
.text
,'html.parser')contents
= soup
.find
('div', id = 'content').find_all
('p')for content
in contents
:content
= content
.get_text
()data_2
.append
(content
)return data_2
def download_txt(name
,content
):f
= open('%s.txt'%name
,'w')f
.write
(content
)f
.close
()for item
in get_novel_chapters
():link
,name
= itemcontent
= str(get_chapter_content
(link
))download_txt
(name
,content
)
附上成功圖
總結
以上是生活随笔為你收集整理的用python抓取小说,以龙王传说为例的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。