2

Here is code which search web with bing, query is man,it will find IDs,Titles,Description,Urls...Is possible to filter only Urls?

use LWP::UserAgent;
use YAML::Tiny;
use JSON;


$key= 'KEY';


$href= 'https://api.datamarket.azure.com/Bing/Search/v1/Web?Query=%27ip%3A213.149.103.135%20php%3Fid%3D%27&$format=JSON';
$ua= LWP::UserAgent->new(keep_alive);
$ua->credentials("api.datamarket.azure.com".':443', '', '', $key);

$resp= $ua->get($href);



print Dump($resp->content);

Output:

--- "{\"d\":{\"results\":[{\"__metadata\":{\"uri\":\"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=\\u0027ip:213.149.103.135 php?id=\\u0027&$skip=0&$top=1\",\"type\":\"WebResult\"},\"ID\":\"31ff54d4-3b0b-4035-8d2b-ecadf6181745\",\"Title\":\"Montenegro Business Alliance\",\"Description\":\"Zakonska regulativa. Zakoni 2012. Zakon o akcizama; Zakon o budzetu Crne Gore za 2012.godinu; Zakon o detektivskoj djelatnosti; Zakon o elektronskim medijima\",\"DisplayUrl\":\"www.visit-mba.org/cg/strana.php?id=37\",\"Url\":\"http://www.visit-mba.org/cg/strana.php?id=37\"},{\"__metadata\":{\"uri\":\"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=\\u0027ip:213.149.103.135 php?id=\\u0027&$skip=1&$top=1\",\"type\":\"WebResult\"},\"ID\":\"e8e8da15-1adc-46a8-b16d-d0c65451c47e\",\"Title\":\"Organizaciona šema - CETI - Centar za ekotoksikološka ...\",\"Description\":\"© 2011 Centar za ekotoksikološka ispitivanja Crne Gore. By Creative Studio LASSO\",\"DisplayUrl\":\"www.ceti.co.me/podkategorija.php?id=9\",\"Url\":\"http://www.ceti.co.me/podkategorija.php?id=9\"},{\"__metadata\":{\"uri\":\"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=\\u0027ip:213.149.103.135 php?id=\\u0027&$skip=2&$top=1\",\"type\":\"WebResult\"},\"ID\":\"3d0571bc-b0ae-40b6-a9f7-72539b112514\",\"Title\":\"Montenegro Business Alliance\",\"Description\":\"MBA Kotor: Stari grad 372: Tel/fax: 032 323 823; 304 208: E-mail: mbako@t-com.me\",\"DisplayUrl\":\"www.visit-mba.org/cg/strana.php?id=30\",\"Url\":\"http://www.visit-mba.org/cg/strana.php?id=30\"},{\"__metadata\":{\"uri\":\"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=\\u0027ip:213.149.103.135 php?id=\\u0027&$skip=3&$top=1\",\"type\":\"WebResult\"},\"ID\":\"4f263448-1cef-4ec7-a55b-1722f994c3f5\",\"Title\":\"Odbor direktora - CETI - Centar za ekotoksikološka ispitivanja ...\",\"Description\":\"Odbor direktora. Ana Mišurović, spec. toks. hem, predsjednik Odbora direktora; Ratka Strugar, diplomirani pravnik, pomoćnik ministra za informaciono društvo i ...\",\"DisplayUrl\":\"www.ceti.co.me/podkategorija.php?id=7\",\"Url\":\"http://www.ceti.co.me/podkategorija.php?id=7\"},{\"__metadata\":{\"uri\":\"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=\\u0027ip:213.149.103.135 php?id=\\u0027&$skip=4&$top=1\",\"type\":\"WebResult\"},\"ID\":\"d11ee035-035a-441f-a610-be360a964da0\",\"Title\":\"KOMISIJA ZA UTVRDJIVANJE KONFLIKTA INTERESA\",\"Description\":\"Komisija za utvrđivanje konflikta interesa. Tel:(+382 20) 621 124. Tel/fax:(+382 20) 620 540. Adresa Crnogorskih serdara b.b\",\"DisplayUrl\":\"konfliktinteresa.me/funkcioneri/EvidFunPrijave.php?ID=Ozo-NTU8...\",\"Url\":\"http://konfliktinteresa.me/funkcioneri/EvidFunPrijave.php?ID=Ozo-NTU8OCYyPz43Og,,\"},{\"__metadata\":{\"uri\":\"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=\\u0027ip:213.149.103.135 php?id=\\u0027&$skip=5&$top=1\",\"type\":\"WebResult\"},\"ID\":\"fb106c3b-ecd2-4c77-ae3b-dba8e7f835ab\",\"Title\":\"KOMISIJA ZA UTVRDJIVANJE KONFLIKTA INTERESA\",\"Description\":\"Komisija za utvrđivanje konflikta interesa. Tel:(+382 20) 621 124. Tel/fax:(+382 20) 620 540. Adresa Crnogorskih serdara b.b\",\"DisplayUrl\":\"konfliktinteresa.me/funkcioneri/EvidFunPrijave.php?ID=Oz4-NDU9...\",\"Url\":\"http://konfliktinteresa.me/funkcioneri/EvidFunPrijave.php?ID=Oz4-NDU9MyYzPz43Og,,\"},{\"__metadata\":{\"uri\":\"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=\\u0027ip:213.149.103.135 php?id=\\u0027&$skip=6&$top=1\",\"type\":\"WebResult\"},\"ID\":\"5b51e876-74b5-4f05-a7da-f780d600a65e\",\"Title\":\"Montenegro Stars Hotel Group\",\"Description\":\"Visit Montenegro Stars hotels - your guide through best hotels in Montenegro\",\"DisplayUrl\":\"www.montenegrostars.com/new/pretraga.php?id_jez=2\",\"Url\":\"http://www.montenegrostars.com/new/pretraga.php?id_jez=2\"},{\"__metadata\":{\"uri\":\"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=\\u0027ip:213.149.103.135 php?id=\\u0027&$skip=7&$top=1\",\"type\":\"WebResult\"},\"ID\":\"9b18c3f0-a0bb-48cc-a6ca-10a8261a3086\",\"Title\":\"Montenegro Stars Hotel Group\",\"Description\":\"Montenegro Stars Hotel Group is the leading hotel company in Montenegro. The company owns three hotels, hotel Splendid Conference and Spa Resort and hotel Montenegro ...\",\"DisplayUrl\":\"www.montenegrostars.com/new/index.php?id_jez=1&id=37\",\"Url\":\"http://www.montenegrostars.com/new/index.php?id_jez=1&id=37\"},{\"__metadata\":{\"uri\":\"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=\\u0027ip:213.149.103.135 php?id=\\u0027&$skip=8&$top=1\",\"type\":\"WebResult\"},\"ID\":\"863ed128-48b2-412e-a737-d3b38bfa7943\",\"Title\":\"Vila Zelenbor - Hotel Bjelobor\",\"Description\":\"Vila Zelenbor je nešto najljepše što Vam se može dogoditi na Žabljaku. Organizujemo ručkove u prirodi na obali prelijepog Zminičkog jezera. Ukoliko st\",\"DisplayUrl\":\"www.hotelbjelobor.com/index.php?id=vila-zelenbor\",\"Url\":\"http://www.hotelbjelobor.com/index.php?id=vila-zelenbor\"},{\"__metadata\":{\"uri\":\"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=\\u0027ip:213.149.103.135 php?id=\\u0027&$skip=9&$top=1\",\"type\":\"WebResult\"},\"ID\":\"d7df8b53-9ba6-4242-92b8-111d5cb91510\",\"Title\":\"Kontakt - Hotel Bjelobor - Hoteli Bjelobor i Zlatni Bor i Vila ...\",\"Description\":\"Kontakt i rezervacije ... Žabljak. Provedite nezaboravni odmor u Žabljaku, jednom od najlepših predjela Crne Gore.\",\"DisplayUrl\":\"www.hotelbjelobor.com/index.php?id=kontakt\",\"Url\":\"http://www.hotelbjelobor.com/index.php?id=kontakt\"},{\"__metadata\":{\"uri\":\"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=\\u0027ip:213.149.103.135 php?id=\\u0027&$skip=10&$top=1\",\"type\":\"WebResult\"},\"ID\":\"af256dae-fcbd-449b-96e0-f67ea3885c66\",\"Title\":\"Ekonomski fakultet - Podgorica\",\"Description\":\"Any additional information and conditions may be found on the link: http://www.delmne.ec.europa.eu/code/navigate.php?Id=131, and my assistant Cécile Desruelle ...\",\"DisplayUrl\":\"www.ekonomija.ac.me/opsirnije.php?opsirnije=6369\",\"Url\":\"http://www.ekonomija.ac.me/opsirnije.php?opsirnije=6369\"},{\"__metadata\":{\"uri\":\"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=\\u0027ip:213.149.103.135 php?id=\\u0027&$skip=11&$top=1\",\"type\":\"WebResult\"},\"ID\":\"7e6d5db9-abfa-47f9-8432-737dff77f56b\",\"Title\":\"fotokopirnica STUDENT\",\"Description\":\"fotokopiranje korichenje stampa vizit karte printanje papir ... Fotokopirnica Student Br. tel: 020 265-293 mob. +382 69 845-152 Adresa: Hol zgrade Tehničkih ...\",\"DisplayUrl\":\"www.fotostudent.co.me/kalkulator2.php?id=1\",\"Url\":\"http://www.fotostudent.co.me/kalkulator2.php?id=1\"},{\"__metadata\":{\"uri\":\"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=\\u0027ip:213.149.103.135 php?id=\\u0027&$skip=12&$top=1\",\"type\":\"WebResult\"},\"ID\":\"833e6f58-a072-4876-8024-da000037c6bc\",\"Title\":\"Tourist info - T-Mobile Montenegro\",\"Description\":\"Nelle grigie montagne montenegrine, dove migliaia di anni fa gli scoscesi pendii del Durmitor e del Vojnik si scontrarono tra loro durante i grandi movimenti ...\",\"DisplayUrl\":\"www.infot.co.me/ita/n.php?id=2\",\"Url\":\"http://www.infot.co.me/ita/n.php?id=2\"},{\"__metadata\":{\"uri\":\"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=\\u0027ip:213.149.103.135 php?id=\\u0027&$skip=13&$top=1\",\"type\":\"WebResult\"},\"ID\":\"a69ca711-0d35-4a69-adce-7d598d2fa908\",\"Title\":\"incognito\",\"Description\":\"Dobrodošli na kratki virtuelni izlet kroz marketing agenciju Incognito Podgorica iz Crne Gore. Na stranicama koje slijede imaćete uvid u incognićanski ...\",\"DisplayUrl\":\"www.incognito.co.me\",\"Url\":\"http://www.incognito.co.me/\"}]}}"
4

