鱼C论坛

 找回密码
 立即注册
楼主: ietar

[作品展示] 复习一下妹子图(啊不对)爬虫

  [复制链接]
发表于 2019-6-16 23:39:16 | 显示全部楼层
哈哈,赚大了,改进了下,不报错,然后到现在都下载了几百个的美女了
不解释,直接说步骤:
1.先在CMD里面安装:
pip install requests
pip3 install Beautifulsoup4
pip3 install lxml
2:贴代码(改进方法):
a、运行了很多次报错,后面进行了一个非空判断
b.楼主说只有24个,我就找了个能遍历的路径,然后就可以一直遍历,路径可以修改哈,例如 https://www.mzitu.com/xinggan/page/2
  1. from bs4 import BeautifulSoup
  2. import requests
  3. import os

  4. # 使用说明 将py文件放在要保存图包的目录下运行
  5. # number别太贪心 目前主页24个 我也就设置最大24

  6. # 源url number确认要整几个图包
  7. for aa in range(2,100):
  8.         pageCount = aa
  9.         url0 = 'https://www.mzitu.com/xinggan/page/' + str(aa)
  10.         headers={
  11.                 'referer': 'https://www.mzitu.com/188045',
  12.                 'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Mobile Safari/537.36'
  13.                 }
  14.         # number = int(input('how many:(max=24)'))
  15.         # while number > 24:
  16.         #     number = int(input('how many:(max=24)'))
  17.         number = 24
  18.         # 打开主页
  19.         res0 = requests.get(url0,headers=headers)
  20.         soup0 = BeautifulSoup(res0.text,'lxml')

  21.         # 正式开整
  22.         for h in range(number):
  23.                 url = soup0.find('div', class_='main').find('div', class_='postlist')\
  24.                           .find_all('li')[h].a['href']
  25.                 res = requests.get(url,headers=headers)
  26.                 soup = BeautifulSoup(res.text,'lxml')
  27.                 target_url = soup.find('div',class_='main-image').img['src']
  28.                 # filename = target_url.split(r'.net/')[-1]
  29.                 # 懒得处理filename里的/了 直接1 2 3 4命名图片完事
  30.                 pages = soup.find('div',class_='pagenavi').find_all('a')[-2].span.text
  31.                 title = soup.find('div',class_='content').find('h2',class_='main-title').text

  32.                 print('正在搞第{}个'.format(h+1))
  33.                 if title not in os.listdir():
  34.                         os.mkdir(title)
  35.                 os.chdir(title)

  36.                 for i in range(int(pages)):
  37.                         url1 = url + r'/' + str(i+1)
  38.                         res1 = requests.get(url1,headers=headers)
  39.                         soup = BeautifulSoup(res1.text,'lxml')
  40.                         target_url = soup.find('div',class_='main-image').img['src']
  41.                         if target_url is None:
  42.                                 break
  43.                         res1 = requests.get(target_url,headers=headers)
  44.                         with open(str(i+1)+r'.jpg', 'wb') as f:
  45.                                 f.write(res1.content)
  46.                 os.chdir('..')
  47.                
  48. # 我完事了 你们呢

  49. #感谢楼主,修改代码by 七夜长川(也是新手)

