본문 바로가기

Coding/Python113

Mechanize 로 로그인하기 // 예제 소스ID : PW : 2016. 2. 26.
파이썬 정규식 정리 http://www.slideshare.net/dahlmoon/20160301 2016. 2. 25.
Python Proxy check import urllib2, socket socket.setdefaulttimeout(180) # read the list of proxy IPs in proxyListproxyList = ['172.30.1.1:8080', '172.30.3.3:8080'] # there are two sample proxy ip def is_bad_proxy(pip): try: proxy_handler = urllib2.ProxyHandler({'http': pip}) opener = urllib2.build_opener(proxy_handler) opener.addheaders = [('User-agent', 'Mozilla/5.0')] urllib2.install_opener(opener) req=urllib2.R.. 2016. 1. 30.
BeautifulSoup 웹파싱 [ex src]HomeSign up Login soup('input') : 모든 input 태그 반환-> [\n, ] soup('input')[0].string : 첫번째 input 태그의 string 반환-> soup.input['id'] : 첫번째 input태그의 id값 반환-> inputID soup.find_all('a') - 모든 a 태그 반환-> [Home, Sign up , Login] soup.find_all('a')[0].string - 첫번째 a태그의 string 반환-> Home soup.find_all('a')[0]['href'] - 첫번째 a태그의 href값 반환-> index.php 내용추가중.. 2016. 1. 29.
Pytesser OCR import PILfrom PIL import Imagefrom pytesser import *import urllib # Downloadurllib.urlretrieve(주소, "codedown.png") # Resizingbasewidth = 300img = Image.open('codedown.png')wpercent = (basewidth/float(img.size[0]))hsize = int((float(img.size[1])*float(wpercent)))img = img.resize((basewidth,hsize), PIL.Image.ANTIALIAS)img.save('resize.png') # Readim = Image.open ('resize.png')text = image_to_stri.. 2016. 1. 27.
소켓 통신할때 Response 못받아오는 문제 최근 소켓으로 통신할때 Response를 파싱하는 작업을 진행하다가제대로 받아오지 못하고 깨진 문자열을 반환하는 문제점을 발견했다. 해결법은 간단하다. 받아온 값을 decompress 해주면 해결되더라.(Request 보낼 때 Accept-Encoding 헤더를 지우면 되는경우도 있음) import gzipimport StringIO body = decompress할 값decompress = StringIO.StringIO(body)gzipper = gzip.GzipFile(fileobj=decompress)temp = gzipper.read()print temp 추가로 웹 크롤링 등의 작업을 할 때 gzip 등으로 compress 시켜주면 보다 빠른 속도로 진행이 가능하다고 한다.(서버에서 해당 인코딩.. 2016. 1. 8.