2

这是用 bing 搜索网页的代码,查询是 man,它会找到 ID、标题、描述、Urls...是否可以只过滤 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);

输出:

--- "{\"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

如果您只想要 URL,则需要首先use JSON(您已经拥有将返回值转换为 Perl 数据结构。

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

当传递给Data::Dumper(缩短)时,结果如下所示:

$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 ..."
      },
    ]
  }
};

现在我们可以很容易地解决这个问题。由于您只需要 URL,这就是我们要做的。

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

就这么简单。让我们看看这里发生了什么:

  • 中有一个哈希键d$data
  • 在其中,还有另一个 hash-key results
  • 因为它保存了我们想要的数组 ref,所以我们将它用作传递给的列表map
  • 现在我们map遍历 的每个元素results,它们方便地位于$_map的内部。
  • 在我们的例子中,我们可以访问密钥Url
  • map将返回一个列表,我们将其分配给@urls.
  • 就是这样。

这是你的清单:

$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 回答