1 に答える 1

2

If you only want the URLs, you need to first use JSON (you have that already) to turn your return value into a Perl data structure.

my $data = decode_json($resp->content);

The result looks like this when passed to Data::Dumper (shortened):

$VAR1 = {
  'd' => {
    'results' => [
      {
        'ID'         => '31ff54d4-3b0b-4035-8d2b-ecadf6181745',
        '__metadata' => {
          'type' => 'WebResult',
          'uri' => 'https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=\'ip:213.149.103.135 php?id=\'&$skip=0&$top=1'
        },
        'Url' => 'http://www.visit-mba.org/cg/strana.php?id=37',
        'Description' => 'Zakonska regulativa. Zakoni 2012. Zakon o akcizama; Zakon o budzetu Crne Gore za 2012.godinu; Zakon o detektivskoj djelatnosti; Zakon o elektronskim medijima',
        'DisplayUrl' => 'www.visit-mba.org/cg/strana.php?id=37',
        'Title'      => 'Montenegro Business Alliance'
      },
      {
        'ID'         => 'e8e8da15-1adc-46a8-b16d-d0c65451c47e',
        '__metadata' => {
          'type' => 'WebResult',
          'uri' => 'https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Web?Query=\'ip:213.149.103.135 php?id=\'&$skip=1&$top=1'
        },
        'Url'         => 'http://www.ceti.co.me/podkategorija.php?id=9',
        'Description' => "\x{a9} 2011 Centar za ekotoksikolo\x{161}ka ispitivanja Crne Gore. By Creative Studio LASSO",
        'DisplayUrl'  => 'www.ceti.co.me/podkategorija.php?id=9',
        'Title'       => "Organizaciona \x{161}ema - CETI - Centar za ekotoksikolo\x{161}ka ..."
      },
    ]
  }
};

