百度图片爬虫

import requests
import re
import os

word =input('请输入要爬取的内容:')
if not os.path.exists(word):
    os.mkdir(word)

i=0

num=int(input("请输入要爬取的页数(每页30张图片):"))

headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'}
urls=['https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=%E5%A3%81%E7%BA%B8&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=&hd=&latest=&copyright=&word='+word+'&pn={}'.format(j) for j in range(30,num*30+1,30)]

def get_img(url):
    response=requests.get(url,headers)
    content=response.content.decode('utf-8')
    img_urls=re.findall('"thumbURL":"(.*?)"',content,re.S)
    global i
    for img_url in img_urls:
        response=requests.get(img_url,headers)
        content=response.content
        with open(word+'/'+'{}.jpg'.format(i+1),'wb') as f:
            f.write(content)
            print("正在爬取第{}张图片".format(i+1))
        i+=1
for url in urls:
    get_img(url)

参考网址

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值