python類物件小說爬蟲 python小說爬蟲

2021-10-13 01:47:52 字數 1284 閱讀 9626

今天簡單的爬取了乙個**,**的爬蟲相對來說比較簡單,爬蟲的**是:

主要通過正規表示式進行的簡單的爬蟲,獲取每個章節的url位址,之後將介面中的內容獲取

通過html可以看到每章節的**後面都是和a標籤裡面的href屬性內容一樣的,只要獲取了屬性中的內容,就可以獲得每個章節網頁的**,通過每個章節的**來獲取每章**的內容

import requests

import re

url = ''

response = requests.get(url)

response.encoding = 'gbk'

html = response.text

title = re.findall(r'',html)[0]

fb = open('%s.txt' % title,'w',encoding='utf-8')

#獲取每章的內容

#print(html)

dl = re.findall(r'

正文

',html)[0]

print(dl)

chapter_info_list = re.findall(r'

(.*?)',dl)

print(chapter_info_list)

for chapter_info in chapter_info_list:

chapter_url,chapter_title = chapter_info

chapter_url = "" %chapter_url

#print(chapter_url)

chapter_response = requests.get(chapter_url)

chapter_response.encoding = 'gbk'

chapter_html = chapter_response.text

chapter_content = re.findall(r'

(.*?)>

',chapter_html)[0]

#print(chapter_content)

chapter_content = chapter_content.replace('

chapter_content = chapter_content.replace('

','')

fb.write(chapter_title)

fb.write(chapter_content)

fb.write('\n')

print(chapter_url)

這是最後獲取的**的內容

Python 爬蟲(獲取小說)

以 筆趣閣 為例 需求 python3版本以上 安裝方法如下 先安裝python3 pip,然後檢查下版本,如果版本可以公升級,就 upgrade pip 一下,然後再安裝beautifulsoup4 sudo apt get install python3 pip pip3 version pip...

python 爬蟲,抓取小說

coding utf 8 from bs4 import beautifulsoup from urllib import request import re import os,time 訪問url,返回html頁面 defget html url req request.request url ...

python 爬蟲東宮小說

2k 網爬取最近大火的 東宮 借鑑之前看過的一段 修改之後,進行簡單爬取。from urllib import request from bs4 import beautifulsoup url req request.request url response request.urlopen req...