0w0

[parser] requests.get(), urllib.request.urlopen().read() 본문

Coding/Python

[parser] requests.get(), urllib.request.urlopen().read()

0w0 2019. 8. 20. 10:09
728x90
반응형

 

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
31
 
import sys,io,time
from bs4 import BeautifulSoup
 
#requests 모듈 사용
import requests
html=requests.get('https://search.naver.com/search.naver?query=날씨')
soup=BeautifulSoup  (html.text,'html.parser')
data1 = soup.find('span',{'class':'todaytemp'})
print("현재온도:", data1.text)
 
 
print("==============================")
 
 
#urllib.request 모듈 사용
import urllib.request
html=urllib.request.urlopen('https://search.naver.com/search.naver?query=%EB%82%A0%EC%94%A8').read()
soup=BeautifulSoup  (html,'html.parser')
data2 = soup.find('span',{'class':'todaytemp'})
print("현재온도:", data2.text)
 
 
 
#requests.get()으로 코드 파싱
#-soup에서 html.text 처럼 text 확장자를 붙어줘야함.
#-get으르 코드를 파싱할 경우 uri에 한글이 있어도 가능
 
#urllib.request.urlopen().read()으로 코드 파싱
#-soup에서 text 확장자 붙이면 안됨.
#-urlopen으로 코드를 파싱할 경우 uri에 한글이 있으며 에러가 발생할 수도 있음.
cs

 

 

728x90
반응형
Comments