[Python #3] Learning Python with Selenium - Dictionary [KR]
딕셔너리는 고유의 키와 그 값으로 이루어진 쌍 컬렉션이다. java 의 맵과 비슷한 개념인 것 같다.
키는 고유값이므로 보통 문자열로 구성되며 값은 파이썬의 모든 객체가 될 수 있다. (튜플도 키로 사용할 수 있다.)
스팀잇 사이트 컨트롤 하면서 딕셔너리를 활용할 수 있는게 그다지 많지 않다. 나중에 steemit python 라이브러리 공부할 때 좀 더 다뤄보도록 하고,
이번에는 간단하게 글의 타이틀, 저자명, URL, 평판, 등록일, 보팅 상태, 보상 금액 등을 딕셔너리에 저장하도록 한다. 해보자~
Scenario
- Steemit 로그인
- 글 목록을 리스트로 저장
- 반복문으로 글 목록의 정보(타이틀, 저자명, URL 등) 딕셔너리에 저장 후 콘솔창에 출력
Source
from selenium import webdriver
import time
from selenium.common.exceptions import NoSuchElementException
import pprint
def login():
# Click the login button
chrome.find_element_by_xpath('//Header/descendant::a[text()="로그인"]').click()
# Enter username
chrome.find_element_by_xpath('//input[contains(@name,"username")]').send_keys('june0620')
# Get posting key from file
with open('C:/Users/USER/Desktop/python/june0620.txt') as pw:
for postingKey in pw:
# Enter posting key
chrome.find_element_by_xpath('//input[contains(@name,"password")]').send_keys(postingKey)
# Click the login button
chrome.find_element_by_xpath('//button[text()="로그인"]').click()
# Waiting for N seconds
time.sleep(3)
# Set chromedriver
chrome = webdriver.Chrome('C:/Users/USER/Desktop/python/chromedriver/chromedriver.exe')
steemit = 'https://steemit.com'
# Set window maximum
chrome.maximize_window()
# chrome.implicitly_wait(2)
chrome.get(steemit)
# Call function 'login'
login()
chrome.get('https://steemit.com/trending/kr')
posts_list = chrome.find_elements_by_xpath('//ul[contains(@class,"PostsList")]/li')
post_info_list = []
for post in posts_list:
post_info = {
"title": post.find_element_by_xpath('descendant::h2').text,
'url': post.find_element_by_xpath('descendant::h2/a').get_attribute('href'),
'username': post.find_element_by_xpath('descendant::span[@class="author"]/strong').text,
'reputation': post.find_element_by_xpath('descendant::span[@class="Reputation"]').text,
'post_date': post.find_element_by_xpath('descendant::span[@class="updated"]').text,
'upvoting_status': post.find_element_by_xpath('descendant::span[@class="Voting__inner"]/span[1]').get_attribute('class'),
'rewards': post.find_element_by_xpath('descendant::span[@class="FormattedAsset "]').text
}
post_info_list.append(post_info)
pprint.pprint(post_info_list)
chrome.quit()
실행 영상
오늘의 내용 정리
Python:
- 딕셔너리는 집합과 동일하게 순서가 없다. 둘은 모두 중괄호{}를 사용하는데 초기화할 때 주의해야 한다. 특히 빈 집합을 만들 경우 a = {} 이런 식으로 초기화하면 type은 딕셔너리가 된다. 공집합은 a = set() 로 초기화한다.
- 콘솔창에 딕셔너리 값을 예쁘게 출력하려면 pprint 모듈의 pprint()을 사용하면 된다.
Reference:
[Python #2] Learning Python with Selenium - List and set [KR]
我来蹭个分数。@tipu curate !shop
Posted using Partiko Android
谢谢~ 好快啊~
!shop
Posted using Partiko Android
点赞没来…不好意思。
Posted using Partiko Android
没事儿~ 别人给我了 哈哈
应该是命令后面有shop的缘故吧?
Posted using Partiko Android
应该是了,下次时间凑巧了再来。
Hi~ 亦亦!

@june0620 has gifted you 1 SHOP!
Currently you have: 98 SHOP
View or Exchange
Are you bored? Play Rock,Paper,Scissors game with me!SHOPPlease go to steem-engine.com.Hi~ june0620!

@also.einstein has gifted you 1 SHOP!
Currently you have: 22 SHOP
View or Exchange
Are you bored? Play Rock,Paper,Scissors game with me!SHOPPlease go to steem-engine.com.@tipu curate
앗~ 팁유 감사합니다^^
Posted using Partiko Android
Upvoted 👌
Thank you so much for participating in the Partiko Delegation Plan Round 1! We really appreciate your support! As part of the delegation benefits, we just gave you a 3.00% upvote! Together, let’s change the world!
june0620, thanks for your kindness to gift SHOP to 1 steemians, you have been received a 31.25% upvote from me~
@tipu curate