python爬取”頂點(diǎn)小說網(wǎng)“《純陽劍尊》的示例代碼
爬取”頂點(diǎn)小說網(wǎng)“《純陽劍尊》
代碼
import requests
from bs4 import BeautifulSoup
# 反爬
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, \
like Gecko) Chrome/70.0.3538.102 Safari/537.36'
}
# 獲得請(qǐng)求
def open_url(url):
response = requests.get(url, headers=headers)
response.encoding = response.apparent_encoding
html = response.text
return html
# 提取標(biāo)題
def get_title(url):
soup = BeautifulSoup(url, 'lxml')
title_tag = soup.find('dd')
title = '\n' + title_tag.h1.get_text() + '\n'
return title
# 提取文本
def get_texts(url):
soup2 = BeautifulSoup(url, 'lxml')
text_tags = soup2.find_all('dd', id="contents")
return text_tags
# 保存標(biāo)題
def save_title(filename, title):
with open(filename, 'a+', encoding='utf-8') as file:
file.write(title)
# 保存文本
def save_text(filename, text):
with open(filename, 'a+', encoding='utf-8') as file:
file.write(text)
# 主程序函數(shù)
def main():
num = input('《純陽劍尊》你想要下載第幾章?(1-802)')
num = int(num)
number = 8184027 + num
url = 'https://www.23us.so/files/article/html/15/15905/' + str(number) + '.html'
filename = '純陽劍尊.txt'
r = open_url(url)
title = get_title(r)
tags = get_texts(r)
save_title(filename, title)
for text_tag in tags:
text = text_tag.get_text() + '\n'
save_text(filename, text)
print('第{}章已經(jīng)下載完成!'.format(num))
if __name__ == '__main__':
main()
爬取結(jié)果:


以上就是python爬取”頂點(diǎn)小說網(wǎng)“《純陽劍尊》的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于python 爬取頂點(diǎn)小說網(wǎng)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python如何通過百度翻譯API實(shí)現(xiàn)翻譯功能
這篇文章主要介紹了Python如何通過百度翻譯API實(shí)現(xiàn)翻譯功能,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
TensorFlow tf.nn.softmax_cross_entropy_with_logits的用法
這篇文章主要介紹了TensorFlow tf.nn.softmax_cross_entropy_with_logits的用法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
django 實(shí)現(xiàn)簡(jiǎn)單的插入視頻
這篇文章主要介紹了django 實(shí)現(xiàn)簡(jiǎn)單的插入視頻,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04
pytorch常用函數(shù)之torch.randn()解讀
這篇文章主要介紹了pytorch常用函數(shù)之torch.randn()解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
Python編程新標(biāo)準(zhǔn)學(xué)會(huì)十項(xiàng)好習(xí)慣提升編碼質(zhì)量
這篇文章主要為大家介紹了Python編程新標(biāo)準(zhǔn)學(xué)會(huì)十項(xiàng)好習(xí)慣提升編碼質(zhì)量,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
用python給csv里的數(shù)據(jù)排序的具體代碼
在本文里小編給大家分享的是關(guān)于用python給csv里的數(shù)據(jù)排序的具體代碼內(nèi)容,需要的朋友們可以學(xué)習(xí)下。2020-07-07
Pygame實(shí)現(xiàn)簡(jiǎn)易版趣味小游戲之反彈球
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡(jiǎn)易版趣味反彈球游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Pycharm社區(qū)版創(chuàng)建Flask項(xiàng)目的實(shí)現(xiàn)步驟
本文主要介紹了Pycharm社區(qū)版創(chuàng)建Flask項(xiàng)目,包括設(shè)置Python環(huán)境、安裝Flask庫以及創(chuàng)建基本的項(xiàng)目結(jié)構(gòu),具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06

