수정 전

 

 

코드 :

1
2
3
4
5
6
7
from selenium import webdriver
driver = webdriver.Chrome(r"C:\Users\JW\Desktop\chromedriver_win32\chromedriver.exe")
driver.get("https://www.hansung.ac.kr/web/www/login")
driver.find_element_by_name('_58_login').send_keys('1433047')
driver.find_element_by_name('_58_password').send_keys('')
driver.find_element_by_class_name('btn_login').click()
 
cs

 

실행결과 : 안됨.

================= RESTART: C:\Users\JW\Desktop\python\1-1.py =================
Traceback (most recent call last):
  File "C:\Users\JW\Desktop\python\1-1.py", line 6, in 
    driver.find_element_by_class_name('btn_login').click()
  File "C:\Users\JW\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 564, in find_element_by_class_name
    return self.find_element(by=By.CLASS_NAME, value=name)
  File "C:\Users\JW\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']
  File "C:\Users\JW\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\JW\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".btn_login"}
  (Session info: chrome=77.0.3865.90)

 

 

수정 후

 

 

 

코드 :

1
2
3
4
5
6
7
8
from selenium import webdriver
driver = webdriver.Chrome(r"C:\Users\JW\Desktop\chromedriver_win32\chromedriver.exe")
driver.get("https://www.hansung.ac.kr/web/www/login")
driver.find_element_by_name('_58_login').send_keys('1433047')
driver.find_element_by_name('_58_password').send_keys('')
driver.find_element_by_xpath("""//*[@id="loginUnited"]/form/input[7]""").click()
 
 
cs

 

실행결과 : 로그인 성공

 

설명 : 

from selenium import webdriver

selenium모듈을 이용할 것이다.

 

driver = webdriver.Chrome(r"C:\Users\JW\Desktop\chromedriver_win32\chromedriver.exe")

크롬의 웹드라이버를 가져와 driver라는 변수에 저장.

 

driver.get("https://www.hansung.ac.kr/web/www/login")

get()함수를 이용하여 매개변수로 받은 url주소를 가져온다.

 

페이지에 들어가 검사를 통해서 id박스에 해당하는 곳을 확인했다. id가 있으니 iname으로 받아온다.

driver.find_element_by_name('_58_login').send_keys('1433047')

find_element_by_name()함수를 이용하여 id가 매개벼수인 곳을 찾고 .send_keys()함수를 이용하여 그 곳에 매개벼수를 입력해주었다.

 

driver.find_element_by_name('_58_password').send_keys('')

비밀버호도 동일하게 코딩

 

driver.find_element_by_xpath("""//*[@id="loginUnited"]/form/input[7]""").click()

수정 전에 실패했던 내용이다. find_element_by_xpath()함수를 이용하여 버튼을 찾고 click()함수를 통해서 클릭을 해준다.

xpath를 받아오는법은 

이렇게 받아오면

//*[@id="loginUnited"]/form/input[7]  이런게 복사가 된다

이 것을 이유는 모르겠지만 ''' ''' (따움표 3개씩)안에 넣어주면 된다.

 

 

 

코드 : 

1
2
3
4
5
6
7
8
9
import bs4
import urllib.request
url = "http://naver.com"
html = urllib.request.urlopen(url)
bsobj = bs4.BeautifulSoup(html, "html.parser")
realtime_hotkeyword = bsobj.find_all("span", {"class":"ah_k"})
for keyword in realtime_hotkeyword:
    print(keyword.text)
 
cs

 

실행결과 :

 

설명 : 

import bs4

import urllib.request

bs4와 request모듈을 사용한다.

 

url = "http://naver.com"

html = urllib.request.urlopen(url)

urllib.request.urlopen()함수를 이용하여 url을 html이라는 변수에 저장했다.

 

bsobj = bs4.BeautifulSoup(html, "html.parser")

bs4.BeautifulSoup(매개변수, "html.parser") 함수를 통하여 파싱을 하고 bsobj라는 변수에 저장했다. 현재에는 해당 페이지의 모든 html문서가 저장되어있는 상황. print(bsobj)로 확인가능.

 

realtime_hotkeyword = bsobj.find_all("span", {"class":"ah_k"})

인기검색어에 해당하는 곳을 찾아보면 span태그안에 class 이름이 정해져있는 것을 알 수 있다.bsobj.find_all()함수를 이용하여 인기검색어들을 realtime_hotkeyword이라는 변수에 저장.

 

for keyword in realtime_hotkeyword:

    print(keyword.text)

for문을 이용하여 모두 출력.

코드:

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import turtle
= turtle.Turtle()
t.speed(50)
t.up()
t.goto(0,-200)
t.down()
t.fillcolor("red")
t.begin_fill()
t.circle(250)
t.end_fill()
= 50;
t.up()
t.goto(0,-100 - x)
t.down()
t.fillcolor("white")
t.begin_fill()
t.circle(200)
t.end_fill()
t.up()
t.goto(0,-50-x)
t.down()
t.fillcolor("red")
t.begin_fill()
t.circle(150)
t.end_fill()
t.up()
t.goto(0,0-x)
t.down()
t.fillcolor("blue")
t.begin_fill()
t.circle(100)
t.end_fill()
t.up()
t.goto(-85,70-x)
t.fillcolor("white")
t.begin_fill()
= 170
t.forward(a)
t.left(144)
t.forward(a)
t.left(144)
t.forward(a)
t.left(144)
t.forward(a)
t.left(144)
t.forward(a)
t.left(144)
t.end_fill()
 
cs

 

실행결과:

 

설명:

= turtle.Turtle()

turtle객체의 메소드인 Turtle()함수를 이용하여 trutle을 생성 후 변수 t에 저장.

 

t.speed(50)

t의 그리기 속도

 

t.up()

up()함수를 이용하여 t를 도화지에서 뗀다(?) : 그려지지 않음.

 

t.goto(0,-200)

해당 좌표로 이동

 

t.down()

down()함수를 이용하여 t를 도화지에 다시 붙인다. : 그려짐

 

t.fillcolor("red")

fillcolor()함수를 이용해서 빨간색으로 칠할 준비

 

t.begin_fill()

begin_fill()함수를 이용해서 색칠 시작

 

t.circle(200)

circle()함수를 이용해서 반지름이 200인 원 그리기

 

t.end_fill()

end_fill()함수를 이용해서 색칠 끝

 

 

 

+ Recent posts