鱼C论坛

 找回密码
 立即注册
查看: 3037|回复: 1

[作品展示] 这是个好网站

[复制链接]
发表于 2020-3-7 21:42:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import requests
  2. from lxml import etree
  3. import os


  4. class ManhuaSpider:
  5.     def __init__(self):
  6.         self.url='http://www.ccnn88.com'
  7.         self.start_url='http://www.ccnn88.com/htm/Pic4/15608.htm'
  8.         self.headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36'}
  9.         
  10.     def parse_url(self, url):
  11.         r = requests.get(url, headers=self.headers)
  12.         return r
  13.         
  14.     def save_img(self, imgs, title):
  15.         os.mkdir(title)
  16.         os.chdir(title)
  17.         for i in imgs:
  18.             with open(i.url.split('_')[-1], 'wb') as f:
  19.                 f.write(i.content)
  20.         os.chdir('..')
  21.         
  22.     def get_content_list(self,url):
  23.         html = etree.HTML(url.content.decode())
  24.         title = html.xpath("/html/head/title/text()")[0]
  25.         print(title)
  26.         next_url = self.url + html.xpath("//li/a[contains(text(),'下一篇')]/@href")[0] if html.xpath("//li/a[contains(text(),'下一篇')]/@href") else None
  27.         if os.path.isdir(title):
  28.             return next_url
  29.         imgs = [self.parse_url(u) for u in html.xpath("//div[@class='details-content text-justify']//img/@src")]
  30.         self.save_img(imgs,title)
  31.         return next_url
  32.         
  33.     def run(self):
  34.         next_url = self.start_url
  35.         while next_url is not None:
  36.             res = self.parse_url(next_url)
  37.             next_url = self.get_content_list(res)
  38.       
  39.       
  40. if __name__ == "__main__":
  41.     manhua = ManhuaSpider()
  42.     manhua.run()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-8-1 21:59:28 | 显示全部楼层
Traceback (most recent call last):
  File "D:\AA\1.py", line 44, in <module>
    manhua.run()
  File "D:\AA\1.py", line 39, in run
    next_url = self.get_content_list(res)
  File "D:\AA\1.py", line 32, in get_content_list
    self.save_img(imgs,title)
  File "D:\AA\1.py", line 17, in save_img
    os.mkdir(title)
OSError: [WinError 123] 文件名、目录名或卷标语法不正确。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-24 16:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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