Warning: Undefined array key "night" in /www/wwwroot/dhaomu.com/wp-content/themes/b2/header.php on line 18

Python初学...爬取百度图库图片

^ 功能就是输入一个想要爬取的字符串比如“超级玛丽”,程序会自动分页加载一直爬取,直到没有图片

import requests
import os
import re
pn = 0 #从哪个图片下标开始 
rn = 30 #每次多少张图片 pn与rn参数是在Google开发者工具里面找到的两个参数。。。很难找,坑爹啊啊啊
global number
#中文的话文件夹名字会乱码
name = "chaojimali"
def getImagePath(pn = 0):
    try:
        url = '''http://image.baidu.com/search/index?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word=%s&pn=%d&rn=%d''' % (name,pn,rn)
        headers = {"user_agent": "Mozilla/5.0"}
        response = requests.get(url, headers=headers)
        # source = BeautifulSoup(response.content, 'lxml', from_encoding='utf-8')
        # paths = source.find_all("objURL")
        content = response.content
        #正则验证
        # links = re.findall('"((http|ftp)s?://.*?.(png|jpg|jpeg|gif))"', response.content)
        links = re.findall('"((http|ftp)s?://.*?.(png|jpg|jpeg|gif))"',content)
        if not os.path.exists(name):
            os.mkdir(name)
        for path in links:
            imgPath = path[0]
            image = requests.get(imgPath)
            #返回码为200才去下载
            if image.status_code != 200:
                continue
            print imgPath
            try:
                #尝试下载图片,失败了跳过这张图
                open(name + os.sep + (imgPath[imgPath.rfind("/"):]), "wb").write(image.content)
            except:
                continue
        pn+=rn
        getImagePath(pn)
    except:
        pn += rn
        getImagePath(pn)
#开始
getImagePath(pn)

温馨提示:
1.本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:duhaomu@163.com,我们将第一时间处理!
2.资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持。
3.所有资源仅限于参考和学习,版权归原作者所有,更多请阅读网站声明

给TA赞赏
共{{data.count}}人
人已赞赏
未整理

【寒冰财商】投资的艺术第二季

2021-5-10 0:00:00

未整理

Layui前端的多合一收款码在线生成系统源码_附多套前端UI模板

2021-5-12 0:00:00

0 条回复 A文章作者 M管理员

Warning: Trying to access array offset on value of type null in /www/wwwroot/dhaomu.com/wp-content/themes/b2/functions.php on line 3914

Warning: Trying to access array offset on value of type null in /www/wwwroot/dhaomu.com/wp-content/themes/b2/functions.php on line 3914
----《》
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索