python2019. 12. 22. 14:54
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
32
33
34
35
import requests
from bs4 import BeautifulSoup
 
 
res = requests.get('https://www.naver.com/')
#res = requests.get('https://easy16.tistory.com/')
 
 
soup= BeautifulSoup(res.content, 'html.parser')
 
 
#Perform a CSS selection operation on the current element.
 
#1,tag 선택
#data = soup.select('div')
 
#2,tag 및 class 선택
#data = soup.select('div.title')
 
#4,class 선택
#data = soup.select('.banner_area')
 
#5,class 계층 선택
#data = soup.select('div.layer_plus._plusAlert strong.tit')
 
#6, id 선택
#data = soup.select('#whale_promotion_banner')
#data = soup.select('form#sfrom')
 
#7, id + class 복합 선택
data = soup.select('div.area_navigation ul#PM_ID_serviceNavi')
 
 
for item in data:
    print(item.get_text())
Posted by easy16
python2019. 12. 19. 23:11

크롬에서 F12 키 또는 Ctrl+Shift+i 를 입력하면 개발자 모드를 통해 

원하는 링크의 html 태그 부분을 쉽게 확인 가능하며, attrs 및 class 또는 id를 활용하여 target을 추출할 수 있으면 충분하다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#다음 실시간 검색어 받아오기
import requests
 
from bs4 import BeautifulSoup
 
html = requests.get('https://www.naver.com/')
 
soup = BeautifulSoup(html.content, "html.parser")
#data = soup.find('title')
 
#print(data.get_text())
#data = soup.find('h3',id="articleTitle")
#data = soup.find('p', attrs = { 'class':'head_channel_layer'})
data_list = soup.find_all('a', attrs={'class':"link_issue", 'tabindex':'-1'})
 
 
 
#print(data.string)
for data in set(data_list) :
    print(data.get_text())

 

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#네이버 실시간 검색어 받아오기
import requests
from bs4 import BeautifulSoup
 
html = requests.get('https://www.naver.com')
soup = BeautifulSoup(html.content, "html.parser")
 
rank_list = soup.find('div',"ah_roll_area PM_CL_realtimeKeyword_rolling")
#결과물에 다시 find를 사용 가능함.
item_list = rank_list.find_all('li',"ah_item")
 
for d in item_list:
        text = d.get_text().strip().split()
        print(text[0],"위 : ",text[1])

'python' 카테고리의 다른 글

크롤링 연습 4. 순위 가져오기  (0) 2019.12.24
크롤링 연습 3. select  (0) 2019.12.22
크롤링 연습 1. find, find_all  (0) 2019.12.17
출력 format 예제  (0) 2019.12.08
jupyter note login token 확인 방법  (0) 2019.12.07
Posted by easy16
python2019. 12. 17. 23:11
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
import requests
 
from bs4 import BeautifulSoup
 
 
soup = BeautifulSoup(html.content, "html.parser")
 
 
#h1로 시작하는 첫번째 tag를 가져옴.
#data = soup.find('h1')
#data = soup.find('p')
#속성(attr)를 인자로 지정하는 여러가지 방법
#data = soup.find('p', class_='head_channel_layer')
#data = soup.find('p','head_channel_layer')
#data = soup.find('p', attrs = { 'class':'head_channel_layer'})
 
data = soup.find(id="articleBodyContents")
 
#print(data)
print(data.get_text())
 
 
#리스트 형태로 가져옴
data = soup.find_all('p')
for item in data:
    print(item.string)

'python' 카테고리의 다른 글

크롤링 연습 3. select  (0) 2019.12.22
크롤링 연습 2. 실시간 검색어  (0) 2019.12.19
출력 format 예제  (0) 2019.12.08
jupyter note login token 확인 방법  (0) 2019.12.07
try except 문  (0) 2019.11.20
Posted by easy16
python2019. 12. 8. 14:40

 

 

ex)

print(" {1} {0} ".format("a","b"))

b a

 

ex)

pie = 3.1456
print("pie = ",format(pie, ".2f"))

pie = 3.15

 

ex)

print(" %5.2s %15.2f" % ("test", 3.1415) )

te 3.14

'python' 카테고리의 다른 글

크롤링 연습 2. 실시간 검색어  (0) 2019.12.19
크롤링 연습 1. find, find_all  (0) 2019.12.17
jupyter note login token 확인 방법  (0) 2019.12.07
try except 문  (0) 2019.11.20
Django test example  (0) 2019.07.19
Posted by easy16
python2019. 12. 7. 22:06

 

Anaconda Powershell prompt 실행 후,

하기 커맨드 입력

(base) PS C:\Users\Lee> jupyter notebook list

'python' 카테고리의 다른 글

크롤링 연습 1. find, find_all  (0) 2019.12.17
출력 format 예제  (0) 2019.12.08
try except 문  (0) 2019.11.20
Django test example  (0) 2019.07.19
Django install guide (link)  (0) 2019.07.19
Posted by easy16
python2019. 11. 20. 10:08

