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

本站现有博文332篇,共被浏览895371

本站已经建立2597天!

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