Prosíme přihlašte se nebo zaregistrujte.

Přihlašte se svým uživatelským jménem a heslem.
Vaše pomoc je stále potřeba!

Autor Téma: html parsing  (Přečteno 3870 krát)

rainbof

  • Aktivní člen
  • *
  • Příspěvků: 255
  • Pták nespavák
html parsing
« kdy: 20 Června 2011, 13:16:31 »
ahoj, jak nějak hezky vytahat z html stránky hodnoty z tagů ? zatim to řesim pomocí pythonu ale třeba někdo znáte ultimátní způsob jak to dělat líp.
Ranní ptáče dřív doskáče!

pacholik

  • Závislák
  • ***
  • Příspěvků: 2072
Re: html parsing
« Odpověď #1 kdy: 20 Června 2011, 13:52:37 »
html2text

a sedem na vytažení parametrů, takhle vytáhnu všechny cíle odkazů
Kód: [Vybrat]
sed -nr 's/^.*<a href="([^"]+)">[^<]+<\/a>.*$/\1/p'
ale nevím jestli je to dostatečně ultimátní ;)

rainbof

  • Aktivní člen
  • *
  • Příspěvků: 255
  • Pták nespavák
Re: html parsing
« Odpověď #2 kdy: 21 Června 2011, 15:33:18 »
a už je na sed (potazmo na awk) nějaká dokumentace 4dummies ?
Ranní ptáče dřív doskáče!

Tomáš Břinčil

  • Občanské sdružení Ubuntu ČR
  • Závislák
  • ***
  • Příspěvků: 1610
  • moje obrázky ↓
    • TomasBrincil.cz
Re: html parsing
« Odpověď #3 kdy: 21 Června 2011, 16:46:42 »
V TLDP je to docela hezky načnuté :)

honzee

  • Aktivní člen
  • *
  • Příspěvků: 185
Re: html parsing
« Odpověď #4 kdy: 22 Června 2011, 10:37:24 »
Pokud jsou to XHTML validni stranky, zkusil bych na to XPath/XQuery...
Mezi má oblíbená prvočísla patří 11° a 13°

Kedrigern

Re: html parsing
« Odpověď #5 kdy: 22 Června 2011, 11:10:02 »
Zrovna jsem to řešil. Nevalidity bych se až tak nebál. Většina stránek (a u jiných to moc nemá smysl) je generována strojově, čili obsah (text) který je zajímavý je v nějakém divu (typicky něco ala <div class="content">). Nevalidní jsou části neobsahové.

Čili vezmeš knihovnu pro XML a tam jde většinou přeskakovat začátek (už kvůli DOCTYPE etc.), čili nastavíš jako root element to <div class="content"> a máš strukturu obsahu přímo hezky po ruce.

Takhle to bude asi o něco přehlednější, než to furt regexpit.

Pozn:
1) pro Python XML knihoven budou mraky
2) já používám Mono (pro info, odkud beru zkušenost)
3) BASH toho zrovna moc nemá - nebo ano? Sed je trochu low level, když se bavíme o connectu někam na net

rainbof

  • Aktivní člen
  • *
  • Příspěvků: 255
  • Pták nespavák
Re: html parsing
« Odpověď #6 kdy: 22 Června 2011, 13:00:31 »
to je velmi dobrej trik s tim root elementem :)
Ranní ptáče dřív doskáče!

starenka

  • Stálý člen
  • **
  • Příspěvků: 1276
  • Karma: plynová [zapalovač] [hasičák]
    • /home/starenka
Re: html parsing
« Odpověď #7 kdy: 26 Června 2011, 11:37:55 »
v pythonu BeautifulSoup, lxml, mechanize, twill, scrapy ....

nicemene obcas proste staci dobre napsanej regexp

