Форум программистов «Весельчак У»
  *
Добро пожаловать, Гость. Пожалуйста, войдите или зарегистрируйтесь.
Вам не пришло письмо с кодом активации?

  • Рекомендуем проверить настройки временной зоны в вашем профиле (страница "Внешний вид форума", пункт "Часовой пояс:").
  • У нас больше нет рассылок. Если вам приходят письма от наших бывших рассылок mail.ru и subscribe.ru, то знайте, что это не мы рассылаем.
   Начало  
Наши сайты
Помощь Поиск Календарь Почта Войти Регистрация  
 
Страниц: [1]   Вниз
  Печать  
Автор Тема: regexp  (Прочитано 21118 раз)
0 Пользователей и 1 Гость смотрят эту тему.
samlab
Гость
« : 30-10-2010 21:34 » 

Приветствую всех... Собственно, вот какой вопрос. Парсию я урлы на каком-нить сайте, вот код...
Код:
#!/usr/bin/perl

$| = 1;

use LWP::UserAgent;

$ua = LWP::UserAgent->new;
$ua->agent('Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');
$ua->default_header('Referer' => "http://google.ru/");
$ua->default_header('Accept-Encoding' => "gzip,deflate");
$ua->default_header('Accept-Language'=> "*/*");
$ua->default_header('Accept'=> "text/html,*/*");
$ua->default_header('Accept-Charset' => "windows-1251,utf-8");

$response = $ua->get('http://site.ru/');

$text = $response->decoded_content;

my @refs = $text =~ m/\b[^>]*?\bhref="(http:\/\/site\.ru[^"]+)"[^>]*>/gi;

for my $ref (@refs)
{
open (F, ">> refs.txt");
print (F "$ref \n");
close (F);
}
В результате, на сайте я могу встретить много раз одни и те же урлы, которые все прямехонько и сохранятся в лог... Есть ли какие нибудь способы, чтобы это обойти?
Заранее спасибо, рад любой подсказке.
« Последнее редактирование: 30-10-2010 21:37 от samlab » Записан
RXL
Технический
Администратор

ru
Offline Offline
Пол: Мужской

WWW
« Ответ #1 : 30-10-2010 21:44 » 

Применить хеш.

Код: (Perl)
my %refs;

for my $ref (@refs)
{
    $refs{$ref} = '';
}

for my $key (keys(%refs))
{
    print("$refs{$key}\n");
}

Можно вместо

Код:
    $refs{$ref} = '';

сделать

Код:
    $refs{$ref}++;

и тем самым еще и подсчитать число совпадений.


Добавлено через 11 минут и 27 секунд:
Можно преобразовать массив в хеш немного проще:

Код: (Perl)
my %refs = map { $_ => '' } @refs;
« Последнее редактирование: 30-10-2010 21:55 от RXL » Записан

... мы преодолеваем эту трудность без синтеза распределенных прототипов. (с) Жуков М.С.
samlab
Гость
« Ответ #2 : 31-10-2010 16:48 » 

