python爬取”頂點小說網(wǎng)“《純陽劍尊》的示例代碼
更新時間:2020年10月16日 09:29:39 作者:Gg、
這篇文章主要介紹了python爬取”頂點小說網(wǎng)“《純陽劍尊》的示例代碼,幫助大家更好的利用python 爬蟲爬取數(shù)據(jù),感興趣的朋友可以了解下
爬取”頂點小說網(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'
}
# 獲得請求
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爬取”頂點小說網(wǎng)“《純陽劍尊》的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于python 爬取頂點小說網(wǎng)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
TensorFlow tf.nn.softmax_cross_entropy_with_logits的用法
這篇文章主要介紹了TensorFlow tf.nn.softmax_cross_entropy_with_logits的用法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
pytorch常用函數(shù)之torch.randn()解讀
這篇文章主要介紹了pytorch常用函數(shù)之torch.randn()解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
Python編程新標(biāo)準(zhǔn)學(xué)會十項好習(xí)慣提升編碼質(zhì)量
這篇文章主要為大家介紹了Python編程新標(biāo)準(zhǔn)學(xué)會十項好習(xí)慣提升編碼質(zhì)量,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
用python給csv里的數(shù)據(jù)排序的具體代碼
在本文里小編給大家分享的是關(guān)于用python給csv里的數(shù)據(jù)排序的具體代碼內(nèi)容,需要的朋友們可以學(xué)習(xí)下。2020-07-07
Pycharm社區(qū)版創(chuàng)建Flask項目的實現(xiàn)步驟
本文主要介紹了Pycharm社區(qū)版創(chuàng)建Flask項目,包括設(shè)置Python環(huán)境、安裝Flask庫以及創(chuàng)建基本的項目結(jié)構(gòu),具有一定的參考價值,感興趣的可以了解一下2024-06-06

