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 # 提取標題 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 # 保存標題 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()
爬取結果:
以上就是python爬取”頂點小說網(wǎng)“《純陽劍尊》的示例代碼的詳細內(nèi)容,更多關于python 爬取頂點小說網(wǎng)的資料請關注腳本之家其它相關文章!
相關文章
TensorFlow tf.nn.softmax_cross_entropy_with_logits的用法
這篇文章主要介紹了TensorFlow tf.nn.softmax_cross_entropy_with_logits的用法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-04-04pytorch常用函數(shù)之torch.randn()解讀
這篇文章主要介紹了pytorch常用函數(shù)之torch.randn()解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02用python給csv里的數(shù)據(jù)排序的具體代碼
在本文里小編給大家分享的是關于用python給csv里的數(shù)據(jù)排序的具體代碼內(nèi)容,需要的朋友們可以學習下。2020-07-07Pycharm社區(qū)版創(chuàng)建Flask項目的實現(xiàn)步驟
本文主要介紹了Pycharm社區(qū)版創(chuàng)建Flask項目,包括設置Python環(huán)境、安裝Flask庫以及創(chuàng)建基本的項目結構,具有一定的參考價值,感興趣的可以了解一下2024-06-06