C++ có thể lấy hình ảnh của trang web được không?

Chào mọi người.
Hiện em đang là newbie và em đang có một ý tưởng như thế này.
Em muốn lấy mình ảnh các lá bài (YUGIOH :))) từ 1 trang web là ygoprodeck.com. Thì mỗi lá bài yugioh đều có 1 đường link trỏ tới file lưu ảnh của nó.
Ví dụ lá Dark Magician phù thủy áo đen nó được lưu trữ hình ảnh của nó ở link https://ygoprodeck.com/pics/36996508.jpg
thì công thức của link nó như sau : <https://ygoprodeck.com/pics/ > + <code ảnh> <.jpg>
Hiện tại em đã biết cách lấy code của anh.
Em cảm ơn.
Các cao nhân cho em hỏi là có cách nào để tự động truy cập vào link của bức ảnh và tải nó về không ạ?
Kiểu em muốn tạo 1 tool tự động ý ạ, nhưng mà khả năng của em thì chưa thể (bây giờ em chỉ biết 1 ít OOP của c++ cơ bản và vài cái thuật toán như sort, tham lam, hay QHD cơ bản thôi,…)
Mọi người cho em hỏi là để viết được tool như thế thì cần tìm tòi về cái gì vậy ạ?

em xài thư viện curl hoặc cpr để tải từ trên web về nha :V
xài thư viện cpr download dễ lắm :V

std::ofstream of("1.jpg", std::ios::binary);
cpr::Response r = cpr::Download(of, cpr::Url{"http://www.httpbin.org/1.jpg"});
std::cout << "http status code = " << r.status_code << "\n";

em cần tìm tòi cách build chương trình C++ với các thư viện ngoài :V

em xài msvc để build thì học cách xài vcpkg, xài mingw để build thì down msys2 về xài

8 Likes

Anh ơi, vậy làm sao để add thư viện này bvaayj anh, mình include <> như bỉnh thường hay là phải tải nó về như PythON?

em xài trình biên dịch gì :V

daj em dùng ide eclipse bản c++

ai ghét em nên chỉ em xài eclipse hả :joy: Install VS Community 2022 đi :V :V
xong cài thêm Git, CMake

xong rồi tạo thư mục project, ví dụ ở đây lấy tên là download-file-from-internet tạo ở thư mục D:/prog, gồm 3 file src/main.cpp, CMakeLists.txt, vcpkg.json

D:/prog/download-file-from-internet
|
`-- src
|   |
|   `-- main.cpp
|
`-- CMakeLists.txt
|
`-- vcpkg.json

file src/main.cpp (ở đây anh ko xài cpr::Download, em tự sửa lại nha :V)

#include <iostream>
#include <cpr/cpr.h>
#include <fmt/format.h>
#include <fstream>

auto main(int argc, char** argv) -> int {
    const std::string id = "36996508.jpg";
    if (std::ifstream in{id}) {
        fmt::print("File {} exists\n", id);
        return 0;
    }
    cpr::Response r = cpr::Get(cpr::Url{fmt::format("https://ygoprodeck.com/pics/{}", id)});
    fmt::print("Status: {}\nContent type: {}\n", r.status_code, r.header["content-type"]);
    if (r.status_code == cpr::status::HTTP_OK) {
        std::ofstream out{id, std::ios::binary};
        if (out) out.write(r.text.data(), r.text.size());
        fmt::print("Downloaded {} bytes, written {} bytes to {}\n", r.downloaded_bytes, r.text.size(), id);
    }
    return 0;
}

file CMakeLists.txt

cmake_minimum_required(VERSION 3.19 FATAL_ERROR)

project(download-file-from-internet CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

add_executable(${PROJECT_NAME} src/main.cpp)
# cpr
find_package(cpr CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE cpr::cpr)
# fmt
find_package(fmt CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE fmt::fmt-header-only)

set_property(DIRECTORY PROPERTY VS_STARTUP_PROJECT ${PROJECT_NAME})

file vcpkg.json

{
  "name": "download-file-from-internet",
  "version": "0.0.1",
  "dependencies": [
    "cpr",
    "fmt"
  ]
}

xong mở powershell vào thư mục này, gõ lệnh

git init
git submodule add https://github.com/microsoft/vcpkg vcpkg
mkdir build
cd build
cmake .. -G"Visual Studio 17 2022" -A x64 -DCMAKE_TOOLCHAIN_FILE=D:/prog/download-file-from-internet/vcpkg/scripts/buildsystems/vcpkg.cmake

lệnh git submodule add chắc chạy tầm 10-30 giây :V
lệnh cmake configure kia nó sẽ Running vcpkg install trước rồi mới config project sau, bước vcpkg install này có thể hơi lâu, chắc tầm 5-15 phút :V được cái tạo project khác nó có cache nó install lẹ hơn :triumph:

là xong, vào thư mục build vừa tạo đó mở download-file-from-internet.sln lên là được.

vcpkg có gần 2000 thư viện lớn nhỏ khác nhau, em cần thư viện nào cứ gõ vcpkg search <tên từ khóa> nó ra thư viện nào muốn xài thử thì cứ thêm vào vcpkg.json chỗ dependencies, vd thêm sqlite3 thì thêm vào:

  ...
  "dependencies": [
    "cpr",
    "fmt",
    "sqlite3"
  ]

rồi build project, cmake nó sẽ tự động gọi vcpkg install sqlite3. Chạy xong tìm xem nó chỉ usage add vào cmake như thế nào, ở đây là mấy dòng

sqlite3 provides CMake targets:
    # this is heuristically generated, and may not be correct
    find_package(unofficial-sqlite3 CONFIG REQUIRED)
    target_link_libraries(main PRIVATE unofficial::sqlite3::sqlite3)

rồi lấy 2 dòng này thêm vào file CMakeLists.txt, sửa cái target main lại thành ${PROJECT_NAME}

add_executable(${PROJECT_NAME} src/main.cpp)
# cpr
find_package(cpr CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE cpr::cpr)
# fmt
find_package(fmt CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE fmt::fmt-header-only)
# sqlite3
find_package(unofficial-sqlite3 CONFIG REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE unofficial::sqlite3::sqlite3)

chạy build project lại lần nữa là là xài sqlite3 được.

muốn thêm file .h, .cpp thì tạo trước mấy file này rồi thêm vào CMakeLists.txt nha :V hơi thủ công tí :V Ví dụ tạo 2 file mới myclass.hmyclass.cpp ở thư mục src thì thêm vào

add_executable(${PROJECT_NAME}
  src/main.cpp
  src/myclass.h
  src/myclass.cpp
)
8 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?