广告 全新升级

[其他发布] 17k小说网爬取书架中的小说

  [复制链接]
查看45672 | 回复165 | 2023-10-14 08:27 | 显示全部楼层 |阅读模式

  1. import requests
  2. from lxml import etree
  3. import os
  4. headers  = {
  5.         "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
  6.         }
  7. session = requests.session()

  8. def login():
  9.     session.post("https://passport.17k.com/ck/user/login",data={
  10.                             "loginName": "账号",
  11.                             "password": "密码"
  12.                     },headers=headers)


  13. def get_shelf_books():
  14.     '''

  15.     :return:
  16.     '''
  17.     res = session.get("https://user.17k.com/ck/author2/shelf?page=1&appKey=2406394919")
  18.     res.encoding="utf8"
  19.     data = res.json().get("data")
  20.     return data


  21. def get_books(data):

  22.     for bookDict in data:
  23.         # print(bookDict)
  24.         bookID = bookDict.get("bookId")
  25.         bookName = bookDict.get("bookName")
  26.         book_path = os.path.join(root_path,bookName)
  27.         if not os.path.exists(book_path):
  28.             os.mkdir(book_path)
  29.         get_chapter(bookID,book_path,bookName)
  30. def get_chapter(bookID,book_path,bookName):
  31.     res = requests.get(f"https://www.17k.com/list/{bookID}.html")
  32.     # print("res",res)
  33.     res.encoding = "utf8"

  34.     selector = etree.HTML(res.text)
  35.     items = selector.xpath('//dl[@class="Volume"]/dd/a')
  36.     # print(items)


  37.     for item in items:
  38.         chapter_href = item.xpath('./@href')[0]
  39.         chapter_title = item.xpath('./span/text()')[0].strip()
  40.         print("chapter_href", chapter_href)
  41.         print("chapter_text", chapter_title)

  42.         res = requests.get("https://www.17k.com" chapter_href)
  43.         res.encoding = "utf8"
  44.         chapter_html = res.text
  45.         print(chapter_html)
  46.         selector = etree.HTML(res.text)
  47.         chapter_text = selector.xpath('//div[contains(@class,"content")]/div[@class="p"]/p[position()<last()]/text()')
  48.         # ret = selector.xpath('//div[contains(@class,"content")]/div[@class="p"]/p[position()<last()]/text()')
  49.         print(chapter_text)
  50.         download(book_path, chapter_title,chapter_text)

  51.         print(f'{bookName}书籍的{chapter_title}章节已经下载完成')

  52. def download(book_path, chapter_title,chapter_text):
  53.     chapter_path = os.path.join(book_path, chapter_title)
  54.     with open(chapter_path, "w", encoding="utf8") as f:
  55.         for line in chapter_text:
  56.             f.write(line "\n")

  57. login()
  58. data = get_shelf_books()
  59. root_path = "我的书架"
  60. if  not os.path.exists(root_path):
  61.     os.mkdir(root_path)

  62. get_books(data)
复制代码


蓝奏链接如访问不了把lanzous换成lanzoux或lanozui
;主题回复不显示隐藏内容就刷新页面;
酉茸 | 2023-10-14 15:46 | 显示全部楼层
太生气了,无法HOLD啦 >_<......
回复

使用道具 举报

3454ki0d0y | 2023-10-14 17:30 | 显示全部楼层
66666666666666
回复

使用道具 举报

vncpuxerwf | 2023-10-14 18:06 | 显示全部楼层
强烈支持楼主ing……
回复

使用道具 举报

谭4142 | 2023-10-14 18:23 | 显示全部楼层
感恩无私的分享与奉献 :)
回复

使用道具 举报

tppccjjwlq | 2023-10-14 19:51 | 显示全部楼层
楼主加油,我们都看好你哦。
回复

使用道具 举报

有芝 | 2023-10-14 20:00 | 显示全部楼层
66666666666666
回复

使用道具 举报

xdtm8tdy7l | 2023-10-15 02:51 | 显示全部楼层
激动人心,无法言表!
回复

使用道具 举报

ho9j3i9487 | 2023-10-15 04:28 | 显示全部楼层
感恩无私的分享与奉献 :)
回复

使用道具 举报

evrcxbnzsu | 2023-10-15 06:51 | 显示全部楼层
楼主我爱你!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则