Спасибо. Только небольшие нюансы... Для примера, взял первый попавшийся варезник...
Если делать, как я показал в первом посту, то выводится вот в таком виде
Код:
h**p://Somesight/engine/opensearch.php
h**p://Somesight/rss.xml
h**p://Somesight/
h**p://Somesight/mults/131278-tom-i-dzherri-polnaya-kollekciya-6-disk-1940.html
h**p://Somesight/mults/
h**p://Somesight/user/tomus77/
h**p://Somesight/mults/131278-tom-i-dzherri-polnaya-kollekciya-6-disk-1940.html#comment
h**p://Somesight/mults/131278-tom-i-dzherri-polnaya-kollekciya-6-disk-1940.html
h**p://Somesight/soft/131276-password-depot-professional-525-2010.html
h**p://Somesight/soft/
h**p://Somesight/user/tomus77/
h**p://Somesight/soft/131276-password-depot-professional-525-2010.html#comment
h**p://Somesight/soft/131276-password-depot-professional-525-2010.html
h**p://Somesight/raznoe/131260-valentin-pikul-bayazet-2006112-kbps.html
h**p://Somesight/raznoe/
h**p://Somesight/user/tomus77/
h**p://Somesight/raznoe/131260-valentin-pikul-bayazet-2006112-kbps.html#comment
h**p://Somesight/raznoe/131260-valentin-pikul-bayazet-2006112-kbps.html
h**p://Somesight/raznoe/131256-uroki-masterstva-shkatulka-s-sekretom-2010dvdrip.html
h**p://Somesight/raznoe/
h**p://Somesight/user/tomus77/
h**p://Somesight/raznoe/131256-uroki-masterstva-shkatulka-s-sekretom-2010dvdrip.html#comment
h**p://Somesight/raznoe/131256-uroki-masterstva-shkatulka-s-sekretom-2010dvdrip.html
h**p://Somesight/raznoe/131254-krasivaya-figura-legko-i-prosto-dvdrip.html
h**p://Somesight/raznoe/
h**p://Somesight/user/tomus77/
h**p://Somesight/raznoe/131254-krasivaya-figura-legko-i-prosto-dvdrip.html#comment
h**p://Somesight/raznoe/131254-krasivaya-figura-legko-i-prosto-dvdrip.html
h**p://Somesight/raznoe/131253-sekrety-xudozhestvennoj-kovki-videouroki-rus.html
h**p://Somesight/raznoe/
h**p://Somesight/user/tomus77/
h**p://Somesight/raznoe/131253-sekrety-xudozhestvennoj-kovki-videouroki-rus.html#comment
h**p://Somesight/raznoe/131253-sekrety-xudozhestvennoj-kovki-videouroki-rus.html
h**p://Somesight/films/131251-delo-temnoe-vystrel-v-andropova-yefir-ot-30102010.html
h**p://Somesight/films/
h**p://Somesight/user/tomus77/
h**p://Somesight/films/131251-delo-temnoe-vystrel-v-andropova-yefir-ot-30102010.html#comment
h**p://Somesight/films/131251-delo-temnoe-vystrel-v-andropova-yefir-ot-30102010.html
h**p://Somesight/soft/131249-comodo-system-cleaner-3016788637-portable-2010rus.html
h**p://Somesight/soft/
h**p://Somesight/user/tomus77/
h**p://Somesight/soft/131249-comodo-system-cleaner-3016788637-portable-2010rus.html#comment
h**p://Somesight/soft/131249-comodo-system-cleaner-3016788637-portable-2010rus.html
h**p://Somesight/mults/131248-ladushki-dlya-malyshej-vypusk-1-sbornik.html
h**p://Somesight/mults/
h**p://Somesight/user/WildCat/
h**p://Somesight/mults/131248-ladushki-dlya-malyshej-vypusk-1-sbornik.html#comment
h**p://Somesight/mults/131248-ladushki-dlya-malyshej-vypusk-1-sbornik.html
h**p://Somesight/soft/131247-windows-xp-game-edition-by-goriander-2010.html
h**p://Somesight/soft/
h**p://Somesight/user/tomus77/
h**p://Somesight/soft/131247-windows-xp-game-edition-by-goriander-2010.html#comment
h**p://Somesight/soft/131247-windows-xp-game-edition-by-goriander-2010.html
h**p://Somesight/films/131232-noch-demonov-night-of-the-demons-2009-bdrip-1080p.html
h**p://Somesight/films/
h**p://Somesight/user/WildCat/
h**p://Somesight/films/131232-noch-demonov-night-of-the-demons-2009-bdrip-1080p.html#comment
h**p://Somesight/films/131232-noch-demonov-night-of-the-demons-2009-bdrip-1080p.html
h**p://Somesight/soft/131230-dvdfab-8032-final-rus.html
h**p://Somesight/soft/
h**p://Somesight/user/BestNeoGameSl/
h**p://Somesight/soft/131230-dvdfab-8032-final-rus.html#comment
h**p://Somesight/soft/131230-dvdfab-8032-final-rus.html
h**p://Somesight/soft/131219-minilyrics-70643-rus.html
h**p://Somesight/soft/
h**p://Somesight/user/BestNeoGameSl/
h**p://Somesight/soft/131219-minilyrics-70643-rus.html#comment
h**p://Somesight/soft/131219-minilyrics-70643-rus.html
h**p://Somesight/games/131196-korsary-proklyatye-sudboj-pc2010rus.html
h**p://Somesight/games/
h**p://Somesight/user/celovek11/
h**p://Somesight/games/131196-korsary-proklyatye-sudboj-pc2010rus.html#comment
h**p://Somesight/games/131196-korsary-proklyatye-sudboj-pc2010rus.html
h**p://Somesight/music/131195-progression-sequences.html
h**p://Somesight/music/
h**p://Somesight/user/celovek11/
h**p://Somesight/music/131195-progression-sequences.html#comment
h**p://Somesight/music/131195-progression-sequences.html
h**p://Somesight/films/131193-bolshoe-alibi-le-grand-alibi-dvdrip700.html
h**p://Somesight/films/
h**p://Somesight/user/celovek11/
h**p://Somesight/films/131193-bolshoe-alibi-le-grand-alibi-dvdrip700.html#comment
h**p://Somesight/films/131193-bolshoe-alibi-le-grand-alibi-dvdrip700.html
h**p://Somesight/music/131191-selected-remixes-1-2010.html
h**p://Somesight/music/
h**p://Somesight/user/celovek11/
h**p://Somesight/music/131191-selected-remixes-1-2010.html#comment
h**p://Somesight/music/131191-selected-remixes-1-2010.html
h**p://Somesight/games/131189-egypt-1156-b-c-tomb-of-the-pharaoh-pcrus.html
h**p://Somesight/games/
h**p://Somesight/user/celovek11/
h**p://Somesight/games/131189-egypt-1156-b-c-tomb-of-the-pharaoh-pcrus.html#comment
h**p://Somesight/games/131189-egypt-1156-b-c-tomb-of-the-pharaoh-pcrus.html
h**p://Somesight/music/131187-dj-feel-trancemission-top-25-october-2010.html
h**p://Somesight/music/
h**p://Somesight/user/celovek11/
h**p://Somesight/music/131187-dj-feel-trancemission-top-25-october-2010.html#comment
h**p://Somesight/music/131187-dj-feel-trancemission-top-25-october-2010.html
h**p://Somesight/games/131185-supersemejka-the-incredibles-dilogiya-pcrepackru.html
h**p://Somesight/games/
h**p://Somesight/user/celovek11/
h**p://Somesight/games/131185-supersemejka-the-incredibles-dilogiya-pcrepackru.html#comment
h**p://Somesight/games/131185-supersemejka-the-incredibles-dilogiya-pcrepackru.html
h**p://Somesight/page/2/
h**p://Somesight/page/3/
h**p://Somesight/page/4/
h**p://Somesight/page/5/
h**p://Somesight/page/6/
h**p://Somesight/page/7/
h**p://Somesight/page/8/
h**p://Somesight/page/9/
h**p://Somesight/page/10/
h**p://Somesight/page/2368/
h**p://Somesight/page/2/
h**p://Somesight/index.php?do=register
h**p://Somesight/index.php?do=lostpassword
h**p://Somesight/soft/129907-wpik-wpi-2010.html
h**p://Somesight/games/131175-the-sims-3-gold-edition-6-v-1-pc2010russkij.html
h**p://Somesight/raznoe/130807-karatye-bazovyj-videokurs-2008dvdrip.html
h**p://Somesight/soft/129513-windows7-maksimalnaya-sp1-v178-x86-and-x64-full.html
h**p://Somesight/clips/130053-rumusic-sbornik-videoklipov-oktyabr-2010-dvd5.html
или, так
Код:
#!/usr/bin/perl -w