v polivce napriklad:
Kód: [Vybrat]
starenka@kosmik1:~$ python
Python 2.6.6 (r266:84292, Apr 20 2011, 11:58:30)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib2
>>> from BeautifulSoup import BeautifulSoup
>>> s = BeautifulSoup(urllib2.urlopen('http://forum.ubuntu.cz').read())
>>> hrefs = (a['href'] for a in s.findAll('a') if a['href'] not in ('#'))
>>> hrefs
<generator object <genexpr> at 0x9e0b52c>
>>> for h in hrefs: print h
...
http://forum.ubuntu.cz/index.php?action=login
http://forum.ubuntu.cz/index.php?action=register
http://forum.ubuntu.cz/index.php?action=activate
http://forum.ubuntu.cz/index.php?action=search;advanced
http://forum.ubuntu.cz/index.php
http://forum.ubuntu.cz/index.php?action=help
http://forum.ubuntu.cz/index.php?action=search
http://forum.ubuntu.cz/index.php?action=login
http://forum.ubuntu.cz/index.php?action=register
http://forum.ubuntu.cz/index.php
http://forum.ubuntu.cz/index.php#3
http://forum.ubuntu.cz/index.php?action=unread;board=29.0
http://forum.ubuntu.cz/index.php?board=29.0
http://forum.ubuntu.cz/index.php?action=profile;u=1331
http://forum.ubuntu.cz/index.php?topic=12735.msg408080#new
http://forum.ubuntu.cz/index.php?action=unread;board=7.0
http://forum.ubuntu.cz/index.php?board=7.0
http://forum.ubuntu.cz/index.php?action=profile;u=16559
http://forum.ubuntu.cz/index.php?topic=56860.msg408080#new
http://forum.ubuntu.cz/index.php?action=unread;board=50.0
http://forum.ubuntu.cz/index.php?board=50.0
http://forum.ubuntu.cz/index.php?board=31.0
http://forum.ubuntu.cz/index.php?board=51.0
http://forum.ubuntu.cz/index.php?board=52.0
http://forum.ubuntu.cz/index.php?board=53.0
http://forum.ubuntu.cz/index.php?board=48.0
http://forum.ubuntu.cz/index.php?board=49.0
http://forum.ubuntu.cz/index.php?action=profile;u=1331
http://forum.ubuntu.cz/index.php?topic=54727.msg408080#new
http://forum.ubuntu.cz/index.php#4
http://forum.ubuntu.cz/index.php?action=unread;board=10.0
http://forum.ubuntu.cz/index.php?board=10.0
http://forum.ubuntu.cz/index.php?action=profile;u=16348
http://forum.ubuntu.cz/index.php?topic=57144.msg408080#new
http://forum.ubuntu.cz/index.php?action=unread;board=14.0
http://forum.ubuntu.cz/index.php?board=14.0
http://forum.ubuntu.cz/index.php?action=profile;u=702
http://forum.ubuntu.cz/index.php?topic=57112.msg408080#new
http://forum.ubuntu.cz/index.php?action=unread;board=5.0
http://forum.ubuntu.cz/index.php?board=5.0
http://forum.ubuntu.cz/index.php?action=profile;u=18976
http://forum.ubuntu.cz/index.php?topic=57143.msg408080#new
http://forum.ubuntu.cz/index.php?action=unread;board=4.0
http://forum.ubuntu.cz/index.php?board=4.0
http://forum.ubuntu.cz/index.php?action=profile;u=16455
http://forum.ubuntu.cz/index.php?topic=57127.msg408080#new
http://forum.ubuntu.cz/index.php?action=unread;board=18.0
http://forum.ubuntu.cz/index.php?board=18.0
http://forum.ubuntu.cz/index.php?action=profile;u=726
http://forum.ubuntu.cz/index.php?topic=57140.msg408080#new
http://forum.ubuntu.cz/index.php?action=unread;board=19.0
http://forum.ubuntu.cz/index.php?board=19.0
http://forum.ubuntu.cz/index.php?action=profile;u=29528
http://forum.ubuntu.cz/index.php?topic=56830.msg408080#new
http://forum.ubuntu.cz/index.php?action=unread;board=20.0
http://forum.ubuntu.cz/index.php?board=20.0
http://forum.ubuntu.cz/index.php?action=profile;u=15504
http://forum.ubuntu.cz/index.php?topic=57134.msg408080#new
http://forum.ubuntu.cz/index.php?action=unread;board=22.0
http://forum.ubuntu.cz/index.php?board=22.0
http://forum.ubuntu.cz/index.php?action=profile;u=29680
http://forum.ubuntu.cz/index.php?topic=57148.msg408080#new
http://forum.ubuntu.cz/index.php?action=unread;board=47.0
http://forum.ubuntu.cz/index.php?board=47.0
http://forum.ubuntu.cz/index.php?action=profile;u=5587
http://forum.ubuntu.cz/index.php?topic=57077.msg408080#new
http://forum.ubuntu.cz/index.php#6
http://forum.ubuntu.cz/index.php?action=unread;board=6.0
http://forum.ubuntu.cz/index.php?board=6.0
http://forum.ubuntu.cz/index.php?action=profile;u=16428
http://forum.ubuntu.cz/index.php?action=profile;u=28688
http://forum.ubuntu.cz/index.php?topic=50343.msg408080#new
http://forum.ubuntu.cz/index.php?action=unread;board=9.0
http://forum.ubuntu.cz/index.php?board=9.0
http://forum.ubuntu.cz/index.php?action=profile;u=17286
http://forum.ubuntu.cz/index.php?topic=57135.msg408080#new
http://forum.ubuntu.cz/index.php#7
http://forum.ubuntu.cz/index.php?action=unread;board=33.0
http://forum.ubuntu.cz/index.php?board=33.0
http://forum.ubuntu.cz/index.php?action=profile;u=28647
http://forum.ubuntu.cz/index.php?topic=57020.msg408080#new
http://forum.ubuntu.cz/index.php#8
http://forum.ubuntu.cz/index.php?action=unread;board=8.0
http://forum.ubuntu.cz/index.php?board=8.0
http://forum.ubuntu.cz/index.php?action=profile;u=1331
http://forum.ubuntu.cz/index.php?topic=57125.msg408080#new
http://forum.ubuntu.cz/index.php#10
http://forum.ubuntu.cz/index.php?action=unread;board=45.0
http://forum.ubuntu.cz/index.php?board=45.0
http://forum.ubuntu.cz/index.php?action=profile;u=1331
http://forum.ubuntu.cz/index.php?topic=56664.msg408080#new
http://forum.ubuntu.cz/index.php#9
http://forum.ubuntu.cz/index.php?action=unread;board=25.0
http://forum.ubuntu.cz/index.php?board=25.0
http://forum.ubuntu.cz/index.php?action=profile;u=21865
http://forum.ubuntu.cz/index.php?topic=55531.msg408080#new
http://forum.ubuntu.cz/index.php?action=stats
http://forum.ubuntu.cz/index.php?action=profile;u=30668
http://forum.ubuntu.cz/index.php?topic=50343.msg408080#new
http://forum.ubuntu.cz/index.php?action=recent
http://forum.ubuntu.cz/index.php?action=stats
http://forum.ubuntu.cz/index.php?action=profile;u=2252
http://forum.ubuntu.cz/index.php?action=profile;u=30658
http://forum.ubuntu.cz/index.php?action=profile;u=16243
http://forum.ubuntu.cz/index.php?action=profile;u=5587
http://forum.ubuntu.cz/index.php?action=profile;u=17451
http://forum.ubuntu.cz/index.php?action=profile;u=7813
http://forum.ubuntu.cz/index.php?action=profile;u=29680
http://forum.ubuntu.cz/index.php?action=profile;u=29008
http://forum.ubuntu.cz/index.php?action=profile;u=7578
http://forum.ubuntu.cz/index.php?action=profile;u=15504
http://forum.ubuntu.cz/index.php?action=profile;u=934
http://forum.ubuntu.cz/index.php?action=profile;u=10860
http://forum.ubuntu.cz/index.php?action=profile;u=28688
http://forum.ubuntu.cz/index.php?action=profile;u=16348
http://forum.ubuntu.cz/index.php?action=profile;u=2167
http://forum.ubuntu.cz/index.php?action=profile;u=17915
http://forum.ubuntu.cz/index.php?action=profile;u=30668
http://forum.ubuntu.cz/index.php?action=profile;u=14478
http://forum.ubuntu.cz/index.php?action=reminder
http://forum.ubuntu.cz/index.php?action=login
http://www.mysql.com/
http://www.php.net/
http://www.simplemachines.org/
http://www.simplemachines.org/about/copyright.php
http://sdruzeni.ubuntu.cz
http://www.nic.cz/
http://validator.w3.org/check/referer
http://jigsaw.w3.org/css-validator/check/referer
« Poslední změna: 26 Června 2011, 12:06:12 od starenka »
mojenka | blozinek v2 | starej blozinek | krotim hady za penize | UJ! Kalužátor

Debian testing vrní v Thinkpadu X200/bedně/EEE 900a, MasoX Tiger v lampe

 

Provoz zaštiťuje spolek OpenAlt.