[爬个图]从某度图片获取数据

爬个某度的图片,没啥说的,直接用吧

import requests
import re
import time
import os
import urllib.parse
import json

# 要爬的页数
page_num=1 
photo_dir="C:\\Users\\hp\\Desktop\\getBaiduImg\\"

def getThumbImage(word):
    num=0
    url = "http://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word={0}&pn={1}"
    while num<50:

        page_url=url.format(urllib.parse.quote(word),num*page_num)
        print(page_url)
        response=requests.get(page_url)
        pic_urls=re.findall('"thumbURL":"(.*?)",',response.text,re.S)
        
        if pic_urls:
        
            for pic_url in pic_urls:
                name=pic_url.split('/')[-1]
                print(pic_url)
                headers={
                    "Referer":page_url,
                }
                html=requests.get(pic_url,headers=headers)
                with open(os.path.join(word_dir,name),'wb')as f:
                    f.write(html.content)
        num=num+1

def getThumb2Image(word):
    num=0
    url = "https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord={0}&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=&hd=&latest=&copyright=&word={0}&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&expermode=&force=&pn={1}&rn="+str(page_num)+"&gsm=1e&1552975216767="
    while num<50:

        page_url=url.format(urllib.parse.quote(word),num*page_num)
        print(page_url)
        response=requests.get(page_url)
        pic_urls=re.findall('"thumbURL":"(.*?)",',response.text,re.S)
        for pic_url in pic_urls:
            name=pic_url.split('/')[-1]
            print(pic_url)
            headers={
                "Referer":page_url,
            }
            html=requests.get(pic_url,headers=headers)
            with open(os.path.join(word_dir,name),'wb')as f:
                f.write(html.content)
        num=num+1
        print(num)
        

if __name__ == "__main__":
    word = "人脸"
    word_dir=os.path.join(photo_dir,word)
    if not os.path.exists(word_dir):
        os.mkdir(word_dir)
    getThumb2Image(word)

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值