python爬取”頂點(diǎn)小說(shuō)網(wǎng)“《純陽(yáng)劍尊》的示例代碼
爬取”頂點(diǎn)小說(shuō)網(wǎng)“《純陽(yá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('《純陽(yáng)劍尊》你想要下載第幾章?(1-802)')
num = int(num)
number = 8184027 + num
url = 'https://www.23us.so/files/article/html/15/15905/' + str(number) + '.html'
filename = '純陽(yáng)劍尊.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)小說(shuō)網(wǎng)“《純陽(yáng)劍尊》的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于python 爬取頂點(diǎn)小說(shuō)網(wǎng)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python?JMESPath庫(kù)輕松操作JSON進(jìn)行數(shù)據(jù)查詢方法實(shí)例
這篇文章主要為大家介紹了Python?JMESPath庫(kù)輕松操作JSON方法實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
PyTorch快速搭建神經(jīng)網(wǎng)絡(luò)及其保存提取方法詳解
本篇文章主要介紹了PyTorch快速搭建神經(jīng)網(wǎng)絡(luò)及其保存提取方法詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
python在前端頁(yè)面使用?MySQLdb?連接數(shù)據(jù)
這篇文章主要介紹了MySQLdb?連接數(shù)據(jù)的使用,文章主要介紹的相關(guān)內(nèi)容又插入數(shù)據(jù),刪除數(shù)據(jù),更新數(shù)據(jù),搜索數(shù)據(jù),需要的小伙伴可以參考一下2022-03-03
python 通過(guò)字符串調(diào)用對(duì)象屬性或方法的實(shí)例講解
下面小編就為大家分享一篇python 通過(guò)字符串調(diào)用對(duì)象屬性或方法的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
pytorch中的nn.Unfold()函數(shù)和fold()函數(shù)解讀
這篇文章主要介紹了pytorch中的nn.Unfold()函數(shù)和fold()函數(shù)用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08

