EADST

Python: Obtain Baidu Images Using Web Crawler

Python: Obtain Baidu Images Using Web Crawler.

Here is the main code.

# -- coding: utf-8 --
import os
import re
import time
import requests

class CarCollect():

def __init__(self, path='./name.txt'):
    self.num = 1
    self.class_number = 0
    self.line_list = []
    with open(path, encoding='utf-8') as file:
        self.line_list = [k.strip() for k in file.readlines()]
        self.class_number = int(self.line_list[0])
        self.line_list = self.line_list[1:]

def dowmload_picture(self, html, keyword, save_path):
    pic_url = re.findall('"objURL":"(.*?)",', html, re.S)  # get image url
    print('Finding keyword: ' + keyword + ' images, start downloading...')
    for each in pic_url:
        print('='*60)
        print('Downloading ' + keyword + ' number ' + str(self.num) + ' image, url: ' + str(each))
        try:
            if each:
                pic = requests.get(each, timeout=7)
                string = save_path + r'\\' + keyword + '_' + str(self.num) + '.jpg'
                if len(pic.content) > 10000: # img size > 10k
                    with open(string, 'wb') as fp:
                        fp.write(pic.content)
                        self.num += 1
        except BaseException:
            print('error, cannot download')
        if self.num > self.class_number:
            break

def __call__(self):
    headers = {
        'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
        'Connection': 'keep-alive',
        'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0',
        'Upgrade-Insecure-Requests': '1'
    }
    session = requests.Session()
    session.headers = headers

    for word in self.line_list:
        # create a folder
        save_path = word + '_file'
        time_now = time.strftime("%Y%m%d_%H%M%S", time.localtime())
        save_path += "_" + time_now
        os.mkdir(save_path)
        # get images
        url = 'https://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word=' + word + '&pn='
        image_number = 0
        self.num = 1
        while image_number < self.class_number:
            try:
                result = session.get(url + str(image_number), timeout=10, allow_redirects=False)
                self.dowmload_picture(result.text, word, save_path)
            except:
                print('Internet error')
            image_number += 60

if name == 'main': path = './keywords.txt' car_collect = CarCollect(path) car_collect() print('Done.')

Here is the text file, keywords.txt. The first line is the number we want to obtain from each keyword. The following lines are the keywords.

20
Dog
Cat

相关标签
About Me
XD
Goals determine what you are going to be.
Category
标签云
JSON transformers CEIR WAN VSCode DeepStream GGML 第一性原理 BeautifulSoup torchinfo Harness FastAPI 域名 Safetensors 版权 XML 云服务器 腾讯云 TensorRT NameSilo IndexTTS2 uwsgi Github hf API网关 MD5 继承 git Miniforge Windows FP8 GoogLeNet InvalidArgumentError Ptyhon Search FP32 git-lfs CSV ResNet-50 Plate Math Datetime ms-swift PDB HaggingFace C++ Pickle HuggingFace Anaconda PyCharm 签证 Diagram DeepSeek tqdm 净利润 Baidu Statistics Dataset llama.cpp Augmentation LeetCode TTS CAM Attention Data API CV Llama uWSGI 报税 图形思考法 Jetson Tracking Web Template Pytorch PyTorch GIT 算法题 Bin Conda FlashAttention 顶会 NLTK CUDA ModelScope Paddle SQLite Linux SQL Review Rebuttal 财报 Gemma EXCEL 关于博主 自我改进 OpenCV Use Knowledge OCR PDF Hungarian 公式 diffusers Animate Sklearn Card Docker Firewall TensorFlow 论文速读 PIP Vim v2ray Tiktoken Interview Distillation Random Algorithm Color UNIX Pillow News Cloudreve Plotly Website printf Proxy Qwen Transformers SVR Freesound v0.dev Shortcut Zip Michelin Google LLAMA Markdown Jev Git 音频 Qwen2.5 Claude Mixtral Streamlit icon Bert BTC Magnet logger Python VPN 阿里云 飞书 SPIE CC YOLO Crawler Quantization RAR 强化学习 Hotel Land VGG-16 ONNX Domain mmap LaTeX Breakpoint 长期记忆 Permission 论文 XGBoost 搞笑 Translation WebCrawler Logo QWEN Bitcoin GPTQ TSV Ubuntu Pandas Vmess 证件照 scipy Password Nginx Base64 GPT4 OpenAI LoRA Django Tensor FP64 Numpy Paper Bipartite 递归学习法 Input SAM tar BF16 AI ChatGPT LLM Hilton Qwen2 Heatmap CTC Quantize RL Disk Jupyter Clash Image2Text 多线程 FP16 多进程 Agent Food Video Excel COCO UI CLAP 图标 RGB NLP
站点统计

本站现有博文339篇,共被浏览996011次

本站已经建立2706天!

热门文章
文章归档
回到顶部