INSTALLATION
#Raspberrypi stretch full version 에서는 beautifulsoup 내장함, lite version 에서는 별도 설치 필요
#beautifulsoup4 및 bs4 설치 및 활용
#beautifulsoup4는pip3에서 설치
sudo apt-get install python3-pip
sudo pip3 install beautifulsoup4
#bs4는 python2 에서 설치해야됨
sudo apt-get install python-pip
sudo pip install bs4
#파이썬을 실행하여 프롬프트에 >>> from bs4 import BeautifulSoup 을 쳐도 에러가 발생하지 않는다면 정상적으로 설치된것
UrlLib 또는 requests 로 사이트를 불러와서 beautifulsoup 또는 RE 로 parsing 함
1. READ WEB DATA
REQUESTS
import requests
import bs4
res = requests.get('http://heeseop.com/')
res.raise_for_status()
res.encoding='euc-kr'
html = res.text
soup = bs4.BeautifulSoup(html, 'html.parser')
# the second, By requests
# -*- coding: UTF-8 -*-
import requests
soup = BeautifulSoup(res, "html.parser") print(soup)
#With Session
import requests
from bs4 import BeautifulSoup as bs
with requests.Session() as s:
first_page = s.get('http://target.site')
html = first_page.text
print(html)
URLLIB
#python2 에서는 urllib2, python3에서는 urllib3 를 사용한다?? << 확인
# -*- coding: UTF-8 -*-
import urllib2
from bs4 import BeautifulSoup
TargetURL = "https://target.site"
res = urllib2.urlopen(TargetURL)
soup = BeautifulSoup(res, "html.parser") print(soup)
#python3 예제 urllib2 와 urllib3는 다른 명령어를 갖는다
from bs4 import BeautifulSoup
import urllib3
http = urllib3.PoolManager()
url = 'http://target.site'
response = http.request('GET', url)
soup = BeautifulSoup(response.data)
print(soup)
URLLIB 두번째
#python3 에서 사용가능
import urllib.request
TargetURL = "https://target.page/"
res = urllib.request.Request(TargetURL)
response = urllib.request.urlopen(res)
page = response.read().decode("utf-8")
print(page)
2. PARSING
# all 이 없을경우 첫번째값 추출, 포함할 경우 array 로 모든 수 포함
# 태그로 찾기 (첫번째 항목)
http://devnauts.tistory.com/120
BEAUTIFULSOUP EXAMPLES
# <p> 중에 info_temperature 안에 todaytemp 에 해당되는 태그를 제외한 텍스트 값을 ctemp 에 저장
ctemp = soup.find('p', class_='info_temperature').find('span', class_='todaytemp').text
sampleURL3 = "http://HERE"
res = urllib2.urlopen(sampleURL3)
soup3 = BeautifulSoup(res, "html.parser")
htmltext = str(soup3)
m = re.search(r'(temp=\"\d\d\")', htmltext)
m2 = str(m.group(0))
m3 = m2.replace("temp=", "")
ctemp = m3.replace("\"","")
RE
#정규식 파싱
# 참조 : https://blog.heeseop.com/82
# 참조 : https://regexr.com/
# r'(이안에서 찾아)' 이라는 표현은 정규식에서 찾으라는 것
# 따옴표(") 앞에는 \를 써줘야함
import re
m = re.search(r'(temp=\"\d\d\"|temp=\"\d\"|temp=\"\-\d\d\"|temp=\"\-\d\")', soup)
hum = re.search(r'(humidity=\"\d\d\")', soup)
wcode = re.findall(r'(code=\"\d\d\")', soup)
wdate = re.findall(r'(day=\"\S\S\S\")', soup)
whigh = re.findall(r'(high=\"\d\d\"|high=\"\d\")', soup)
wlow = re.findall(r'(low=\"\d\d\"|low=\"\d\")', soup)
wlow = re.findall(r'(low=\"\d\d\"|low=\"\d\"|low=\"\-\d\d\"|low=\"\-\d\")', soup)
'Computer > Programing' 카테고리의 다른 글
Python data structure 파이썬 자료구조 (0) | 2019.02.07 |
---|---|
Python load json from web (0) | 2019.02.07 |
Telegram URL 이용하여 문자 보내는법 (0) | 2019.02.07 |
Python 실행값을 PHP로 불러오기 (0) | 2019.02.07 |
텍스트 분석 후 특정파일만 출력 (0) | 2019.02.07 |
Snoopy 이용법해서 파싱 방법 (0) | 2019.02.07 |
텔레그램 봇 TelegramBotPHP 활용 ( 문자 및 사진보내기 ) (0) | 2019.02.07 |
Mysql 입력 출력 (0) | 2019.02.07 |