본문 바로가기

크롤링

(9)
Python - 한국기상청 도시별 현재 날씨 정보 분석 후 csv 저장 1. import moduleimport requests from bs4 import BeautifulSoup as BS 2. Sample URL : http://www.weather.go.kr/weather/observation/currentweather.jsp 3. HTML Parsing Codeimport requests from bs4 import BeautifulSoup as BS url = 'http://www.weather.go.kr/weather/observation/currentweather.jsp' response = requests.get(url) if response.status_code != 200: print("%d 에러가 발생했습니다." % response.status_code..
Python - webdriver headless Mode(창 숨김 모드) 1. 크롤링 자동화 하다 의문 사항 - 크롬 드라이버를 로드 해서 크롬을 새로 하나 여는거 말고 창을 숨김으로 백그라운도로 로드하고 열수 있지 않을까? 2. 코드 작성 - 기존 기본 webdriver 코드from selenium import webdriver driver = webdriver.Chrome('C:/chromedriver/chromedriver') driver.implicitly_wait(3) driver.get('사이트 주소') - 수정 headless Mode 코드from selenium import webdriver # 크롬 headless 모드 실행 chrome_options = webdriver.ChromeOptions() chrome_options.add_argument('head..
190302>Python - Naver 증권 일일 시세 크롤링 Q. Naver 증권 일일 시세 크롤링 하기(금융코드 035420) A.import requests from bs4 import BeautifulSoup as BS def refine_price(text): price = int(text.replace(",", "")) return price url = "https://finance.naver.com/item/sise_day.nhn?code=035420" response = requests.get(url) text = response.text html = BS(text, 'html.parser') tr_list = html.find_all("tr", {"onmouseover":"mouseOver(this)"}) for tr in tr_list: date ..
190224> Python-NaverNews 사용자 입력 받아 크롤링 Q.사용자에게 입력 받아 네이버 뉴스를 크롤링해보자. A.import requests from bs4 import BeautifulSoup as BS def list2dict(keywords): keys = {} for keyword in keywords: keys[keyword]=0 return keys date = input("언제[YYYYmmdd] 기사를 검색할까요? : ") pages = int(input("총 몇 페이지를[20/page]를 검색할까요? : ")) keywords = input("관심있는 단어를 띄어쓰기로 입력하세요 : ").split() keys = list2dict(keywords) number = 1 news= [] for page in range(1, pages + 1): ..
190224> Python-NaverNews 사진 + 뉴스 제목 + 요약 + 제공자 크롤링 Q. 네이버 뉴스 중 속보에 해당하는 사진 + 뉴스 제목 + 요약 + 제공자를 크롤링 해보자. A.import requests from bs4 import BeautifulSoup r = requests.get("https://news.naver.com/main/list.nhn?mode=LSD&mid=sec&sid1=100") c = r.content soup = BeautifulSoup(c, "html.parser") all=soup.find("ul",{"class":"type06_headline"}) # print(all) all2 = all.find_all(."li") # print(all2[0]) for item in all2: try: img = item.find("dt",{"class":"ph..
190224> Python-Naver News 크롤링 Q. 네이버 메뉴 중 색칠 칠한 뉴스란을 크롤링해보자. A.import urllib.request import bs4 url = "https://www.naver.com/" html = urllib.request.urlopen(url) bs_obj = bs4.BeautifulSoup(html, "html.parser") ul = bs_obj.find("ol", {"class":"ca_l"}) lis = ul.findAll("li") for li in lis: a = li.find("a") O.소식통 "김정은 전용열차, 톈진역 통과해 남행 관측"北美 하노이 의제협상팀, 정상회담 'D-3' 맞아 '숨고르기'4대강 보 해체 논란…"900억 들여 해체?"vs"유지비 1천700억""'환경부 블랙리스트' 보은성 인..
190224> Python-Naver Menu 크롤링 Q. Q-1. 메일 / 카페 / 블로그 / 지식in / 쇼핑 / 네이버페이 / 네이버티비Q-2. 사전 / 뉴스 / 증권 / 부동산 / 지도 / 영화 / 뮤직 / 책 / 웹툰네이버 상기 메뉴를 크롤링 해보자. A.A-1.import urllib.request import bs4 url = "https://www.naver.com/" html = urllib.request.urlopen(url) bs_obj = bs4.BeautifulSoup(html, "html.parser") ul = bs_obj.find("ul", {"class":"an_l"}) lis = ul.findAll("li") for li in lis: a_tag = li.find("a") span = a_tag.find("span",{"cl..
190224> Jupyter Notebook으로 확인하는 BeautifulSoup In [32]: #-*- coding: utf-8 -*- from bs4 import BeautifulSoup as bs In [33]: html_doc = """ 초콜릿 과자 사탕 오렌지 """ In [34]: # html 파서를 이용해서 html 형태의 데이터를 읽어온다. soup = bs(html_doc, 'html.parser') In [35]: # 1. td 태그를 모두 찾아서 td_list 담은 후, 루프를 돌리면서 각 td 태그 내용을 출력한다. td_list = soup.find_all('td') In [36]: for td_item in td_list: print(td_item.string) 사탕 오렌지 In [37]: # 2. id 가 choco 인 항목을 찾아서 해당 태그 내용을 출력..