查看: 455|回复: 2

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

[复制链接]
发表于 2023-10-13 18:40 | 显示全部楼层 |阅读模式
非法程序、 2023-10-13 18:40 455 2 显示全部楼层

  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)
复制代码


发表于 2023-10-14 00:02 | 显示全部楼层
194122 2023-10-14 00:02 显示全部楼层
怎么我把账号密码输进去运行是这个结果
Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\解压到桌面打开\17k\17.py", line 3, in <module>
    from lxml import etree
ModuleNotFoundError: No module named 'lxml'

进程已结束,退出代码1

点评

解决了 没有安装软件包  详情 回复 发表于 2023-10-14 00:05
回复

使用道具 举报

发表于 2023-10-14 00:05 | 显示全部楼层
194122 2023-10-14 00:05 显示全部楼层
194122 发表于 2023-10-14 00:02
怎么我把账号密码输进去运行是这个结果
Traceback (most recent call last):
  File "C:%users\Administr ...

解决了 没有安装软件包
回复

使用道具 举报

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

本版积分规则 返回列表 发新帖

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