[Python #1] Learning Python with Selenium - Log in to steemit [CN]steemCreated with Sketch.

in #zzan5 years ago (edited)

到现在用的编程语言只有java一个,使我不得不再学一个语言,Python。
充满变化的现实不学必淘汰。
幸好还有steemit这样的永久保存平台,想简简单单的留个学习记录吧。(不知道能支持多久。)

以前学过python,但忘得一干二净。用我这个笨脑袋分析了下,失败的原因应该是硬学。
边做项目边学习会不会好点呢? 就选一个项目吧。
听说selenium这个库可以控制网页,支持python,就你了。

开启python和selenium自动化steemit网站项目,走起~

Install

install Python: https://www.python.org/downloads/
install Selenium module: pip install selenium
install IDE pycharm: https://www.jetbrains.com/pycharm/download/#section=windows

Scenario

  1. 访问steemit并把窗口设置为最大值
  2. 在登录界面输入账号
  3. 从文件获取postingkey 并输入
  4. 登录完成

Source

from selenium import webdriver
import time

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(5)
chrome.get(steemit)
# Call function 'login'
login()
chrome.quit()

演示视频

https://youtu.be/5i5qbntTras

本次内容整理

Python:

  1. 函数以def开头,以冒号(:)结束,而且函数内部都需要缩进,这个不同于java等语言的{}。缩进的部分叫suite。
  2. 定义变量时不需要定义类型,python自动识别类型。这么好,一边去 java。
  3. 读文件时以 with open() as xx: 格式读取。 suite要缩进。
  4. 循环以 for x in xx: 格式输入,不要忘记suite要缩进。

Selenium:

  1. find_element_by_xpath() 类函数用于查找网页元素(如:登录元素)
  2. click()类函数可点击元素,sendkeys()类函数则输入文本

Pycharm:

  1. 一次性改掉变量名的快捷键是 shift + F6 。
Sort:  

@tipu curate

来自于 [WhereIn Android] (http://www.wherein.io)

Upvoted 👌

昨天韩文 今天发中文是这意思吗👍😉

来自于 [WhereIn Android] (http://www.wherein.io)

对的~ 谢谢你的tipu
!shop

Posted using Partiko Android

你好鸭,小萍萍!
@june0620赠送1枚SHOP币给你!

目前你总共有: 26枚SHOP币

查看或者交易 SHOP币 请到 steem-engine.com.

无聊吗?跟我猜拳吧! **石头,剪刀,布~**

!shop

来自于 [WhereIn Android] (http://www.wherein.io)

你好鸭,june0620!
@yanhan赠送1枚SHOP币给你!

目前你总共有: 81枚SHOP币

查看或者交易 SHOP币 请到 steem-engine.com.

无聊吗?跟我猜拳吧! **石头,剪刀,布~**

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

대신 영어가 좀 딸립니다 ^^ ㅎㅎㅎ

Posted using Partiko Android

우와~~~ 멋집니다. 중국어 실력이 장난아니시네요.^^

ㅎㅎ 감사합니다^^
영어로도 언젠가 포스팅 날릴 날이 오길 바래봅니다^^

Posted using Partiko Android

Coin Marketplace

STEEM 0.17
TRX 0.14
JST 0.029
BTC 59060.44
ETH 2608.94
USDT 1.00
SBD 2.43