Now we can easily make our way through this. As you only want the URLs, this is what we are going to do.

my @urls = map { $_->{'Url'} } @{ $data->{d}->{results} };

It's as simple as that. Let's look at what is happening here:

  • There's a hash-key d in $data.
  • Inside that, there's another hash-key results.
  • Since that holds the array ref we want, we use it as the list to pass to map.
  • Now we map over each element of the results, which conveniently are inside of $_ in the map block.
  • We can just access the keys, in our case Url.
  • map will return a list, which we assign to @urls.
  • And that's it.

Here's your list:

$VAR1 = [
  'http://www.visit-mba.org/cg/strana.php?id=37',
  'http://www.ceti.co.me/podkategorija.php?id=9',
  'http://www.visit-mba.org/cg/strana.php?id=30',
  'http://www.ceti.co.me/podkategorija.php?id=7',
  'http://konfliktinteresa.me/funkcioneri/EvidFunPrijave.php?ID=Ozo-NTU8OCYyPz43Og,,',
  'http://konfliktinteresa.me/funkcioneri/EvidFunPrijave.php?ID=Oz4-NDU9MyYzPz43Og,,',
  'http://www.montenegrostars.com/new/pretraga.php?id_jez=2',
  'http://www.montenegrostars.com/new/index.php?id_jez=1&id=37',
  'http://www.hotelbjelobor.com/index.php?id=vila-zelenbor',
  'http://www.hotelbjelobor.com/index.php?id=kontakt',
  'http://www.ekonomija.ac.me/opsirnije.php?opsirnije=6369',
  'http://www.fotostudent.co.me/kalkulator2.php?id=1',
  'http://www.infot.co.me/ita/n.php?id=2',
  'http://www.incognito.co.me/'
];
于 2013-03-19T15:04:50.050 に答える