python抓取百度妹子图

from pyquery import PyQuery as pq
from lxml import etree

请求库

import requests

count = 1

def Download_image(page):
global count

# 请求网页的url
# https://i.meizitu.net/2019/05/02a02.jpg
url = 'https://www.mzitu.com/182610/{}'.format(page)

# 请求头
# 图片的请求头
headers = {
    'Referer': url,
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3676.400 QQBrowser/10.4.3505.400'
}

# 主页的请求头
headers_host = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3676.400 QQBrowser/10.4.3505.400'
}


# 模拟浏览器对服务器发送请求
response = requests.get(url, headers=headers_host).content.decode('utf-8')

# 打印一个状态码
# print(response)
# 数据提取
html = etree.HTML(response)
# xpath的数据提取规则
src_url = html.xpath("//div[@class='main-image']/p/a/img/@src")

for i in src_url:
    # 发起二次请求 请求图片的链接
    image_data = requests.get(i, headers=headers)
    # print(image_data)
    # 保存图片
    # 电脑可以识别的图片  二进制  16进制   a 追加  b进制文件的读写
    with open('妹子图/{}.jpg'.format(count), 'ab') as f:
        f.write(image_data.content)

    count += 1

page = int(input(‘请输入你想抓取的页数:’))
for i in range(1, page):
Download_image(i)

作者:永简
来源:CSDN
原文:https://mp.csdn.net/mdeditor/93479227
版权声明:本文为博主原创文章,转载请附上博文链接!
更多资料请加:QQ3541108857

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值