Search image in HTML code

Asked

Viewed 2,691 times

1

In a form, I will perform an image search on Bing (in the background) so:

titulo.Text = "sou doador"
string minhaBusca = titulo.Text + " app";
minhaBusca = "http://www.bing.com/images/search?q=" + minhaBusca;

Then I want to get the source code of that page the link of the first 3 images, for this I need to search the source code by: .jpg or .png.
I know there’s a pattern on the page on Bing, that the image link is inside a div item:

<div class="item"><a href="http://vmulher4.vila.to/interacao/4004739/doacao-de-sangue-eu-sou-um-doador-e-voce-57209-1.jpg" class="thumb" target="_blank" h="ID=images,5012.1">

How do I get the three links from href and store in an array or variable? And to display them in my form?

  • i downloaded the page you put as a link and did not find the structure you point... I found a div with the class dg_u and inside it the picture’s Thumb...

  • On line 17, you have the div item, really noticed also that all the image has the class thumb then it would be easier to catch the element, but how? kk

1 answer

1


First you need to install a NUGET package in your project, called Htmlagilitypack - Nuget, he who will help you in this!.

After that the Htmlweb of the code below will be available in your project for use.

Use as follows:

  <img src="valor da lista aqui dentro ex: http://tse4.mm.bing.net/th?id=OIP.Meea6e108be6309f544bab7a1....">

Code in c#:

titulo.Text = "sou doador";
string minhaBusca = titulo.Text + " app";
string url = "http://www.bing.com/images/search?q=" + minhaBusca;
HtmlWeb web = new HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = web.Load(url);


       var div = doc.DocumentNode.SelectSingleNode("//div[@class='imgres']"); //pega o que tendo dentro da div que tem a classe 'imgres'

        List<string> ListaImagens = new List<string>(); // cria a lista que vai guardar os links
        if (div != null) // verifica se a div achada anteriormente nao e nula.
        {

            foreach (HtmlNode type in div.Descendants("a")) // pego todos as tags <a> dentro da div, e percorre cada um (objeto type)
            {
                ListaImagens.Add(type.Element("img").Attributes["src2"].Value); //adiciono na lista o elemento 'img' dentro de 'a', e pego o valor do atributo 'src2' do elemento 'img'
            }
        }
        else
        {
            MessageBox.Show("Ops! Não retornou imagem nenhuma!");
        }

Remember to be using the following assemblies up there:

using HtmlAgilityPack; //do pacote nuget!
using System;
using System.Collections.Generic;
using System.Windows.Forms;

The values will be inside the Image List list list. Ai is just use as I showed you in the HTML IMG tag example

  • Very good, I had thought about using this package, but I didn’t know where to start.. this one "//div[@class='imgres']" want to say what to look for on the page? Example, I want to take the parameter href of all (or first three) tags a who have the class thumb how would you like it? Where would I change? This I didn’t understand right

  • This code is a little complicated... because you need to use the methods of the package I gave you to find the link. entering us, etc. I will comment as much as I can explain to you, but the code is taking the images that you mentioned to me and putting in the list, just take the first 3 elements of the list. And use as I told you. In Bing, it does not provide the image in JPEG, PNG, etc... just a reference link. which is a bit boring.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.