上一主题 下一主题
ScriptCat,新一代的脚本管理器脚本站,与全世界分享你的用户脚本油猴脚本开发指南教程目录
返回列表 发新帖

睡前故事python

[复制链接]
  • TA的每日心情
    慵懒
    2 小时前
  • 签到天数: 431 天

    [LV.9]以坛为家II

    148

    主题

    423

    回帖

    1144

    积分

    版主

    积分
    1144

    油中2周年生态建设者

    发表于 2020-12-3 15:10:58 | 显示全部楼层 | 阅读模式
    就很糟心 写完了 才看到有个一个专门的库去处理这种问题会简单很多。。。

    过几天重写一下⑧

    先记录一下这个代码
    1. import requests
    2. import re
    3. def get_stroy():
    4.     url = 'http://book.sbkk8.com/gushihui/taijiaogushi/'

    5.     headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/537.36'}
    6.     res = requests.get(url=url,headers=headers)
    7.     res.encoding = 'gbk'
    8.     name_list1 = re.compile(r'.html" target="_blank">(.*?)</a>').findall(res.text)
    9.     #print(name_list1)
    10.     name_list=[]
    11.     #爬出来的名字中有的有其他的符号  在这里我们对他进行替换掉
    12.     for i in range(len(name_list1)):
    13.         a=name_list1[i].replace('<b>','')
    14.         b=a.replace('</b>','')
    15.         name_list.append(b)
    16.     print(name_list)#到这里我们就得到了正确的故事标题 name_list
    17.     #我们开始获取URL链接
    18.     url_list1 = re.compile(r'mululist"> <a href="(.*?)" target="_blank">').findall(res.text)
    19.     print(url_list1)
    20.     url_list=[]
    21.     for i in range(len(url_list1)):#循环对url进行拼接
    22.         url_list.append(url_list1[i].replace('/gushihui/','http://book.sbkk8.com/gushihui/'))
    23.     print(url_list)
    24.     #到这里我们就有了所有的标题和url链接,
    25.     #进入url取出故事内容
    26.     story=[]
    27.     for i in range(len(url_list)):
    28.         url=url_list[i]
    29.         res_story = requests.get(url)
    30.         res_story.encoding = 'gbk'
    31.         # print(res_story.text)
    32.         res_story = re.compile(
    33.             r'</span><p>.*\s.*\s.*\s.*\s.*\s.*\s.*\s.*\s.*\s.*\s.*\s.*\s.*\s.*\s.*\s.*\s.*\s.*\s.*\s.*\s.*\s.*\s.*\s.*\s.*').findall(
    34.             res_story.text)[0]
    35.         # print(res_story)
    36.         a = res_story.replace('<br style="color: rgb(34, 34, 34); font-family: sans-serif, 宋体, 微软雅黑, arial, ', '')
    37.         a = a.replace('<u>一</u>', '')
    38.         a = a.replace('”', '"')
    39.         a = a.replace('</p></div>', '')
    40.         a = a.replace(', Helvetica; line-height: 24px;" />', '')
    41.         a = a.replace("'Helvetica Neue'", '')
    42.         a = a.replace('“', '"')
    43.         a = a.replace('</span><p>', '')
    44.         a = a.replace('</html>', '')
    45.         a = a.replace('</body>', '')
    46.         a = a.replace('<script src="http://book.sbkk8.com/js/foot.js" type="text/javascript"></script>', '')
    47.         a = a.replace('</div>', '')
    48.         a = a.replace(
    49.             '<p>声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请通知我们,我们会及时删除。联系Q-Q:2-8-1-6-4-1-1-4-9-5</p>', '')
    50.         a = a.replace('<p>2016 <b><a href="http://book.sbkk8.com">book.sbkk8.com</a></b> - 版权所有</p>', '')
    51.         a = a.replace('<div id="footer"> ', '')
    52.         a = a.replace(' <script src="http://book.sbkk8.com/js/pinglun.js" type="text/javascript"></script>', '')
    53.         a = a.replace('<script src="http://book.sbkk8.com/js/adBottom.js" type="text/javascript"></script>', '')
    54.         a = a.replace('<div class="sohuCmt1">', '')
    55.         a = a.replace('<div class="adBottom">', '')
    56.         a = a.replace('>【下一篇】<i>:</i></a></span>', '')
    57.         a = a.replace("'pagedaohang'", '')
    58.         a = a.replace('class=', '')
    59.         a = a.replace("'/gushihui/taijiaogushi/92709.html'", '')
    60.         a = a.replace('</span><span "toup1"><a href=', '')
    61.         a = a.replace('<script src="http://book.sbkk8.com/js/adBottom_sougou.js" type="text/javascript"></script>', '')
    62.         a = a.replace(
    63.             '>【第一篇】</a></span><span "tomenu"><a href="/gushihui/taijiaogushi/" title="胎教故事" "returnIndex">【回目录】</a>',
    64.             '')
    65.         a = a.replace(" 'pagedaohang pagedaohang1'", '')
    66.         a = a.replace('<div "prenext prenext1"><span "todown1"><a', '')
    67.         a = a.replace('<p>', '')
    68.         a = a.replace('</p>', '')
    69.         a = a.replace('"', '"')
    70.         a = a.replace('……', '')
    71.         story.append(a)

    72.     print(len(story))
    73.     for i in range(len(story)):
    74.         with open(name_list[i]+'.txt','w') as f:
    75.             f.write(story[i])
    76.             print(name_list[i]+'         已爬取!')





    77. if __name__ == "__main__":
    78.     get_stroy()
    复制代码


    I don't hate programming but the fucking world.

    发表回复

    本版积分规则

    快速回复 返回顶部 返回列表