Beautifulsoup Ve Web.Py İle Neler Yapılabilir
-
Sorusunun cevabı aşağıda
Mobil TBT yapılabilir.
Şu an düzeltmediğim tek sıkıntısı 20-30 tane ard arda yapılan alıntılar ekrana sığmıyor, taşıyor
Geliştirmeye açık, örnektir
Kullanımı
------------
Gereksinimler: Python 2.7, Web.py, BeautifulSoup4, requests
çift tıklayıp çalıştırıyorsunuz, default olarak 8080'de bir webserver açıyor
tarayıcınızda localhost:8080 yazıyorsunuz, mobil tbt karşınızda
ekstra bi özellik beklemeyin çok uğraşmadım
tek gıcık oldugum şeyi düzelttim : sayfaları mobile sığacak hale getirmek
- uzun linkleri kısaltır
- resimleri linke çevirir, tıklarsan yeni sekmede açar
-
#!/usr/bin/python # coding: utf-8 import os, time, re, subprocess, requests, sys, codecs, web, urllib, textwrap from bs4 import BeautifulSoup as bs import lxml.html import binascii urls = ( '/(.*)', 'index', ) class index: def GET(self,name): r = web.input(p=None).p web.header('Content-Type', 'text/html; charset=UTF-8') yield '''<meta name="viewport" content="width=device-width, initial-scale=1.0">''' # mobile uygun hale getir yield '''<style> a:link { text-decoration: none; } a:visited { text-decoration: none; } a:hover { text-decoration: underline; } a:active { text-decoration: underline; } </style>\n''' # link stilleri yield '''<style type="text/css">#tbt { border: 1px solid #CC0066; background-color: #f2f2f2; width: 100%; border-radius: 6px; -webkit-border-radius: 6px; -moz-border-radius: 6px;}#tbt td, #tbt th { padding: 5px; color: #333;}#tbt thead { font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif; padding: .2em 0 .2em .5em; text-align: left; color: #4B4B4B; background-color: #C8C8C8; background-image: -webkit-gradient(linear, left top, left bottom, from(#f2f2f2), to(#e3e3e3), color-stop(.6,#B3B3B3)); background-image: -moz-linear-gradient(top, #D6D6D6, #B0B0B0, #B3B3B3 90%); border-bottom: solid 1px #CC0066;}#tbt th { font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 17px; line-height: 20px; font-style: normal; font-weight: normal; text-align: left; text-shadow: white 1px 1px 1px;}#tbt td { line-height: 20px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 12px; border-bottom: 1px solid #CC0066; border-top: 1px solid #CC0066;}#tbt td:hover { background-color: #fff;}</style>''' # tbt tablo stili yield '''<style type="text/css">div.bunuyazdicontent{/*opacity:0.95;*/}div.bunuyazdidiv{/*opacity:0.95;*/padding:5px 5px 5px 5px;margin-left:10px;margin-right:10px;clear:both;/*line-height:0.8em;*/background-color:#F3F9FF;/*border:1px gray inset;*/border: 1px solid #D4E5FF;}</style>''' # alinti css if not r : # anasayfayi goster yield '<title>TBT Anasayfa</title>' url = 'http://www.tahribat.com/Aktif-Forum-Konulari' veri = {"hours" : "168", "b1" : "OK"} veri = {"hours" : "24", "b1" : "OK"} h_headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36'} sorgu = requests.post(url, data=veri, headers=h_headers).content doc = lxml.html.fromstring(sorgu) doc.make_links_absolute(url) html = lxml.html.tostring(doc) soup = bs(html) tablo = soup.find_all('table', class_=re.compile('(?i)^ForumTable1$'))[0] satirlar = tablo.find_all('tr') yield '<table id="tbt">' for i in satirlar: sutunlar = i.find_all('td') try: baslik = sutunlar[1].a.text konu_linki = sutunlar[1].a.get('href') konu_linki = re.sub('(?i)http.?://.*?tahribat.com/(Forum-[^/]*)/', '\g<1>/1/-/-/999999', konu_linki) # tum mesajlari icersin konu_linki_encoded = '/?p=%s' % urllib.quote_plus(konu_linki) # url encode konu_linki_encoded = '/?p=%s' % (konu_linki) # url encode konuyu_acan = sutunlar[2].a.text sutunlar[2].a.extract() # acilma_tarihi = re.findall('\d* \w* \d* \(\w*\) \d\d:\d\d',sutunlar[2].text)[0] son_mesaji_yazan = sutunlar[3].a.text sutunlar[3].a.extract() # son_mesaj_tarihi = re.findall('\d*:\d*:\d* ..',sutunlar[3].text)[0] # hit = re.findall('(?i)(\d*) Hit',sutunlar[4].text)[0] # mesaj_sayisi = re.findall('(?i)(\d*) mesaj',sutunlar[4].text)[0] temp = '<tr><td><a href="%s" target=_self>%s</a><br><b>Sahip:</b> %s <b>Son Yazan:</b> %s</td></tr>\n' % (konu_linki_encoded,baslik,konuyu_acan,son_mesaji_yazan) yield temp except: pass yield '</table>' else: # link geldi bi kontrol et url = urllib.unquote_plus(r) # url decode # test = re.findall('(?i)^http.?://.*?tahribat\.com/Forum-.*',url) # link dogru mu test = ' ' url = 'http://www.tahribat.com/%s' % url if len(test) > 0 : sorgu = requests.get(url).content doc = lxml.html.fromstring(sorgu) doc.make_links_absolute('http://tahribat.com/') html = lxml.html.tostring(doc) soup = bs(html) konu = soup.title.string konu = re.sub('(?i)^Tahribat.Com . ','',konu) yield '<title>%s</title><b>Konu: %s</b><p><table id="tbt"><thead id="tbt"><tr><td><b><a href="/">ANASAYFA</a></b></thead></td></tr>' % (konu,konu) duzelt = soup.find_all('a',href=re.compile('(?i)tahribat.com/Forum-')) for i in duzelt: temp = '%s' % i.get('href') temp = urllib.quote_plus(temp) i['href'] = '/?path=%s' % temp postlar = soup.find_all('table', class_=re.compile('(?i)categorytable')) for i in postlar: try: yorumu_yazan = i.find_all('span', class_=re.compile('(?i)nikneym'))[0].text mesaj = i.find_all('div', class_=re.compile('(?i)forummsg'))[0].decode_contents() mesaj = bs(mesaj) resimler = mesaj.find_all('img') if len(resimler) > 0: for resim in resimler: if resim.get('src') == None : resim.extract() else: res_link = '%s' % resim.get('src') crc = '%s' % binascii.crc32(res_link) crc = crc.replace('-','') duzelt = '<a href="%s" target=_blank> Resim - %s </a>' % (res_link,crc) duzelt = bs(duzelt) resim.replace_with(duzelt) linkler = mesaj.find_all('a') if len(linkler) > 0 : for j in linkler: if j.get('href') == None : if j.text == '' or j.text == None : j.extract() else: temp = '%s' % j.text temp = re.sub(r'"','',temp) j["href"] = temp if len(j.string) > 50 : temp0 = (len(j.string)/2) temp = textwrap.wrap(j.string,width=temp0) temp2 = '' for temp3 in temp: if temp2 == '' : temp2 = temp3 else: temp2 = '%s..<br>..%s' % (temp2,temp3) j.replace_with(bs('<a href="%s">%s</a>' % (j['href'],temp2))) yield '<tr><td><b>%s :</b><br>%s</td></tr>\n' % (yorumu_yazan,mesaj) except: pass yield '</table>' # if __name__ == "__main__" : app = web.application(urls, globals()) app.run() #
-
Sana normalde gicik olurum Daft ama dönüsüne sevindim. Eline saglik
Florenzi tarafından 05/May/15 02:33 tarihinde düzenlenmiştir -
Hocam Adamın dibisin. Şu adama bir rütbe felan verin yahu.
DrKill tarafından 05/May/15 07:53 tarihinde düzenlenmiştir -
Adam ilmek ilmek yazmış valla
-
uslu bi coder olursanız belki bir gün holy'yi bile görebilirsiniz
-
BeautifulSoup, web.py ve requests modüllerinin klasörlerini Lib klasörüne attıktan sonra QPython ile androidde çalıştırabildim, bilginiz olsun.
Lxml kodlarını silmek zorunda kaldım ama (lxml: libxml2 ve bir tane daha modül gerektiriyor, onlarında android için derlenmiş .so dosyası yok sanırım)
YekteranBaymedir tarafından 07/May/15 16:29 tarihinde düzenlenmiştir -
Bu konu neden ilgi görmüyo :/
-
DrKill bunu yazdı
Hocam Adamın dibisin. Şu adama bir rütbe felan verin yahu.
bencede
hiçbirşey anlamadım ama yararlı birşeye benziyor :)
-
futurist bunu yazdıDrKill bunu yazdı
Hocam Adamın dibisin. Şu adama bir rütbe felan verin yahu.
bencede
hiçbirşey anlamadım ama yararlı birşeye benziyor :)
python programlama dili ile basit bir betik yazdım
şeyhin yeni tbtyi kodlamasını bekleyemedim
mobil tbt yazdım kendim
daha sonraki mesajımda da, aynı scripti android telefonda çalıştırabildiğimden bahsettim (ek bi sunucuya ihtiyaç olmadan)
Qpython pure python modülleri destekliyor dedim (ekstra dll gerektirmiyosa androidde kendi scriptlerinizi kullanabilirsiniz)
ValentinoRossi bunu yazdıBu konu neden ilgi görmüyo :/
konuyu açarken ilgi görmeyeceğini biliyordum zaten (başlıktan dolayı olabilir)
he bi de çalıştırmak için biraz python bilmek gerekio, uğraşacak adam yok tbtde
YekteranBaymedir tarafından 09/May/15 01:54 tarihinde düzenlenmiştir -
eline sağlık güzel olmuş +rep :D
şu yield in olayı nedir tam olarak?