분류 전체보기79 [Scrapy] 09. Settings URL : https://de.globalvoices.org contents 1. settings.py - 다양한 설정 방법 2. 각 설정 항목 상세 설명 3. News 사이트 크롤링 연습 4. 기타 항목(캐시, 미들웨어) 1. settings.py - 다양한 설정 방법 Scrapy 환경설정 실행 방법 (1) 커맨드 라인 실행 -> scrapy crawl 크롤러명 -s(--set) = (2) Spider 실행시 직접 지정 custom_settings = {'DOWNLOAD_DELAY' : 3} (3) Settings.py에 지정 -> 추천 (4) 서브 명령어(신경x) (5) 글로벌 설정 : scrapy.settings.default_settings 2. 각 설정 항목 상세 설명 # -*- coding: .. 2020. 4. 10. [Scrapy] 09. Exports URL : https://docs.scrapy.org/en/latest/topics/feed-exports.html# 1. 출력 형식 JSON, JSON Lines(Java Script Object Notation) CSV(쉼표로 구분한 텍스트) XML(마크업 언어) Pickle(python 자료형을 저장) Marshal(바이너리 형식) 2. 저장 위치 Local File System(My Pc) FTP - (server) S3 - (AWS) Amazon 3. 저장 방법 (1) 기본 콘솔 cmd : (--output, -o), (--output-format, -t) 옵션 설정 --set=FEED_EXPORT_INDENT = 2 (2) Settings.py 파일 사용 자동으로 저장(파일명, 형시, 위치) .. 2020. 4. 8. [Scrapy] 08. Items URL : https://www.itnews.com/ content 1. Items 사용 이유 2. IT News 사이트 크롤링 연습 3. 메인 페이지 -> 상세 페이지 크롤링 4. Item 선언 및 수집 데이터 mapping 1. Items 사용 이유 scrapy에서 제공하는 자료구조 클래스이다. Items 클래스를 정의하고 우리가 수집하고자 하는 정보들을 명시하면, Spider 상에서 스크래핑을 수행한 결과물을 파일형태로 저장할 때, items 객체를 이용해 간편하게 관리 가능하다. 장점 (1) 수집 데이터를 일관성있게 관리 가능 (2) 데이터를 Dictionary로 관리, 오타 방지 -> Directory(dir)을 확인함으로써 알 수 있다. (3) 추후 가공 및 DB 저장 용이 2. IT News.. 2020. 4. 8. [Scrapy] 07. Selectors(css, xpath) url : https://www.w3scrhools.com content 1. Css Selector 2. xpath Selector 3 . 크롤링 실습 4. 참고 사이트 * Xpath 도움 사이트 https://docs.scrapy.org/en/latest/topics/selectors.html#working-with-relative-xpaths http://www.nextree.co.kr/p6278/ * css 선택자 도움 사이트 https://docs.scrapy.org/en/latest/topics/selectors.html#extensions-to-css-selectors 들어가기 전에... 타겟 데이터는 크롬 개발자 도구 사용 선택자 연습 팁 : scrapy shell 에서 테스트(효율성) s.. 2020. 4. 8. [Scrapy] 06. Scrapy Spider 활용 content 1. Multi Domain 2. spider Attribute 3. Logger 4. Response 분기 1. Multi Domain 여러 도메인에 동시에 접근 가능하게 해준다. (1) start_urls list에 url 추가. (2) create function -> yield scrapy.Request(url, self.parse) 2. spider Attribute 스파이더 종류 : CrawlSpider, XMLFeedSpider, CSVFeedSpider, SitemapSpider custom_settings : 사용자 시스템 설정(setting) start_urls : response 받을 url 설정 name : spider의 name 설정 Logger dir(self) 를.. 2020. 4. 8. [Scrapy] 05. Shell 사용법 content 1. shell 기본 사용법 2. 명령어 사용 실습 3. 대상 데이터 수집 실습(css, xpath) 4. Fetch, View, response, request shell 기본 사용법 shell이란? 스파이더를 실행하지 않고도 스크래핑 코드를 빠르게 시도하고 디버깅 할 수 있다. 데이터 추출 코드를 테스트 하는 데 사용하지만, 일반 python 셸이므로 모든 종류의 코드를 테스트 가능하다. (1) 방법1 cmd : scrapy shell cmd : fetch('https://blog.scrapinghub.com') cmd : response (2) 방법2 cmd : scrapy shell url (3) 방법3 shell은 로컬 파일에서도 동작 가능하다. cmd : scrapy shell .. 2020. 4. 7. [HackerRank] 27. Picking Numbers def pickingNumbers(a): high = 1 low = 1 maximum = 0 for n in a: high = a.count(n+1) + a.count(n) low = a.count(n-1) + a.count(n) maximum = max(maximum, high, low) return maximum if __name__ == '__main__': n = int(input().strip()) a = list(map(int, input().rstrip().split())) result = pickingNumbers(a) print(result) 2020. 4. 7. [HackerRank] 26. forming a magic square def CreateMagicSquare(): # 3x3 magic square 모든 경우의 수 생성 num = set(range(1, 10)) group = [] magic_square = [] for a in range(1, 10): for b in range(1, 10): for c in range(1, 10): if (a + b + c == 15) & (a != b) & (a != c) & (b != c): temp = [a, b, c] group.append(temp) for i in range(len(group)): for j in range(len(group)): for k in range(len(group)): if set(group[i] + group[j] + group[k]) == num.. 2020. 4. 7. [HackerRank] 25. Cats and Mouse # My code def catAndMouse(a, b, c): a_distance = abs(a - c) b_distance = abs(b - c) if a_distance > b_distance: return 'Cat B' elif a_distance < b_distance: return 'Cat A' else: return 'Mouse C' if __name__ == '__main__': q = int(input()) for q_itr in range(q): abc = input().split() a = int(abc[0]) b = int(abc[1]) c = int(abc[2]) result = catAndMouse(a, b, c) print(result) 2020. 4. 7. [HackerRank] 24. Electronics shop # My code def getMoneySpent(keyboards, drives, b): temp = -1 for keyboard in keyboards: for drive in drives: if keyboard + drive 2020. 4. 7. [HackerRank] 23. Apple and orange Implementation # 방법1 # Complete the countApplesAndOranges function below. def countApplesAndOranges(s, t, a, b, apples, oranges): # Computing distance apples = [a + apple for apple in apples] oranges = [b + orange for orange in oranges] # apples & oranges in the range in_apples = [apple for apple in apples if (apple >= s) and (apple = s) and (orange 2020. 4. 7. [HackerRank] 22. Between Two Sets Between Two Sets You will be given two arrays of integers and asked to determine all integers that satisfy the following two conditions: The elements of the first array are all factors of the integer being considered The integer being considered is a factor of all elements of the second array 문제 요약 A의 요소로 나누어 떨어지고 B 요소를 나누어 떨어지게 하는 수의 개수 # 방법1 import math from functools import reduce # # Complet.. 2020. 4. 7. 이전 1 2 3 4 5 ··· 7 다음