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

睡前故事python 库运用

[复制链接]
  • TA的每日心情

    昨天 22:44
  • 签到天数: 416 天

    [LV.9]以坛为家II

    148

    主题

    423

    回帖

    1133

    积分

    版主

    积分
    1133

    油中2周年生态建设者

    发表于 2020-12-4 13:01:46 | 显示全部楼层 | 阅读模式
    处理起来更加简单
    1. import requests
    2. import parsel


    3. #此函数获取故事的链接
    4. def get_url():
    5.     res = requests.get('http://book.sbkk8.com/gushihui/taijiaogushi/')
    6.     selector = parsel.Selector(res.text)
    7.     #print(selector)
    8.     url_s = selector.css('.mulu > ul > li > a::attr(href)').getall()
    9.     #print(url_s)
    10.     for url in url_s:
    11.         url = 'http://book.sbkk8.com/' + url#到这里取出了所有正确的可访问的URL
    12.         #调用下载函数
    13.         down_story(url)
    14.     print('全部爬取完成!')



    15. #根据URL下载故事
    16. def down_story(url):
    17.     res = requests.get(url)
    18.     res.encoding=res.apparent_encoding#返回的进行编码
    19.     #print(res.text)
    20.     selector = parsel.Selector(res.text)
    21.     title = selector.css('#maincontent > h1::text').get()
    22.     story_list = selector.css('#content > p::text').getall()
    23.     #print(story_list)
    24.     with open (title+'.txt','w',encoding='utf-8') as f:
    25.         for i in range(len(story_list)):
    26.             f.write(story_list[i])
    27.     print('<'+title+'>'+'  爬取成功!')





    28. if __name__ == "__main__":
    29.     get_url()
    复制代码


    已有1人评分好评 贡献 理由
    李恒道 + 2 + 1

    查看全部评分 总评分:好评 +2  贡献 +1 

    I don't hate programming but the fucking world.
  • TA的每日心情
    开心
    2024-3-13 10:14
  • 签到天数: 211 天

    [LV.7]常住居民III

    284

    主题

    3810

    回帖

    3739

    积分

    管理员

    积分
    3739

    管理员荣誉开发者油中2周年生态建设者喜迎中秋油中3周年挑战者 lv2

    发表于 2020-12-4 13:38:57 | 显示全部楼层
    睡前故事有什么用?讲给妹妹听吗?
    上不慕古,下不肖俗。为疏为懒,不敢为狂。为拙为愚,不敢为恶。/ 微信公众号:一之哥哥
    回复

    使用道具 举报

  • TA的每日心情

    昨天 22:44
  • 签到天数: 416 天

    [LV.9]以坛为家II

    148

    主题

    423

    回帖

    1133

    积分

    版主

    积分
    1133

    油中2周年生态建设者

    发表于 2020-12-4 16:18:13 | 显示全部楼层
    王一之 发表于 2020-12-4 13:38
    睡前故事有什么用?讲给妹妹听吗?

    懂得都懂
    I don't hate programming but the fucking world.
    回复

    使用道具 举报

    发表回复

    本版积分规则

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