파이썬 프로그래밍으로 지루한 작업 자동화하기 학습중..
import time, threading
print('start of game')
def takeaNap() :
i = 0
while True :
time.sleep(1)
print('wake up! : #{}'.format(i))
i += 1
if input() :
print('key')
continue
else :
print('else')
pass
break
threadObj = threading.Thread(target = takeaNap)
threadObj.start()
print('end of game')
threadobj2 = threading.Thread(target = print, args = ['Cats', 'Dogs', 'Frogs'], kwargs = {'sep' : '&'})
# 새 thread에 매개변수 전달할 때는 args 및 kwargs를 사용해야 한다. print(A)형태가 아닌 print 형태로 전달
# https://nostarch.com/automatestuff2 참고 사이트
# https://automatetheboringstuff.com/ website 참고
threadobj2.start()
#! python3
# downloadXkcd.py - Downloads every single XKCD comic.
import requests, os, bs4, threading, sys
os.makedirs('xkcd', exist_ok=True) # store comics in ./xkcd
print("Start!!")
def downloadXkcd(startComic, endComic) :
for urlNumber in range(startComic, endComic) :
print('download pages https://xkcd.com/%s...' %(urlNumber))
res = requests.get('https://xkcd.com/%s' % (urlNumber))
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text, features = 'lxml')
comicElem = soup.select('#comic img')
if comicElem ==[] :
print('could not find comic image')
else :
comicUrl = 'https:' + comicElem[0].get('src')
print(comicUrl)
print('downloading img %s...' % (comicUrl))
res = requests.get(comicUrl)
res.raise_for_status()
imageFile = open(os.path.join('xkcd', os.path.basename(comicUrl)), 'wb')
for chunk in res.iter_content(100000) :
imageFile.write(chunk)
imageFile.close()
downloadThreadS = []
for i in range(1, 10, 1) :
downloadThread = threading.Thread(target = downloadXkcd, args = (i, i + 99))
downloadThreadS.append(downloadThread)
downloadThread.start()
for downloadThread in downloadThreadS :
downloadThread.join() #스레드가 종료될 때까지는 다음 코드 실행을 막아준다.
print('done')
'파이썬(PYTHON)' 카테고리의 다른 글
pyautogui 모듈 활용 연습 (0) | 2020.08.08 |
---|---|
subprocess 모듈 활용 연습 (0) | 2020.08.08 |
Time 모듈 연습 (0) | 2020.08.04 |
DataFrame 연습 (0) | 2020.08.04 |
PyQt5를 이용한 기본 UI 구성 (0) | 2020.08.03 |