复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-16 23:50:38 | 显示全部楼层
学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-6-17 00:03:50 | 显示全部楼层
  1. from bs4 import BeautifulSoup
  2. import requests
  3. import os

  4. # 使用说明 将py文件放在要保存图包的目录下运行
  5. # number别太贪心 目前主页24个 我也就设置最大24

  6. # 源url number确认要整几个图包
  7. for aa in range(2,100):
  8.         pageCount = aa
  9.         url0 = 'https://www.mzitu.com/xinggan/page/' + str(aa)
  10.         headers={
  11.                 'referer': 'https://www.mzitu.com/188045',
  12.                 'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Mobile Safari/537.36'
  13.                 }
  14.         # number = int(input('how many:(max=24)'))
  15.         # while number > 24:
  16.         #     number = int(input('how many:(max=24)'))
  17.         number = 24
  18.         # 打开主页
  19.         res0 = requests.get(url0,headers=headers)
  20.         soup0 = BeautifulSoup(res0.text,'lxml')

  21.         # 正式开整
  22.         for h in range(number):
  23.                 url = soup0.find('div', class_='main').find('div', class_='postlist')\
  24.                           .find_all('li')[h].a['href']
  25.                 res = requests.get(url,headers=headers)
  26.                 soup = BeautifulSoup(res.text,'lxml')
  27.                 target_url = soup.find('div',class_='main-image').img['src']
  28.                 # filename = target_url.split(r'.net/')[-1]
  29.                 # 懒得处理filename里的/了 直接1 2 3 4命名图片完事
  30.                 pages = soup.find('div',class_='pagenavi').find_all('a')[-2].span.text
  31.                 title = soup.find('div',class_='content').find('h2',class_='main-title').text

  32.                 print('正在搞第{}个'.format(h+1))
  33.                 if title not in os.listdir():
  34.                         os.mkdir(title)
  35.                 os.chdir(title)

  36.                 for i in range(int(pages)):
  37.                         url1 = url + r'/' + str(i+1)
  38.                         res1 = requests.get(url1,headers=headers)
  39.                         soup = BeautifulSoup(res1.text,'lxml')
  40.                         target_url = soup.find('div',class_='main-image').img['src']
  41.                         if target_url is None:
  42.                                 break
  43.                         res1 = requests.get(target_url,headers=headers)
  44.                         with open(str(i+1)+r'.jpg', 'wb') as f:
  45.                                 f.write(res1.content)
  46.                 os.chdir('..')
  47.                
  48. # 我完事了 你们呢

  49. #感谢楼主,修改代码by 七夜长川(也是新手)

复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-6-17 09:00:23 | 显示全部楼层
MC摸鱼 发表于 2019-6-16 11:09
若是服务器在你beautifulsoup后更新了数据怎么办呢?

完全不影响吧 这个爬虫的功能 是不管内容如何 只管爬几个
要获取最新的(1个)就再运行1次 how many填(1)就好了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-6-17 09:00:58 | 显示全部楼层
七夜长川 发表于 2019-6-16 19:20
pip install requests
pip3 install Beautifulsoup4
pip3 install lxml

你就是课代表咯
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-19 09:29:28 | 显示全部楼层
ietar 发表于 2019-6-16 09:47
爬虫一般是只针对一个任务的 把首页换成台湾首页还能正常工作 只是凑巧两个页面结构一样而已..

哦哦,这样啊。谢谢你!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-19 10:25:44 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-6-20 23:31:26 From FishC Mobile | 显示全部楼层
强无敌
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-23 23:34:11 From FishC Mobile | 显示全部楼层
学习了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-24 00:17:41 | 显示全部楼层

这。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-25 12:45:41 From FishC Mobile | 显示全部楼层
haha
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-6-26 19:26:23 | 显示全部楼层
Traceback (most recent call last):
  File "D:/Pictures/第一个爬虫.py", line 1, in <module>
    from bs4 import BeautifulSoup
ModuleNotFoundError: No module named 'bs4'

这是啥意思
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-26 23:36:07 From FishC Mobile | 显示全部楼层
66
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-6-28 07:19:34 From FishC Mobile | 显示全部楼层
這個能不推一下嗎?!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-28 08:32:22 From FishC Mobile | 显示全部楼层
我还得努力学啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-28 09:56:53 | 显示全部楼层
请问怎么操作怎么用阿
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-29 16:49:05 | 显示全部楼层
cangmao 发表于 2019-6-26 19:26
Traceback (most recent call last):
  File "D:/Pictures/第一个爬虫.py", line 1, in
    from bs4 im ...

导入三个包,这个包需要额外安装,我写了pip的,你找找看看,pip install BeautifulSoup
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-29 19:15:39 | 显示全部楼层
支持一下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-2 20:48:14 From FishC Mobile | 显示全部楼层
大神好厉害。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-2 23:34:18 | 显示全部楼层
不错不错
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-29 00:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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