update multiprocessing for crwaling
- 셀레니움은 직접 크롬드라이버를 통해 html을 읽기 때문에 느리고, 크롬창이 켜진다. - 이를 보안하기 위해 크롬창을 headless를 통해 안보게 해주었다. - 멀티프로세싱을 통해 한번에 3개 크롬드라이버를 띄워 단어 3개에 대해 동시에 크롤링 할 수 있게 했다.
Showing
2 changed files
with
28 additions
and
16 deletions
| ... | @@ -3,17 +3,23 @@ from selenium.webdriver.common.keys import Keys | ... | @@ -3,17 +3,23 @@ from selenium.webdriver.common.keys import Keys |
| 3 | import time | 3 | import time |
| 4 | import urllib.request | 4 | import urllib.request |
| 5 | import os | 5 | import os |
| 6 | +from multiprocessing import Pool #멀티쓰레딩 | ||
| 6 | 7 | ||
| 7 | # 구글 드라이버를 크롬 버전에 맞게 설치하고 경로를 입력해준다. | 8 | # 구글 드라이버를 크롬 버전에 맞게 설치하고 경로를 입력해준다. |
| 8 | # 이곳에 크롬 드라이버 경로를 입력해주세요. | 9 | # 이곳에 크롬 드라이버 경로를 입력해주세요. |
| 9 | - | ||
| 10 | chromedriver_path = "/Users/kimtaeyoung/Desktop/project/opensource_termproject/2020-02-OSS-TermProject/python/chromedriver" | 10 | chromedriver_path = "/Users/kimtaeyoung/Desktop/project/opensource_termproject/2020-02-OSS-TermProject/python/chromedriver" |
| 11 | - | ||
| 12 | # 몇개의 파일을 크롤링할지 | 11 | # 몇개의 파일을 크롤링할지 |
| 13 | crawling_num = 500 | 12 | crawling_num = 500 |
| 14 | 13 | ||
| 15 | -# 폴더 생성해주는 함수 | 14 | +# headless 셀레니움 |
| 15 | +# 크롬 드라이버에 적용할 옵션들 | ||
| 16 | +options = webdriver.ChromeOptions() | ||
| 17 | +options.add_argument('headless') | ||
| 18 | +options.add_argument('window-size=1920x1080') | ||
| 19 | +options.add_argument("disable-gpu") | ||
| 20 | +# 혹은 options.add_argument("--disable-gpu") | ||
| 16 | 21 | ||
| 22 | +# 폴더를 확인하고 없다면 만들어준다. | ||
| 17 | def createFolder(directory): | 23 | def createFolder(directory): |
| 18 | try: | 24 | try: |
| 19 | if not os.path.exists(directory): | 25 | if not os.path.exists(directory): |
| ... | @@ -23,7 +29,6 @@ def createFolder(directory): | ... | @@ -23,7 +29,6 @@ def createFolder(directory): |
| 23 | 29 | ||
| 24 | # 크롤링 할 단어들을 받아온다. | 30 | # 크롤링 할 단어들을 받아온다. |
| 25 | # 크롤링할 단어는 keywords.txt에 입력하면 된다. | 31 | # 크롤링할 단어는 keywords.txt에 입력하면 된다. |
| 26 | - | ||
| 27 | def get_keywords(keywords_file='python/keywords.txt'): | 32 | def get_keywords(keywords_file='python/keywords.txt'): |
| 28 | # read search keywords from file | 33 | # read search keywords from file |
| 29 | with open(keywords_file, 'r', encoding='utf-8-sig') as f: | 34 | with open(keywords_file, 'r', encoding='utf-8-sig') as f: |
| ... | @@ -41,16 +46,13 @@ def get_keywords(keywords_file='python/keywords.txt'): | ... | @@ -41,16 +46,13 @@ def get_keywords(keywords_file='python/keywords.txt'): |
| 41 | 46 | ||
| 42 | return keywords | 47 | return keywords |
| 43 | 48 | ||
| 44 | -#모델 폴더를 만들어준다. | 49 | +# 크롤링 |
| 45 | - | 50 | +# get_keywords의 결과를 파라미터로 넣어주면 된다. |
| 46 | -createFolder("python/model/") | 51 | +def crawling(search_name): |
| 47 | - | 52 | + driver = webdriver.Chrome(chromedriver_path, chrome_options=options) #headless를 위한 옵션을 추가 |
| 48 | -for i in get_keywords() : | ||
| 49 | - | ||
| 50 | - driver = webdriver.Chrome(chromedriver_path) | ||
| 51 | driver.get("https://www.google.co.kr/imghp?hl=ko&tab=wi&authuser=0&ogbl") | 53 | driver.get("https://www.google.co.kr/imghp?hl=ko&tab=wi&authuser=0&ogbl") |
| 52 | elem = driver.find_element_by_name("q") | 54 | elem = driver.find_element_by_name("q") |
| 53 | - elem.send_keys(i) | 55 | + elem.send_keys(search_name) |
| 54 | elem.send_keys(Keys.RETURN) | 56 | elem.send_keys(Keys.RETURN) |
| 55 | 57 | ||
| 56 | SCROLL_PAUSE_TIME = 1 | 58 | SCROLL_PAUSE_TIME = 1 |
| ... | @@ -74,17 +76,18 @@ for i in get_keywords() : | ... | @@ -74,17 +76,18 @@ for i in get_keywords() : |
| 74 | count = 1 | 76 | count = 1 |
| 75 | 77 | ||
| 76 | #폴더가 없다면 폴더를 만들어준다. | 78 | #폴더가 없다면 폴더를 만들어준다. |
| 77 | - createFolder("python/model/"+i) | 79 | + createFolder("python/model/"+search_name) |
| 78 | 80 | ||
| 79 | for image in images: | 81 | for image in images: |
| 80 | try: | 82 | try: |
| 81 | image.click() | 83 | image.click() |
| 84 | + # 이미지가 로딩되는 속도. 안정적으로는 2-3초가 적당. | ||
| 82 | time.sleep(2) | 85 | time.sleep(2) |
| 83 | imgUrl = driver.find_element_by_xpath('/html/body/div[2]/c-wiz/div[3]/div[2]/div[3]/div/div/div[3]/div[2]/c-wiz/div[1]/div[1]/div/div[2]/a/img').get_attribute("src") | 86 | imgUrl = driver.find_element_by_xpath('/html/body/div[2]/c-wiz/div[3]/div[2]/div[3]/div/div/div[3]/div[2]/c-wiz/div[1]/div[1]/div/div[2]/a/img').get_attribute("src") |
| 84 | opener=urllib.request.build_opener() | 87 | opener=urllib.request.build_opener() |
| 85 | opener.addheaders=[('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1941.0 Safari/537.36')] | 88 | opener.addheaders=[('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1941.0 Safari/537.36')] |
| 86 | urllib.request.install_opener(opener) | 89 | urllib.request.install_opener(opener) |
| 87 | - urllib.request.urlretrieve(imgUrl, "python/model/"+i+"/"+str(count) + ".jpg") | 90 | + urllib.request.urlretrieve(imgUrl, "python/model/"+search_name+"/"+str(count) + ".jpg") |
| 88 | count = count + 1 | 91 | count = count + 1 |
| 89 | 92 | ||
| 90 | # 크롤링할 개수 설정. | 93 | # 크롤링할 개수 설정. |
| ... | @@ -93,4 +96,13 @@ for i in get_keywords() : | ... | @@ -93,4 +96,13 @@ for i in get_keywords() : |
| 93 | except: | 96 | except: |
| 94 | pass | 97 | pass |
| 95 | 98 | ||
| 96 | - driver.close() | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| 99 | + # 끝나고 크롬 드라이버를 종료해준다 | ||
| 100 | + driver.close() | ||
| 101 | + | ||
| 102 | +if __name__ == '__main__': | ||
| 103 | + | ||
| 104 | + #모델 폴더를 만들어준다. | ||
| 105 | + createFolder("python/model/") | ||
| 106 | + | ||
| 107 | + pool = Pool(processes=3) # 3개의 프로세스를 사용합니다. | ||
| 108 | + pool.map(crawling, get_keywords()) | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
-
Please register or login to post a comment