[Python #3] Learning Python with Selenium - Dictionary [CN]
dictionary(以下称dict)是由key和value组成的collection。类似于java的Map
因key是固定值,所以一般都是字符转类型,value可容纳python所有的对象。(tuple也可以用于key)
控制steemit网站不太好使用dict。以后学steemit python库的时候再详细学习下dict。
这次就简单学习, 就从帖子目录获取标题,作者,URL,名声,发帖日期,点赞状态,收益保存到dict。
Scenario
- 登录Steemit
- 获取帖子目录并保存为list
- 循环目录并把信息保存到dict输出到控制台
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:
- dict不支持顺序类似于set。两者都用中括号{},所以初始化的时候要注意,特别是在创建空集合的时候不能以a = {} 这样的方式写入,因为python会识别为dict。要创建空集合应以a = set()形式定义。
- 在控制台输出的时候,最好用pprint()module的pprint()函数来改装dict。
Reference:
[Python #2] Learning Python with Selenium - List and set [CN]
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!
@tipu curate
Posted using Partiko Android
Upvoted 👌
谢谢你的tipiu 😀
Posted using Partiko Android
!shop
감사합니다^^
Posted using Partiko Android
你好鸭,june0620!
@ravenkim赠送1枚SHOP币给你!
目前你总共有: 33枚SHOP币
查看或者交易
无聊吗?跟我猜拳吧! **石头,剪刀,布~**SHOP币
请到 steem-engine.com.Scissors
Posted using Partiko Android
It’s a tie! 平局!想不到你也出鸡爪!
石头
It’s a tie! 平局!再来!下回我再出拳头!
剪刀
You lose! 你输了!不给我点赞,你就再吃我一拳~
!shop
@tipu nominate
항상 행복한 💙 오늘 보내셔용~^^
Posted using Partiko Android
감사합니다^^
행복한 하루 보내셔요~
!shop
Posted using Partiko Android
你好鸭,bluengel!
@june0620赠送1枚SHOP币给你!
目前你总共有: 40枚SHOP币
查看或者交易
无聊吗?跟我猜拳吧! **石头,剪刀,布~**SHOP币
请到 steem-engine.com.剪刀
You win!!!! 你赢了!我这新手村猜拳小能手的名号让给你了! 给你1枚SHOP币!
你好鸭,june0620!
@bluengel给您叫了一份外卖!
由 @hertz300 小Q 迎着闪电 开着拖拉机 给您送来
水果月饼
吃饱了吗?跟我猜拳吧! 石头,剪刀,布~
如果您对我的服务满意,请不要吝啬您的点赞~
@onepagex
Rock
Posted using Partiko Android
You lose! 你输了!愿赌服输,请给我点赞~
石头
Posted using Partiko Android
来晚了。!shop
Posted using Partiko Android
tipu 竞争太激烈 😄
!shop
Posted using Partiko Android
你好鸭,亦亦!
@june0620赠送1枚SHOP币给你!
目前你总共有: 103枚SHOP币
查看或者交易
无聊吗?跟我猜拳吧! **石头,剪刀,布~**SHOP币
请到 steem-engine.com.石头
You win!!!! 你赢了! 给你1枚SHOP币!
你好鸭,june0620!
@also.einstein赠送1枚SHOP币给你!
目前你总共有: 34枚SHOP币
查看或者交易
无聊吗?跟我猜拳吧! **石头,剪刀,布~**SHOP币
请到 steem-engine.com.布
It’s a tie! 平局!再来!在猜拳界,我还没有输过!
布
You win!!!! 你赢了!愿赌服输!