抓取百度图片(python)

1.功能分析

通过python搜索想要的百度图片,并下载保存。

2.分析百度图片网页

通过谷歌浏览器打开百度图片翻页版,点击f12分析可知网页地址为

https://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word="+搜索内容+"&pn="+20*页码(页码由0开始)+"&gsm=0&ct=&ic=0&lm=-1&width=0&height=0
查看源码可知图片地址为
"thumbURL":"需要的地址"
3.编写代码

通过简单的分析后,我们就可以进行代码编写

首先需要手动创建一个文件夹download

引用os模块,通过以下代码使接下来的图片保存在download文件夹下

os.chdir("d:\\download")

将要搜索的内容保存在st

st=input("图片搜索:")

接下来引用requests模块,保存网页,并通过正则表达式,提取图片地址

m=0
url="https://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word="+st+"&pn="+str(20*m)+"&gsm=0&ct=&ic=0&lm=-1&width=0&height=0"
http=requests.get(url).text
photore=re.compile('"thumbURL":"(.*?)"')
co=photore.findall(http)

将图片保存下来

a=1
for i in co:
        try:
            pic=requests.get(i)
            f=open(str(a)+".jpg","wb")
            f.write(pic.content)
            f.close()
        except:
            print("图片无法下载")
            continue
        a=a+1

通过while循环,可以将每一页上的图片全部提取并保存

以下是全部代码

import requests,os,re
os.chdir("d:\\download")
m=0
a=1
st=input("图片搜索:")
while m<3:
    url="https://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word="+st+"&pn="+str(20*m)+"&gsm=0&ct=&ic=0&lm=-1&width=0&height=0"
    http=requests.get(url).text
    photore=re.compile('"thumbURL":"(.*?)"')
    co=photore.findall(http)
    for i in co:
        try:
            pic=requests.get(i)
            f=open(str(a)+".jpg","wb")
            f.write(pic.content)
            f.close()
        except:
            print("图片无法下载")
            continue
        a=a+1
    m=m+1
4.运行效果

运行效果如下:




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值