用python爬個小說

2021-10-16 05:02:25 字數 3500 閱讀 3344

上下班,地鐵間,用手機在各類****看**時,總會有莫名其妙的彈窗,是不是很煩惱。其實我們可以借助python寫個小工具,將想看的**爬下來。

我們可以通過bp(瀏覽器f12也可以,不過不如bp直觀)看一下輸入目錄頁的url之後會看到什麼資訊

該結果是乙個json格式,其中欄位chaptername代表章節名稱,chapterid代表章節的id號,該id號在獲取每章節內容時會用到。

將具體的章節id替換到請求具體內容的url中去,會看到一下內容:

該結果也是乙個json格式,其中content欄位後面就代表**的具體內容,切分具體內容之後最好把

或者空格等影響觀感的字元去掉。

對應的**如下: 

def extract_catalog():

catalog_ids =

catalog_list = json.loads(requests.get(catalog_uri).text)['chapterlist']

for catalog_node in catalog_list:

return catalog_ids

def extract_one_content(content_id, story_file):

real_uri = content_uri.format(content_id)

response_result = json.loads(requests.get(real_uri).text)['result']

title_name = response_result['chaptername']

content = response_result['content'].split("千萬網友推薦:")[0]

content = re.sub('\s+', '', content).strip()

story_file.write("\n".format(title_name))

story_file.write(content.replace("

", "\n"))

story_file.write("".format("\n\n"))

def extract_content(ids, story_file):

for content_id in ids:

extract_one_content(content_id, story_file)

def main():

story_file = open("/tmp/story_guigushi.txt", "w+")

ids = extract_catalog()

extract_content(ids, story_file)

story_file.close()

if __name__ == '__main__':

main()

首先看到的目錄頁如下: 

目錄被a標籤包圍著,其中的chapterid為目錄id,title屬性為章節名稱。我們將chapterid替換到抓取內容的url中就可以抓取每一章節的內容,其內容截圖如下:

我們發現內容其實巢狀在id屬性值為chaptercontent的div中。於是我們可以通過python進行如下抓取:

def catalog_filter(tag):

is_right = false

if tag.name == "a" and tag.has_attr('href') \

and not tag.has_attr('id') and not tag.has_attr('class'):

if tag.attrs['href'].startswith(title_mark):

is_right = true

return is_right

def extract_catalog():

catalog_ids =

catalog_name =

sourp = beautifulsoup(requests.get(catalog_uri).text, 'html.parser')

for tag in sourp.find_all(catalog_filter):

return catalog_ids, catalog_name

def extract_one_content(content_id, title_name, story_file):

real_uri = content_uri.format(content_id)

sourp_content = beautifulsoup(requests.get(real_uri).text, 'html.parser')

for tag_content in sourp_content.find_all('div', attrs=):

story_file.write("\n".format(title_name))

for content in tag_content.strings:

content = re.sub('\s+', '', content).strip()

story_file.write(content.replace("

", "\n"))

story_file.write("\n")

story_file.write("".format("\n\n"))

break

def extract_content(ids, title_names, story_file):

for content_id,title_name in zip(ids, title_names):

extract_one_content(content_id, title_name, story_file)

def main():

story_file = open("/tmp/story_guigushi.txt", "w+")

ids, title_names = extract_catalog()

extract_content(ids, title_names, story_file)

story_file.close()

if __name__ == '__main__':

main()

其中 title_mark 的值為 "/chapterdetail.aspx?bookid=430178&chapterid=",作為href屬性值來過濾a標籤。

通過主要站抓取之後我們發現章節還是20幾章,其餘章節獲取需要登入。

於是,作為守法的公民,我們應當尊重智財權,換個**再看吧!

用python爬取小說章節內容

在學爬蟲之前,最好有一些html基礎,才能更好的分析網頁.主要是五步 1.獲取鏈結 2.正則匹配 3.獲取內容 4.處理內容 5.寫入檔案 如下 匯入相關model from bs4 import beautifulsoup import requests import re 獲取目標鏈結位址 ur...

用python爬取小說章節內容

在學爬蟲之前,最好有一些html基礎,才能更好的分析網頁.主要是五步 1.獲取鏈結 2.正則匹配 3.獲取內容 4.處理內容 5.寫入檔案 如下 匯入相關model 2from bs4 import beautifulsoup 3import requests 4importre5 6 獲取目標鏈結...

Python爬取小說

感覺這個夠蛋疼的,因為你如果正常寫的話,前幾次執行沒問題,之後你連 都沒改,再執行就出錯了。其實這可能是網路請求失敗,或者有反爬蟲的東西吧。但這就會讓你寫的時候非常苦惱,所以這這東西,健壯性及其重要!import requests from bs4 import beautifulsoup impo...