环境:python3  pycharm

模块:requests  bs4  urlretrieve  os  time

第一步:获取网页源代码

import requests
from bs4 import BeautifulSoup
from urllib.request import urlretrieve
import os
import time
def get_html(url):
try:
response = requests.get(url)
response.encoding = 'gbk'
return response.text
except Exception as e:
print(e)
if __name__ == '__main__':
url = 'http://www.521609.com/meinvxiaohua/'
get_html(url)

第二步:下载美女图片

def down_show(html,page):
try:
soup = BeautifulSoup(html,'lxml')
all_img = soup.find("div",class_="index_img list_center").find_all('img') num = 1
for img in all_img:
src = img.get('src')
url_pic = 'http://www.521609.com' + src
if os.path.exists('show'):
pass
else:
os.mkdir('show')
urlretrieve(url_pic,'./show/'+'第%s页-%s.jpg'%(page,num))
num += 1
except Exception as e:
print(e)

第三步:可选打印多少页,代码所示下载5页

def get_pages(page):
for i in range(121,page+121):
url = 'http://www.521609.com/meinvxiaohua/list%d.html' % i
html = get_html(url)
down_show(html,i-120)
time.sleep(1)
print("图片下载完毕")
if __name__ == '__main__':
get_pages(5)

也可以采用多线程

import requests
from bs4 import BeautifulSoup
import threading
import time
import os headers = {
'Referer': 'http://www.521609.com/meinvxiaohua/',
'User-Agent': '',
} def get_html(url):
try:
response = requests.get(url=url,headers=headers)
response.encoding = "gb2312"
return response.text #文本,字符串
except Exception as e:
print(e) def mk_dir():
os.makedirs('./show/',exist_ok=True) def down_image(html,page):
try:
soup = BeautifulSoup(html,'lxml')#可以解析html,xml
all_img = soup.find('div',class_='index_img list_center').find_all('img')
num = 1
for img in all_img:
src = img.get('src')#后半部分的地址
url = 'http://www.521609.com' + src
content = requests.get(url=url,headers=headers).content#字节流
with open('./show/第%s页-%s.jpg' % (page,num),'wb') as file:
file.write(content)
num += 1
time.sleep(1)
except Exception as e:
print(e)
pass def get_pages(page):
for i in range(121,121+page):
url = "http://www.521609.com/meinvxiaohua/list%s.html" % i
html = get_html(url)
if not os.path.exists('show'):
mk_dir()
down_image(html,page)
time.sleep(1)
print('美女图片前%s页下载完毕' % str(i-120))
# if not os.path.exists('show'):
# mk_dir()
# thread = []
# for i in range(121,121+page):
# url = "http://www.521609.com/meinvxiaohua/list%s.html" % i
# html = get_html(url)
# t = threading.Thread(target=down_image,args=(html,str(i-120)))
# thread.append(t)
# for i in thread:
# i.start()
# for j in thread:
# j.join() def main():
start_time = time.time()
get_pages(3)
stop_time = time.time()
load_time = stop_time - start_time
print(load_time)#48.115086793899536 if __name__ == '__main__':
main()

