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

你可能感兴趣的:(python,python)