30 Mart 2009 Pazartesi

Pythonla internetten sayfa çekmek

Pythonla çok samimi olmasakta bazen işimi çok kolaylaştırdığını(150 yi aşkın sayfayı belli bir veri için kontrol etmek gibi) inkar edemem. Basit bir kod belki ama kullanışlı olduğu kesin.

#Page.py

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\////////////////////////////////////////////

import urllib

import urllib2

POST = 1

GET = 2

def retrieve(url,values,method):

----------->try:

----------------------->data=urllib.urlencode(values)

----------------------->if method == POST:

---------------------------------->request=urllib2.Request(url,data)

---------------------------------->response=urllib2.urlopen(request)

---------------------->elif method == GET:

---------------------------------->get_url=url+'?'+data

---------------------------------->response=urllib2.urlopen(get_url)

---------------------->content=response.read()

---------------------->return content

---------->except urllib2.HTTPError, e:

---------------------->print 'Gecersiz istek!!!'

---------------------->print 'Hata: ', e.code

---------->except urllib2.URLError, e:

---------------------->print 'Servera baglanti yok!!'

---------------------->print 'Sebep: ', e.reason

---------->print 'Opppsss...Fonksiyon basarisiz oldu.'

////////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

Bu modulün kullanımına örnek olarak ta şöyle bir kod yazılabilir.

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\////////////////////////////////////////////

#-*- coding: utf-8 -*- #kod içerisinde türkçe karakter kullanabilmek için

import Page

print "Trying...."

for i in range(1,179):

----------->print "%s " % i,

----------->values={'sektorid':'39','ilce_id':'','page':str(i)}

----------->content=Page.retrieve('http://www.webrehberi.net/istanbul_rehberi/alt_sektor.php',values,Page.GET)

----------->if (">ERDEK<" in content) or (">DURSUNBEY<" in content):

----------------------->print ""

----------------------->print "Found at %s. page" % i

////////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

Herzaman ki gibi kodlara “Dosyalarım linkinden ulaşılabilir.

Hiç yorum yok:

Yorum Gönder