L-shuai

使用正则表达式

re.compile 函数

compile 函数用于编译正则表达式,生成一个正则表达式( Pattern )对象,供 match() 和 search() 这两个函数使用。

语法格式为:

 re.compile(pattern[, flags])

参数:

  • pattern : 一个字符串形式的正则表达式

  • flags : 可选,表示匹配模式,比如忽略大小写,多行模式等,具体参数为:

    1. re.I 忽略大小写

    2. re.L 表示特殊字符集 \w, \W, \b, \B, \s, \S 依赖于当前环境

    3. re.M 多行模式

    4. re.S 即为 . 并且包括换行符在内的任意字符(. 不包括换行符)

    5. re.U 表示特殊字符集 \w, \W, \b, \B, \d, \D, \s, \S 依赖于 Unicode 字符属性数据库

    6. re.X 为了增加可读性,忽略空格和 # 后面的注释

findall

在字符串中找到正则表达式所匹配的所有子串,并返回一个列表,如果没有找到匹配的,则返回空列表。

注意: match 和 search 是匹配一次 findall 匹配所有。

语法格式为:

 findall(string[, pos[, endpos]])

参数:

  • string : 待匹配的字符串。

  • pos : 可选参数,指定字符串的起始位置,默认为 0。

  • endpos : 可选参数,指定字符串的结束位置,默认为字符串的长度。

python爬虫之小说网站--下载小说(正则表达式)

思路:

  1. 找到要下载的小说首页,打开网页源代码进行分析(例:https://www.kanunu8.com/files/old/2011/2447.html)

  2. 分析自己要得到的内容,首先分析url,发现只有后面的是变化的,先获得小说的没有相对路径,然后组合成新的url(每章小说的url)

  3. 获得每章小说的内容,进行美化处理

相关截图

03qR9s.png

03qfcq.png

03qIBT.png

 

源代码

 
import re
 import requests
 ​
 # 要爬取的网站
 url = \'https://www.kanunu8.com/book4/10509/\'# 先获取二进制,再进行解码
 txt = requests.get(url).content.decode(\'gbk\')
 # txt.conten是二进制形式的   ---n<head>\r\n<title>\xd6\xd0\xb9\xfa\xba\xcf\xbb\xef\xc8\xcb
 # print(txt)
 ​
 m1 = re.compile(r\'<td colspan="4" align="center"><strong>(.+)</strong>\')
 # print(m1.findall(txt))
 ​
 m2 = re.compile(r\'<td( width="25%")?><a href="(.+\.html)">(.+)</a></td>\')
 print(m2.findall(txt))
 ​
 # 获得小说的目录以及对应的每个章节的相对路径
 raw = m2.findall(txt)
 ​
 sanguo = []
 for i in raw:
     print([i[2],url+i[1]])
     # [\'第五章 成功之母\', \'https://www.kanunu8.com/book4/10509/184616.html\']
     # 生成每个章节对应的url
     sanguo.append([i[2],url+i[1]])
 ​
 print("*"*100)
 print(sanguo)
 # [[\'第一章 梦的起源\', \'https://www.kanunu8.com/book4/10509/184612.html\'], [\'第二章 偶像兄弟\', \'https://www.kanunu8.com/book4/10509/184613.html\'], [\'第三章 恋爱必修\', \'https://www.kanunu8.com/book4/10509/184614.html\'], [\'第四章 爱的代价\', \'https://www.kanunu8.com/book4/10509/184615.html\'], [\'第五章 成功之母\', \'https://www.kanunu8.com/book4/10509/184616.html\'], [\'第六章 命运转折\', \'https://www.kanunu8.com/book4/10509/184617.html\'], [\'第七章 被迫下海\', \'https://www.kanunu8.com/book4/10509/184618.html\'], [\'第八章 渐行渐远\', \'https://www.kanunu8.com/book4/10509/184619.html\'], [\'第九章 三箭合一\', \'https://www.kanunu8.com/book4/10509/184620.html\'], [\'第十章 梦想起航\', \'https://www.kanunu8.com/book4/10509/184621.html\'], [\'第十一章 领航梦想\', \'https://www.kanunu8.com/book4/10509/184622.html\'], [\'第十二章 平地波澜\', \'https://www.kanunu8.com/book4/10509/184623.html\'], [\'第十三章 新的招牌\', \'https://www.kanunu8.com/book4/10509/184624.html\'], [\'第十四章 神的弱点\', \'https://www.kanunu8.com/book4/10509/184625.html\'], [\'第十五章 裂隙初现\', \'https://www.kanunu8.com/book4/10509/184626.html\'], [\'第十六章 上市之争\', \'https://www.kanunu8.com/book4/10509/184627.html\'], [\'第十七章 梦想巅峰\', \'https://www.kanunu8.com/book4/10509/184628.html\'], [\'第十八章 乾纲独断\', \'https://www.kanunu8.com/book4/10509/184629.html\'], [\'第十九章 一剑穿心\', \'https://www.kanunu8.com/book4/10509/184630.html\'], [\'第二十章 渡尽劫波\', \'https://www.kanunu8.com/book4/10509/184631.html\'], [\'尾\u3000声\', \'https://www.kanunu8.com/book4/10509/184632.html\']]
 ​
 ​
 # 匹配每章节的正文内容
 # 每章小说的正文在<p>标签中
 m3 = re.compile(r\'<p>(.+)</p>\',re.S)
 ​
 # 小说中的<br />要被替换为空白
 m4 = re.compile(r\'<br />\')
 ​
 # &nbsp;也要被替换
 m5 = re.compile(r\'&nbsp;&nbsp;&nbsp;&nbsp;\')
 ​
 # 新建一个txt 中国合伙人1.txt
 with open(\'中国合伙人1.txt\',\'a\') as f:
     for i in sanguo:
         # i[1] 是章节的url
         i_url = i[1]
         print("正在下载--->%s" % i[0])
         # 根据每个章节的url,先获取正文页面的二进制,再编码
         r_nr = requests.get(i_url).content.decode(\'gbk\')
         # 匹配正文  :带有<p>的
         n_nr = m3.findall(r_nr)
         print(n_nr)
         # 把<br/>替换为空  sub()和replace()区别:sub()可以用正则
         n = m4.sub(\'\',n_nr[0])
         # 把&nbsp;也替换为空
         n2 = m5.sub(\'\',n)
         n2 = n2.replace(\'\n\',\'\')
         # 写入txt
         # i[0]是章节名字
         f.write(\'\n\'+i[0]+\'\n\')
         f.write(n2)

 

 

分类:

技术点:

相关文章: