Combining images and text from diverse sources

I'm currently engaged in some web scraping, attempting to extract both the image and text from different classes, and then showcase them together. Below is an example of the HTML snippet:

<div class="thumbnail">
  <div class="image>
    <img src="https://www.image.com/01.jpg">
  </div>
  <div class="text">
   Apple
  </div>
</div>
<div class="thumbnail">
  <div class="image>
    <img src="https://www.image.com/02.jpg">
  </div>
  <div class="text">
   Banana
  </div>
</div>

Within my loop, I aim to retrieve both the image and text content.

const group: { [key: string]: string } = {};
    this.$('.thumbnail .image img[src]')
      .get()
      .forEach(img => {
        const $img = this.$(img);
        // Null Check
        const imgText = $img
          .siblings('.text')
          .text()
          .trim();
        group[imgText] = location.origin + imgText;
      });
    Type = { 'Fruit': group };

Upon console logging, I notice that I can successfully retrieve the image but struggle to fetch the text. Various methods like siblings, next, closest have been attempted with no success. Moreover, I'm uncertain about what should replace location.origin.

Answer №1

Using jQuery, you have the ability to target all images on a page and extract valuable information such as the source URL and the related text from its nearest thumbnail.

$(".thumbnail img").each(function(index, image) {
    var src = $(image).attr('src');
    var text = $(image).closest(".thumbnail").find('.text').text().trim();
})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Displaying a static image on an HTML5 canvas without any movement

As a novice in canvas game development, I must apologize for my lack of knowledge. I have an image with dimensions 2048px width and 1536px height that needs to be placed within a canvas (the width and height vary on different devices). While I am able to ...

Error in Next.js only occurs during the production build when using styled components, tailwind CSS, and twin styling

After successfully building my application in development mode, I encountered an error when attempting a production build. The error appears on the image linked below: https://i.stack.imgur.com/sNr2v.png I suspect that the issue lies within the _document ...

The issue of receiving an undefined JSON response persists in an AJAX file upload scenario

Currently, I am utilizing Valum's Ajax File uploader for handling same-page file uploads. The issue I'm facing is that despite numerous attempts and variations in my script, I consistently receive "undefined" when trying to access responseJSON[&a ...

Is it necessary to have the script tag as the first tag in the body of the HTML?

I have a script file that needs to be included by third party implementors on their web pages. It is crucial for this script to be the first tag within the body element, like so: <body> <script type="text/javascript" src="/my_script.js"></ ...

Having trouble with Angular UI Select functionality?

I have integrated the angular ui select library from https://github.com/angular-ui/ui-select into my project. Instead of using the traditional select element, I am now utilizing the ui-select directive. This is a snippet of my code: <select class=" ...

Protected node.js REST API

I want to ensure the security of a restful API and aim to keep it simple and stateless. What is the best way to store, generate, and authenticate API keys? I was considering using node-uuid to generate keys, storing them in Redis, and then authenticating ...

The behavior of the input in React-bootstrap-daterangepicker is acting strangely

I'm encountering an issue while trying to integrate react-bootstrap-datepicker with input fields. The problem arises when I click on one of the inputs or press a key, causing the calendar to toggle (showing if hidden and vice versa). To reproduce this ...

Would you be able to clarify why the run() function is giving me an error when I try to return {1,3}, but it works fine when I return {a,b} in

I'm really confused as to why the return {1,3} in the run() function is throwing an error when it works just fine for return {a,b} in the fun() function function fun(){ let a = 10; let b = 20; return {a, b}; } ...

The Vue router-view is mistakenly loading the parent component instead of displaying its own content

Here is a simple route configuration: { path: '/', component: Home, }, This route configuration sets the path to the home page and loads the Home component when the path is '/'. However, I am encountering an issue where the Po ...

What is the most efficient way to retrieve the operating system's name and version using JavaScript?

I'm in the process of developing an object that will simplify accessing browser and system information by implementing a function. One particular function within this object is responsible for retrieving the operating system name and version, returnin ...

Mastering the Art of SQL Submission with PHP Ajax Requests

I've encountered an issue with a form that, upon validation, triggers two Ajax requests before submitting the data to create an entry in a MySQL database. The Ajax requests generate URLs to Google Documents using upload.php. However, when certain unk ...

What is the process for fetching the chosen outcome from a subsequent webpage using HTML?

How can I display selected cities on a different webpage after clicking a button? Currently, I can only get the results on the same page. For example, if a user selects NYC and Delhi, those cities should be displayed on another HTML page. ...

Collecting items within the #shadow-root HTML element

I'm currently attempting to retrieve matches and their corresponding odds from THIS PAGE using Python alongside Selenium Here is the code snippet I am working with: inner_page = driver.find_element(By.CSS_SELECTOR, "div#bt-inner-page").sha ...

one-time occurrence of $mdToast injection within a parent class

Seeking advice on how to efficiently place a single instance of $mdToast (from Angular Material) into a base class (Typescript). In my UI, I have five tabs with separate controller instances and it seemed logical to centralize the $mdToast declaration in a ...

Discover the ins and outs of utilizing the Google+ Hangout API specifically for chatting purposes

I am currently working on a webpage and I want to include a hangout button that will allow users to connect with me and start chatting automatically when clicked. Here is the code I have so far: <script src="https://apis.google.com/js/platform.js" asy ...

After refreshing the div, Ckeditor fails to display

I'm currently facing an issue with assigning an initial value to a ckeditor using a jQuery adapter in PHP. Whenever jQuery refreshes the div containing the ckeditor, the ckeditor disappears. Here is how I've defined the editor: $ckeditor = new ...

Having issues with transferring data from ajax to php

Why is my data not being sent from the form to the PHP script? Here is my JavaScript file: var newMessage = { 'name': nameInput.val(), 'email': emailInput.val(), 'message' ...

Grid items in Material UI are not positioned next to each other

I am encountering an issue with the Grid component in material-ui. The grid items are currently stacking below each other instead of beside each other, and I'm unsure of what is causing this behavior. My intention is for the grid items to stack on top ...

What is the best method for retrieving text that does not contain tags (such as text

My challenge involves storing headings and paragraphs into separate arrays, but I am struggling with handling text between <br>. Below is my HTML code and Python snippet: <p><strong>W Ognisku</strong><br>W londyńskim Ognisku ...

A guide on retrieving the selected option from a dropdown menu with React Material UI

Utilizing Material UI React, I am constructing a dropdown menu containing various options. My concern is: if I choose two options from different dropdowns within the menu, how can I intercept or store which option was selected? Below is a snippet of my co ...