[Python #3] Learning Python with Selenium - Dictionary [CN]steemCreated with Sketch.

in #cn5 years ago (edited)

dictionary(以下称dict)是由key和value组成的collection。类似于java的Map
因key是固定值,所以一般都是字符转类型,value可容纳python所有的对象。(tuple也可以用于key)

控制steemit网站不太好使用dict。以后学steemit python库的时候再详细学习下dict。
这次就简单学习, 就从帖子目录获取标题,作者,URL,名声,发帖日期,点赞状态,收益保存到dict。

Scenario

  1. 登录Steemit
  2. 获取帖子目录并保存为list
  3. 循环目录并把信息保存到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:

  1. dict不支持顺序类似于set。两者都用中括号{},所以初始化的时候要注意,特别是在创建空集合的时候不能以a = {} 这样的方式写入,因为python会识别为dict。要创建空集合应以a = set()形式定义。
  2. 在控制台输出的时候,最好用pprint()module的pprint()函数来改装dict。

Reference:
[Python #2] Learning Python with Selenium - List and set [CN]

Sort:  

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!

감사합니다^^

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


You lose! 你输了!愿赌服输,请给我点赞~

来晚了。!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!!!! 你赢了!愿赌服输!

Coin Marketplace

STEEM 0.20
TRX 0.14
JST 0.030
BTC 68021.51
ETH 3262.66
USDT 1.00
SBD 2.67