使用request+Beautiful爬取妹子图

一、request安装

pip install requests

request使用示例

import requests
response = requests.get('https://www.mzitu.com/') # get()方法发送请求,获取HTML网页
response.status_code  # 返回状态码
response.text  #以文本格式返回网页内容
response.content  # 以二进制形式返回

二、BeautifulSoup库

BeautifulSoup是Python的一个库,最主要的功能就是从网页爬取我们需要的数据。BeautifulSoup将html解析为对象进行处理,全部页面转变为字典或者数组。

pip install beautifulsoup4

BeautifulSoup基本使用

三、抓取图片

from bs4 import BeautifulSoup
url='http://www.mzitu.com'
res = requests.get(url)    # get()方法发送请求,获取HTML网页
soup = BeautifulSoup(res.text, 'html.parser')  # # 使用BeautifulSoup来解析我们获取到的网页 
#coding=utf-8
import requests
from bs4 import BeautifulSoup
def imgurl(url):
    res = requests.get(url)   # url为a标签的helf链接,即为图片封面的图片
    soup = BeautifulSoup(res.text, 'html.parser')   # 使用BeautifulSoup来解析我们获取到的网页
    page = int(soup.select('.pagenavi span')[-2].text)   # 获取总页数,-2为去掉上下页
    # a = soup.select('.main-image a')[0]  # 获取当前图片链接
    # src = a.select('img')[0].get('src')
    src = soup.select('.main-image a img')[0].get('src')  # 获取图片链接
    meiziid = src[-9:-6]  # 切片将src的倒数的字符串做名字
    print('开始下载妹子:', format(meiziid))  # 输出窗口提示下载
    for i in range(1, page+1):
        i = '%02d' % i
        img = src.replace('01.jpg', str(i)+'.jpg')  # replace()替换页数
        #  添加headers模拟浏览器工作 反反爬
        headers = {
            'User-Agent': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)',
            'Referer': 'http://www.mzitu.com'
        }
        response = requests.get(img, headers=headers)

        f = open('/Users/xcn/Desktop/mzitu/'+meiziid+'%s.jpg' % i, 'wb')  # 放在D:\666\目录下
        f.write(response.content)
        f.close()
        print('===> %s 完成 ' % (meiziid + i))
    print(' %s 已下载\n' % meiziid)

def imgpage(page=''):
    res = requests.get('http://www.mzitu.com/page/' + page)
    soup = BeautifulSoup(res.text, 'html.parser')  # 解析页面
    href = soup.select('#pins a')  # 筛选

    list = set([i.get('href') for i in href])  # 遍历获取筛选后的href链接并用set()去掉重复的链接
    [imgurl(i) for i in list]  # 遍历下载

result = input('下载哪一页:')
imgpage(result)

四、大功告成

原文地址:https://www.cnblogs.com/baishuchao/p/9282226.html

时间: 2024-07-30 15:35:35

使用request+Beautiful爬取妹子图的相关文章

Python 爬虫入门(二)——爬取妹子图

Python 爬虫入门 听说你写代码没动力?本文就给你动力,爬取妹子图.如果这也没动力那就没救了. GitHub 地址: https://github.com/injetlee/Python/blob/master/%E7%88%AC%E8%99%AB%E9%9B%86%E5%90%88/meizitu.py 公众号:[智能制造专栏].欢迎关注,分享智能制造与编程那些事. 爬虫成果 当你运行代码后,文件夹就会越来越多,如果爬完的话会有2000多个文件夹,20000多张图片.不过会很耗时间,可以在

Python 爬虫入门之爬取妹子图

Python 爬虫入门之爬取妹子图 来源:李英杰  链接: https://segmentfault.com/a/1190000015798452 听说你写代码没动力?本文就给你动力,爬取妹子图.如果这也没动力那就没救了. GitHub 地址: https://github.com/injetlee/Python/blob/master/%E7%88%AC%E8%99%AB%E9%9B%86%E5%90%88/meizitu.py 爬虫成果 当你运行代码后,文件夹就会越来越多,如果爬完的话会有2

爬取妹子图

这是之前写的一个简单爬取妹纸图的爬虫,下面是源代码: # -*- coding: utf-8 -*- import requests,time,urllib.request,os from multiprocessing import Processfrom lxml import etree #os.chdir("meizhu")切换工作目录print (os.getcwd())#查看当前工作目录 headers = {"User-Agent" : "M

爬取妹子图(requests + BeautifulSoup)

---恢复内容开始--- 刚刚入门爬虫,今天先对于单个图集进行爬取,过几天再进行翻页爬取. 使用requests库和BeautifulSoup库 目标网站:妹子图 今天是对于单个图集的爬取,就选择一个进行爬取,我选择的链接为:http://www.mzitu.com/123114 首先网站的分析,该网站有一定的反爬虫策略,所以应对就是加入headers(目前是小白,目前不知道具体为毛这样做) Hostreferer = { 'User-Agent':'Mozilla/4.0 (compatibl

Python又来爬取妹子图啦,一个T的硬盘都不够用

淘女郎爬虫,可动态抓取淘女郎的信息和照片. 需要额外安装的第三方库 requests pip install requests pymongo pip install pymongo 学习Python过程中会遇到很多问题,你可以到我们的 python学习交流群[七 三 五,九 三 四,八 四 一],基础,进阶.从企业招聘人才需求 到怎么学习python,和学习什么内容都有免费系统分享,让你无论是自学还是找相应的培训都能让你少走弯路.希望可以帮助你快速了解Python,学习python 模块功能

python 爬取妹子图

作为一个python还没入门的小白,搞懂这段代码实在是很不容易,还要去学html的知识(#黑脸) 因此我加上了注释,比较好读懂点 #coding=utf-8 import time import requests from bs4 import BeautifulSoup import os import sys if(os.name == 'nt'): print(u'你正在使用win平台') else: print(u'你正在使用linux平台') header = {'User-Agent

aiohttp aiofiles asyncio异步爬取妹子图

import aiohttp import asyncio import aiofiles import async_timeout from bs4 import BeautifulSoup import time import os async def aitaotu(sem, urllist):     headers = {         'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/web

使用request+bs4爬取所有股票信息

爬取前戏 我们要知道利用selenium是非常无敌的,自我认为什么反爬不反爬都不在话下,但是今天我们为什么要用request+bs4爬取所有股票信息呢?因为他比较原始,因此今天的数据,爬取起来也是比较繁琐的!接下来让我们emmmm....你懂得 爬取步骤 第一步:获取股票代码 1)我们通过这个链接去网易看一下具体的股票信息,下面这个网页是乐视网的股票信息http://quotes.money.163.com/trade/lsjysj_300104.html#01b07 2)上海证券交易所的官方网

抓取妹子图

import requests from bs4 import BeautifulSoup import os class mzitu(): def requst(self,url):#定义获取网页的函数 headers={'user-agent':"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36"}#模拟浏览器,以