第六篇 - bs4爬取校花网的更多相关文章

  1. python爬虫基础应用----爬取校花网视频

    一.爬虫简单介绍 爬虫是什么? 爬虫是首先使用模拟浏览器访问网站获取数据,然后通过解析过滤获得有价值的信息,最后保存到到自己库中的程序. 爬虫程序包括哪些模块? python中的爬虫程序主要包括,re ...

  2. Go语言实战-爬取校花网图片

    一.目标网站分析 爬取校花网http://www.xiaohuar.com/大学校花所有图片. 经过分析,所有图片分为四个页面,http://www.xiaohuar.com/list-1-0.htm ...

  3. python实战项目 — 爬取 校花网图片

    重点: 1.  指定路径创建文件夹,判断是否存在 2. 保存图片文件 # 获得校花网的地址,图片的链接 import re import requests import time import os ...

  4. scrapy爬取校花网男神图片保存到本地

    爬虫四部曲,本人按自己的步骤来写,可能有很多漏洞,望各位大神指点指点 1.创建项目 scrapy startproject xiaohuawang scrapy.cfg: 项目的配置文件xiaohua ...

  5. Scrapy爬虫框架之爬取校花网图片

    Scrapy Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中.其最初是为了页面抓取 (更确切来说, 网络抓取 )所设 ...

  6. Python-爬取校花网视频(单线程和多线程版本)

    一.参考文章 python爬虫爬取校花网视频,单线程爬取 爬虫----爬取校花网视频,包含多线程版本 上述两篇文章都是对校花网视频的爬取,由于时间相隔很久了,校花网上的一些视频已经不存在了,因此上述文 ...

  7. <scrapy爬虫>爬取校花信息及图片

    1.创建scrapy项目 dos窗口输入: scrapy startproject xiaohuar cd xiaohuar 2.编写item.py文件(相当于编写模板,需要爬取的数据在这里定义) # ...

  8. 二、Item Pipeline和Spider-----基于scrapy取校花网的信息

    Item Pipeline 当Item在Spider中被收集之后,它将会被传递到Item Pipeline,这些Item Pipeline组件按定义的顺序处理Item. 每个Item Pipeline ...

  9. 爬虫(猫眼电影+校花网+github+今日头条+拉钩)

    Requests+正则表达式爬取猫眼TOP100榜电影信息 MARK:将信息写入文件解决乱码方法,开启进程池秒爬. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

随机推荐

  1. StringBuilder与String有哪些区别?

    System.String具备不可修改性,在程序中这样的特性容易产生性能上的问题.针对这个问题.NET提供的StringBuilder类可以解决类似的问题. String 和 StringBuilde ...

  2. ASP.NET Web.config文件的配置(Configuration API)

    本次我们讨论主要聚焦在以下Web.config配置文件的设置值的读取. 1.<connectionString />连接字符串的读取. 2.<appSettings />应用程 ...

  3. 莫烦theano学习自修第九天【过拟合问题与正规化】

    如下图所示(回归的过拟合问题):如果机器学习得到的回归为下图中的直线则是比较好的结果,但是如果进一步控制减少误差,导致机器学习到了下图中的曲线,则100%正确的学习了训练数据,看似较好,但是如果换成另 ...

  4. Spring Boot 构建电商基础秒杀项目 (八) 商品创建

    SpringBoot构建电商基础秒杀项目 学习笔记 新建数据表 create table if not exists item ( id int not null auto_increment, ti ...

  5. size_t的使用

    size_t的取值range是目标平台下最大可能的数组尺寸 典型的例子:x64平台下size_t是8位,而x32平台下是4位: int在两个平台下均为4位 所以在使用的时候一定要配置好对应的平台,否则 ...

  6. cuda编程-矩阵乘法(2)

    采用shared memory加速 代码 #include <stdio.h> #include <stdlib.h> #include <math.h> #inc ...

  7. JarvisOJ Basic Base64?

    GUYDIMZVGQ2DMN3CGRQTONJXGM3TINLGG42DGMZXGM3TINLGGY4DGNBXGYZTGNLGGY3DGNBWMU3WI=== 题目非常具有迷惑性,我一开始以为就是一 ...

  8. Ubuntu16.04安装使用wps

    Ubuntu16.04安装使用wps 1.wps官网下载并安装wps 此处以Debian安装包为例,官网下载路径 http://www.wps.cn/product/wpslinux/# 直接安装: ...

  9. Mail.Ru Cup 2018 Round 2

    A:阅读理解. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> ...

  10. LeetCode:152_Maximum Product Subarray | 最大乘积连续子数组 | Medium

    题目:Maximum Product Subarray Find the contiguous subarray within an array (containing at least one nu ...