'Python'에 해당되는 글 13건

  1. 2014.09.25 Crawler Class Sample
  2. 2014.09.24 List 필터링(Filtering)
  3. 2014.09.24 map function

Crawler Class Sample

Python 2014. 9. 25. 10:46

데이터 Crawling  시에 python 코드 샘플

#
# import modules
#
import MySQLdb
import urllib
import re


#
#URL
#
MAIN_URL = "http://www.test.com"

#
#TMPL
#
TMPL	= ""


#
# CLASS crawler
#
class Crawler(object):

	def __init__(self):

		self.db		    = MySQLdb.connect(host="",user="",passwd="",db="")
		self.cur  		= self.db.cursor()


	def __del__(self):
		self.db.close()
		

	def run(self):
        html  	= urllib.urlopen(MAIN_URL)
		page 	= html.read()
		match 			= re.compile(TMPL , re.DOTALL)
		match_list 		= match.findall(page)


if __name__ == "__main__":

	crawler = Crawler()
	crawler.run()



Posted by ElvinKim
,

List 필터링(Filtering)

Python 2014. 9. 24. 18:29

간단한 조건에 대하여 List 요소 필터링

(EX)

tmpList = range(1,101)
evenList = [num for num in tmpList if num%2 == 0]

'Python' 카테고리의 다른 글

[python] 직접 만든 module import 시 import 한 클래스에 파일을 로드할 시 주의!  (0) 2014.10.20
[Python] PyQt 튜토리얼  (0) 2014.10.08
[python]python file download  (0) 2014.09.25
Crawler Class Sample  (0) 2014.09.25
map function  (0) 2014.09.24
Posted by ElvinKim
,

map function

Python 2014. 9. 24. 17:53

리스트에 동일한 함수 효과를 주고 싶을 때 map 함수를 사용한다.

예를 들어

def square(x):
	return x*x

lst = [1,2,3,4,5,6,7,8,9]

print map(square, lst)

결과는 


Posted by ElvinKim
,