Python 爬虫2例:爬网络小说

程序逻辑:按给出的第一章节URL,抓HTML,然后通过正则表达式,取出小说章节的 标题、正文、下章节的URL, 然后跳转到下一章节,不断循环处理。 取出的正文写入文本文件。 同时记录每次取过的URL,如果网络异常了,重启程序,可以从文件中取URL继续上次的抓取任务。

正则,对应如下图:

Python 爬虫2例:爬网络小说_第1张图片

代码: https://download.csdn.net/download/fangkailove/11205946

#!/usr/bin/python
# -*- coding: gbk -*-

# by gnolux 20190526
# email: [email protected]

from urllib import request
import re
import os
import socket

#socket.setdefaulttimeout(60)

#第一章节的URL
url = 'https://www.23us.la/html/203/203086/1130014.html'
#URL前辍,用来拼接出下章节的绝对URL用
url_prex = 'https://www.23us.la/html/203/203086/'

#取章节标题的正则表达式
title_match_str = r'

(.*?)

' #取章节正文的正则表达式 content_match_str = r'
(.*?)
' #取下一章节URL的正则表达式(相对URL) next_page_match_str = r'

转载注明:转自 https://blog.csdn.net/fangkailove/article/details/90577174

你可能感兴趣的:(python,爬虫,Python)