鱼C论坛

 找回密码
 立即注册
查看: 6330|回复: 13

[技术交流] 【简单爬虫】 爬取妹子图 自动下载 [改进]

[复制链接]
发表于 2019-12-4 09:39:17 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 伪。 于 2019-12-4 09:40 编辑

改自: 【作品展示】闲来无事做了个半自动获取妹子图的爬虫

新增:
1.根据套图分类文件夹,将一个套图下载到一个文件夹。
2.自动创建文件夹。
3.自动读取下一页内容。
4.已经下载过的将不再进行下载。


  1. # 爬取妹子图
  2. import requests
  3. import os
  4. from bs4 import BeautifulSoup

  5. MZITU = 'https://www.mzitu.com'

  6. USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36'

  7. HEADERS = {
  8.     'User-Agent': USER_AGENT,
  9.     'Referer': MZITU}

  10. SAVE_PATH = 'D:\妹子图\\'


  11. def get_request(url):
  12.     res = requests.get(url, headers=HEADERS)
  13.     print("正在请求 " + url)
  14.     return res


  15. def get_request_soup(url):
  16.     html = get_request(url).text
  17.     print("正在解析 ")
  18.     return BeautifulSoup(html, 'lxml')


  19. def get_girl_list(url):
  20.     soup = get_request_soup(url)
  21.     next_url = soup.find(class_='next page-numbers').get('href')
  22.     girls = soup.find(class_='postlist').select('li')
  23.     for girl in girls:
  24.         url = girl.find('a').get('href')
  25.         name = girl.find('span').find('a').string
  26.         get_girl_group(url, 1, name)
  27.     if not next_url == "":
  28.         get_girl_list(next_url)


  29. def get_girl_group(url, page, name):
  30.     soup = get_request_soup(url + "/" + str(page))
  31.     max_page = int(soup.find(class_='pagenavi').select('span')[-2].string)
  32.     image = soup.find(class_='main-image').find('img').get('src')
  33.     save_img(image, name, page)
  34.     page += 1
  35.     if page < max_page:
  36.         get_girl_group(url, page, name)


  37. # 存储妹子图片到本地
  38. def save_img(url, file_name, page):
  39.     file_path = SAVE_PATH + file_name
  40.     image_path = file_path + "\" + str(page) + ".jpg"
  41.     print('检查文件是否存在 ' + image_path)
  42.     if not os.path.exists(image_path):
  43.         print("文件正在下载...")
  44.         html = get_request(url).content
  45.         print("下载成功")
  46.         print("检查文件夹是否存在")
  47.         exi = os.path.exists(file_path)
  48.         if not exi:
  49.             print("创建文件夹 " + file_name)
  50.             os.makedirs(file_path)
  51.         else:
  52.             print("文件夹已存在 " + file_name)
  53.         print("存入磁盘中...")
  54.         f = open(image_path, 'wb')
  55.         f.write(html)
  56.         print("存放完成")
  57.         f.close()
  58.     else:
  59.         print("文件已存在,无需重复下载" + image_path)


  60. get_girl_list(MZITU)
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-12-5 17:27:13 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-12-13 21:13:25 | 显示全部楼层
  File "E:/PythonFile/学习/xx.py", line 54
    image_path = file_path + "\" + str(page) + ".jpg"
                                                    ^
SyntaxError: EOL while scanning string literal
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-1-10 15:24:22 | 显示全部楼层
请问下 File "E:/学习py资料/尝试搭建服务器/venv/爬图2.py", line 54
    image_path = file_path + "\" + str(page) + ".jpg"
                                                    ^
SyntaxError: EOL while scanning string literal这个是什么意思呢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-1-10 15:24:56 | 显示全部楼层
Zynismus 发表于 2019-12-13 21:13
File "E:/PythonFile/学习/xx.py", line 54
    image_path = file_path + "\" + str(page) + ".jpg"
  ...

我也是遇到这个问题了,你解决了吗?是要建文件夹吗????还是怎么做呢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-15 09:57:29 | 显示全部楼层
本帖最后由 学Python不开车 于 2020-2-15 10:27 编辑

这个问题主要是 在于 储存路径的最后一位 不能是,反斜杠'\',如果是反斜杠就会报错, 即使用反斜杠转义也不行,所以要避免这种写法 ,可以将 SAVE_PATH 改成   SAVE_PATH = 'D://妹子图//'  把54行的 "\"  改成"/"

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-15 10:19:08 | 显示全部楼层
本帖最后由 学Python不开车 于 2020-2-15 10:27 编辑
还有苹果 发表于 2020-1-10 15:24
我也是遇到这个问题了,你解决了吗?是要建文件夹吗????还是怎么做呢



这个问题主要是 在于 储存路径的最后一位 不能是,反斜杠'\',如果是反斜杠就会报错, 即使用反斜杠转义也不行,所以要避免这种写法 ,可以将 SAVE_PATH 改成   SAVE_PATH = 'D://妹子图//'  把54行的 "\"  改成"/"
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-4 16:10:10 | 显示全部楼层
AttributeError: 'NoneType' object has no attribute 'select'

一套没下完就出现了这个错误。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-4 18:16:27 | 显示全部楼层
我正在再改进这个,然后测试时没设置爬取次数,现在给我封IP了,网站都打不开了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-11 13:52:32 | 显示全部楼层
D:\File\FILE\venv\Scripts\python.exe D:/File/FILE/pythonfile/pythonx/demo04.py
Traceback (most recent call last):
  File "D:/File/FILE/pythonfile/pythonx/demo04.py", line 76, in <module>
    get_girl_list(MZITU)
  File "D:/File/FILE/pythonfile/pythonx/demo04.py", line 30, in get_girl_list
    soup = get_request_soup(url)
  File "D:/File/FILE/pythonfile/pythonx/demo04.py", line 26, in get_request_soup
    return BeautifulSoup(html, 'lxml')
  File "D:\File\FILE\venv\lib\site-packages\bs4\__init__.py", line 225, in __init__
    raise FeatureNotFound(
bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?
正在请求 https://www.mzitu.com
正在解析

Process finished with exit code 1
我的执行报这个错误什么问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2020-3-11 16:41:46 | 显示全部楼层
好像跑不完
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-18 17:31:17 | 显示全部楼层
AttributeError: 'NoneType' object has no attribute 'select'
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-10 21:27:49 | 显示全部楼层
No module named 'requests'
这个是什么错误呢??
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-7 20:53:31 | 显示全部楼层
Betterman. 发表于 2021-3-10 21:27
No module named 'requests'
这个是什么错误呢??

你缺少这个库,去控制版面下呗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-25 13:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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