try : 에러 여부를 판단할 코드 부

except : 에러를 처리

finally : 위의 두가지 문과 상관없이 실행되는 부

 

 Ex)

try:
  print(x)
except:
  print("An exception occurred")

 

출처 : https://www.w3schools.com/python/python_try_except.asp

'python' 카테고리의 다른 글

출력 format 예제  (0) 2019.12.08
jupyter note login token 확인 방법  (0) 2019.12.07
Django test example  (0) 2019.07.19
Django install guide (link)  (0) 2019.07.19
python3 version 선택  (0) 2019.07.19
Posted by easy16
python2019. 7. 19. 14:28

 

test example..

 

아래 명령어로 확인시 3.7.x... required 버전은 3.8 이상..

(업데이트가 번거로운데 Ubuntu 버전이 낮아서 그런걸로 보임)

$python -c "import sqlite3; print sqlite3.sqlite_version"

 

테스트 목적이므로 old 버전을 설치하여 회피,  실제로 개발한다면 최신버전의 ubuntu를 활용할 것.

 

#sudo pip3 install django==2.1 // 설치 시 sqlite dependancy로 인해 제대로 설치가 안됨.

 

./manage.py runserver 127.0.0.1:8000 사용 시, 다른 host에서 connection 불가.. 

 

(pytest) leej5@LeeJ5:~/python_project/test1/test/t1$ ./manage.py runserver 192.168.1.3:8000
Performing system checks...

System check identified no issues (0 silenced).
July 19, 2019 - 05:21:25
Django version 2.1, using settings 't1.settings'
Starting development server at http://192.168.1.3:8000/
Quit the server with CONTROL-C.
[19/Jul/2019 05:21:41] "GET / HTTP/1.1" 200 16348
[19/Jul/2019 05:21:41] "GET /static/admin/css/fonts.css HTTP/1.1" 200 423
[19/Jul/2019 05:21:42] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 200 82564
[19/Jul/2019 05:21:42] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 200 80304
[19/Jul/2019 05:21:42] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 200 81348
Not Found: /favicon.ico
[19/Jul/2019 05:21:42] "GET /favicon.ico HTTP/1.1" 404 1970
^C(pytest) leej5@LeeJ5:~/python_project/test1/test/t1$ ./manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
July 19, 2019 - 05:21:52
Django version 2.1, using settings 't1.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
^C(pytest) leej5@LeeJ5:~/python_project/test1/test/t1$ ./manage.py runserver 192.168.1.3:8000
Performing system checks...

System check identified no issues (0 silenced).
July 19, 2019 - 05:22:00
Django version 2.1, using settings 't1.settings'
Starting development server at http://192.168.1.3:8000/
Quit the server with CONTROL-C.
[19/Jul/2019 05:22:13] "GET / HTTP/1.1" 200 16348

 

'python' 카테고리의 다른 글

jupyter note login token 확인 방법  (0) 2019.12.07
try except 문  (0) 2019.11.20
Django install guide (link)  (0) 2019.07.19
python3 version 선택  (0) 2019.07.19
virtual env(venv) install in ubuntu 14  (0) 2019.07.19
Posted by easy16
python2019. 7. 19. 11:31

https://computingforgeeks.com/how-to-install-pip3-django-on-ubuntu-18-04-ubuntu-16-04-lts/

'python' 카테고리의 다른 글

try except 문  (0) 2019.11.20
Django test example  (0) 2019.07.19
python3 version 선택  (0) 2019.07.19
virtual env(venv) install in ubuntu 14  (0) 2019.07.19
외부 명령어의 실행  (0) 2019.07.17
Posted by easy16
python2019. 7. 19. 11:24

apt-get isntall python3.x 이후,

 

아래의 예제 처럼 등록

ex)

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 2

sudo update-alternatives --config python3

 

(pytest) leej5@LeeJ5:~/python_project/test1$ python3 -V
Python 3.6.3

'python' 카테고리의 다른 글

Django test example  (0) 2019.07.19
Django install guide (link)  (0) 2019.07.19
virtual env(venv) install in ubuntu 14  (0) 2019.07.19
외부 명령어의 실행  (0) 2019.07.17
코루틴 link  (0) 2018.08.27
Posted by easy16
python2019. 7. 19. 10:03

 

프로젝트 마다 package 버전을 달리 관리하기 위한 가상환경 설정


$pip3 install virtualenv
$virtualenv venveej5

@LeeJ5:~$ cd pytest/bin
leej5@LeeJ5:~/pytest/bin$ . activate

(pytest) leej5@LeeJ5:~/pytest/bin$

 

~/pytest/bin$ sudo pip3 install django

'python' 카테고리의 다른 글

Django test example  (0) 2019.07.19
Django install guide (link)  (0) 2019.07.19
python3 version 선택  (0) 2019.07.19
외부 명령어의 실행  (0) 2019.07.17
코루틴 link  (0) 2018.08.27
Posted by easy16