일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 클론
- 깃 토큰
- 파이썬
- Visual Studio Code
- 단축키
- error
- 오류
- cmd
- 에러
- visualstudio code
- import data
- jupyter
- console창
- github token
- 따옴표 삭제
- csv
- OrCAD 다운로드
- vscode
- Python
- run sql script
- 데이터베이스
- localhost
- MySQL
- database
- DataGrip
- github clone
- clone
- error 해결
- php
- PHPStorm
Archives
- Today
- Total
개발 노트
파이썬을 이용하여 크롤링(Crawling) 하기 20/7/16 본문
인턴 앱 개발 : 20.07.06~08.31/Crawling : Python
파이썬을 이용하여 크롤링(Crawling) 하기 20/7/16
hayoung.dev 2020. 8. 2. 19:341. 파이썬, 아나콘다, 크롬 드라이버를 설치한다.
2. 아나콘다에서 폴더를 생성하고 파이썬으로 설정한다.
3. pip install request(URL 불러오는 함수),
BeautifulSoup4(웹 크롤링을 위한 기초 모듈),
pandas(수집한 자료를 엑셀에 저장시켜 주는 모듈)
datatime(현재 연 월 일 시초 불러오는 함수)를 설치한다.
4. 크롤링 하려는 부분의 xpath를 얻는다.
1) 크롤링 하고 싶은 사이트를 chrome으로 들어가서 f12를 누르거나
마우스 우클릭 - 검사 를 누른다.
2) 뜨는 콘솔 창에서 사진의 표시된 부분을 누르고 크롤링하려는 부분을 클릭하면 그 부분의 코드를 알 수 있다.
참고로 크롤링을 하다 보면 밑 사진과 같이
"Chrome이 자동화된 테스트 소프트웨어에 의해 제어되고 있습니다."
라는 문구와 함께 python을 통해 제어되는 크롬 창이 뜨는데,
이 창에서 xpath를 추출하는 것이 아니므로 유의한다.
5. 내가 짠 python 코드
In [1] : import pandas as pd
// import는 pandas 모듈을 불러오겠다는 뜻. as는 pd라는 단어로 pandas를 대체하겠다는 뜻.
import numpy as np
import platform
import matplotlib.pyplot as plt
%matplotlib inline
path = "c:/windows/Fonts/malgun.ttf"
from matplotlib import font_manager, rc
if platform.system() =='Darwin':
rc('font', family='AppleGothic')
elif platform.system() == 'Windows':
font_name = font_manager.FontProperties(fname=path).get_name()
rc('font', family=font_name)
else:
print('Unknown system... sorry')
plt.rcParams['axes.unicode_minus'] = False
In [2] : from selenium import webdriver
import time
In [3] : driver = webdriver.Chrome(C:\\Users\HM4\Chromedriver\chromedriver')
driver.get("https://covid19healthbot.cdc.gov/?language=en-us")
In [4] : driver.find_element_by_xpath("""//*[@id="webchat"]/div/div[2]/div/ul/li[2]/div/div[2]/div[2]/div/div/div/div/div[3]/div[1]/div/button[1]""").click()
//앞에서 얻은 xpath 주소를 """ """ 안에 첨부. path의 화면으로 진입함.
In [5] : driver.find_element_by_xpath("""//*[@id="webchat"]/div/div[2]/div/ul/li[6]/div/div[2]/div[2]/div/div/div/div/div[3]/div[1]/div/button[1]""").click()
In [6] : driver.find_element_by_xpath("""//*[@id="webchat"]/div/div[2]/div/ul/li[8]/div/div[2]/div[2]/div/div/div/div/div[1]/div/select""").click()
In [7] : loca_list_raw = driver.find_element_by_xpath("""//*[@id="webchat"]""")
loca_list = loca_list_raw.find_elements_by_tag_name("option")
loca_names_values = [option.text for option in loca_list]
loca_names_values = loca_names_values[1:]
loca_names_values
Out [7] : //선택한 xpath 안에 있는 list가 전부 출력되어 저장됨.
['Please select a location',
'Alabama',
'Alaska',
'American Samoa',
'Arizona',
'Arkansas',
'California',
'California - Los Angeles County',
'Colorado',
'Connecticut',
'Delaware',
'District Of Columbia',
'Federated States Of Micronesia',
'Florida',
'Georgia',
'Guam',
'Hawaii',
'Idaho',
'Illinois',
'Illinois – Chicago',
'Indiana',
'Iowa',
'Kansas',
'Kentucky',
'Louisiana',
'Maine',
'Marshall Islands',
'Maryland',
'Massachusetts',
'Michigan',
'Minnesota',
'Mississippi',
'Missouri',
'Montana',
'Nebraska',
'Nevada',
'New Hampshire',
'New Jersey',
'New Mexico',
'New York',
'New York – New York City',
'North Carolina',
'North Dakota',
'Northern Mariana Islands',
'Ohio',
'Oklahoma',
'Oregon',
'Palau',
'Pennsylvania',
'Puerto Rico',
'Rhode Island',
'South Carolina',
'South Dakota',
'Tennessee',
'Texas',
'Utah',
'Vermont',
'Virgin Islands',
'Virginia',
'Washington',
'West Virginia',
'Wisconsin',
'Wyoming']
반응형
'인턴 앱 개발 : 20.07.06~08.31 > Crawling : Python' 카테고리의 다른 글
파이썬을 이용한 크롤링 연습 소스코드 20/7/15 (0) | 2020.08.07 |
---|---|
파이썬 오류 해결 방법 : pip install BeautifulSoup : Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output. (0) | 2020.08.06 |
파이썬 기초 사용하기(데이터 반환, 열의 이름 바꾸기) (0) | 2020.08.02 |
파이썬 셀레니움에서 error: unicodeescape \UXXXXXXXX escape 해결법 (0) | 2020.08.02 |
cmd로 python 입력할 때 오류 뜨는 경우 (0) | 2020.08.02 |