$| = 1;

use LWP::UserAgent;

$ua = LWP::UserAgent->new;
$ua->agent('Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)');
$ua->default_header('Referer' => "http://google.ru/");
$ua->default_header('Accept-Encoding' => "gzip,deflate");
$ua->default_header('Accept-Language'=> "*/*");
$ua->default_header('Accept'=> "text/html,*/*");
$ua->default_header('Accept-Charset' => "windows-1251,utf-8");

$response = $ua->get('h**p://Somesight/');

$text = $response->decoded_content;

my @refs = $text =~ m/\b[^>]*?\bhref="(http:\/\/somesight\.net[^"]+)"[^>]*>/gi;

my %refs;

for my $ref (@refs)
{
    $refs{$ref}++;
}

for my $key (keys(%refs))
{
    print("$refs{$key}\n");
}
В результате, печатается только $key, а рефок нету... Здесь была моя ладья...
« Последнее редактирование: 31-10-2010 19:20 от Finch » Записан
RXL
Технический
Администратор

ru
Offline Offline
Пол: Мужской

WWW
« Ответ #3 : 31-10-2010 21:42 » 

Да, напутал.

Код:
for my $key (keys(%refs))
{
    print("$key\n");
}
Записан

... мы преодолеваем эту трудность без синтеза распределенных прототипов. (с) Жуков М.С.
samlab
Гость
« Ответ #4 : 31-10-2010 22:29 » new

Спасибо, теперь - как надо, все работает...
Записан
Страниц: [1]   Вверх
  Печать  
 

Powered by SMF 1.1.21 | SMF © 2015, Simple Machines