手把手教你爬取清纯小姐姐私房照,小孩子别学

Posted 诗一样的代码

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了手把手教你爬取清纯小姐姐私房照,小孩子别学相关的知识,希望对你有一定的参考价值。

手把手教你爬取清纯小姐姐私房照,小孩子别学

先上效果图

import re
import requests
import os

头文件:
因为爬虫需要用到请求网络部分,所以需要这两个包,没有的话自行下载即可。这个可以直接用pip安装。如果连pip都不懂,那就只能学习一下python基础了。

请求头:

 headers = 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/84.0.4147.125 Safari/537.36'

完整的请求:

url = 'https://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word=='+name+'+&pn='+str(i*30)
        result = requests.get(url,headers=headers)
        dowmloadPic(result.content.decode(), name)

正则表达式:

 pic_url = re.findall('"objURL":"(.*?)",',html,re.S)

下载图片:

 fp = open(dir, 'wb')
        fp.write(pic.content)
        fp.close()

完整代码:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import re
import requests
import os


def dowmloadPic(html, keyword,i):
    pic_url = re.findall('"objURL":"(.*?)",',html,re.S)
   
    abc=i*60
    print('找到关键词:' + keyword + '的图片,现在开始下载图片...')
    for each in pic_url:
        print('正在下载第' + str(abc) + '张图片,图片地址:' + str(each))
        try:
            pic = requests.get(each, timeout=10)
        except requests.exceptions.ConnectionError:
            print('【错误】当前图片无法下载')
            continue

        dir = r'D:\\image\\i' + keyword + '_' + str(abc) + '.jpg'
        if not os.path.exists('D:\\image'):
            os.makedirs('D:\\image')
        
        fp = open(dir, 'wb')
        fp.write(pic.content)

.......完整源码,请关注公众号,后台回复领取


视频讲解传送门

更多干货内容,请移步到公众号:诗一样的代码

以上是关于手把手教你爬取清纯小姐姐私房照,小孩子别学的主要内容,如果未能解决你的问题,请参考以下文章

手把手教你爬取清纯小姐姐私房照,小孩子别学

手把手教你爬取清纯小姐姐私房照,小孩子别学,后篇

[视频教学,讲解]手把手教你爬取清纯小姐姐私房照

手把手带你爬取小姐姐私房照,准备好你的纸!

手把手带你爬取百度美女图片,Python练手项目!

520表白神器 —— 一个python就够了(附爬取小姐姐私房照的程序哦)