create account

[Python #1] Learning Python with Selenium - Log in to steemit [CN] by june0620

View this thread on: hive.blogpeakd.comecency.com
· @june0620 · (edited)
$12.47
[Python #1] Learning Python with Selenium - Log in to steemit [CN]
到现在用的编程语言只有java一个,使我不得不再学一个语言,Python。
充满变化的现实不学必淘汰。
幸好还有steemit这样的永久保存平台,想简简单单的留个学习记录吧。(不知道能支持多久。)

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

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

### Install

install Python: [https://www.python.org/downloads/](https://www.python.org/downloads/)
install Selenium module: pip install selenium
install IDE pycharm: [https://www.jetbrains.com/pycharm/download/#section=windows](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](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 。
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 20 others
👎  
properties (23)
authorjune0620
permlink1-learning-python-with-selenium-log-in-to-steemit-cn
categoryzzan
json_metadata{"tags":["zzan","cn","jjm","sct","sct-cn","sct-freeboard","palnet","liv","lifestyle","dblog","partiko"],"links":["https://www.python.org/downloads/","https://www.jetbrains.com/pycharm/download/#section=windows","https://youtu.be/5i5qbntTras"],"app":"partiko","format":"markdown","canonical_url":"https://www.steemcoinpan.com/@june0620/1-learning-python-with-selenium-log-in-to-steemit-cn","image":["https://img.youtube.com/vi/5i5qbntTras/0.jpg"]}
created2019-10-10 08:39:48
last_update2019-10-11 01:25:09
depth0
children12
last_payout2019-10-17 08:39:48
cashout_time1969-12-31 23:59:59
total_payout_value6.274 HBD
curator_payout_value6.198 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,041
author_reputation118,592,211,436,406
root_title"[Python #1] Learning Python with Selenium - Log in to steemit [CN]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd0
post_id91,408,425
net_rshares38,077,222,454,215
author_curate_reward""
vote details (85)
@gasigogi ·
우와~~~ 멋집니다. 중국어 실력이 장난아니시네요.^^
properties (22)
authorgasigogi
permlinkre-june0620-1-learning-python-with-selenium-log-in-to-steemit-cn-20191011t031019930z
categoryzzan
json_metadata{"community":"busy","app":"busy/2.5.6","format":"markdown","tags":["zzan"],"users":[],"links":[],"image":[]}
created2019-10-11 03:10:21
last_update2019-10-11 03:10:21
depth1
children1
last_payout2019-10-18 03:10:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length30
author_reputation39,703,543,454,713
root_title"[Python #1] Learning Python with Selenium - Log in to steemit [CN]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id91,434,687
net_rshares0
@june0620 ·
ㅎㅎ 감사합니다^^
영어로도 언젠가 포스팅 날릴 날이 오길 바래봅니다^^

Posted using [Partiko Android](https://partiko.app/referral/june0620)
properties (22)
authorjune0620
permlinkjune0620-re-gasigogi-re-june0620-1-learning-python-with-selenium-log-in-to-steemit-cn-20191011t035827232z
categoryzzan
json_metadata{"app":"partiko","client":"android"}
created2019-10-11 03:58:27
last_update2019-10-11 03:58:27
depth2
children0
last_payout2019-10-18 03:58:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length111
author_reputation118,592,211,436,406
root_title"[Python #1] Learning Python with Selenium - Log in to steemit [CN]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id91,435,579
net_rshares0
@lucky2015 ·
중국 관련된 일 하시나봅니다.
엄청난 중국어 실력.
이게 진짜 부러워할 능력이죠!

Posted using [Partiko Android](https://partiko.app/referral/lucky2015)
properties (22)
authorlucky2015
permlinklucky2015-re-june0620-1-learning-python-with-selenium-log-in-to-steemit-cn-20191010t105312069z
categoryzzan
json_metadata{"app":"partiko","client":"android"}
created2019-10-10 10:53:12
last_update2019-10-10 10:53:12
depth1
children1
last_payout2019-10-17 10:53:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length117
author_reputation200,705,691,700,765
root_title"[Python #1] Learning Python with Selenium - Log in to steemit [CN]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id91,411,635
net_rshares0
@june0620 ·
대신 영어가 좀 딸립니다 ^^ ㅎㅎㅎ

Posted using [Partiko Android](https://partiko.app/referral/june0620)
properties (22)
authorjune0620
permlinkjune0620-re-lucky2015-lucky2015-re-june0620-1-learning-python-with-selenium-log-in-to-steemit-cn-20191010t163854015z
categoryzzan
json_metadata{"app":"partiko","client":"android"}
created2019-10-10 16:38:54
last_update2019-10-10 16:38:54
depth2
children0
last_payout2019-10-17 16:38:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length91
author_reputation118,592,211,436,406
root_title"[Python #1] Learning Python with Selenium - Log in to steemit [CN]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id91,420,685
net_rshares0
@partiko ·
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!
properties (22)
authorpartiko
permlinkre-1-learning-python-with-selenium-log-in-to-steemit-cn-20191010t093021
categoryzzan
json_metadata"{"app": "partiko"}"
created2019-10-10 09:30:24
last_update2019-10-10 09:30:24
depth1
children0
last_payout2019-10-17 09:30:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length213
author_reputation39,207,160,334,751
root_title"[Python #1] Learning Python with Selenium - Log in to steemit [CN]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id91,409,635
net_rshares0
@teamcn-shop ·
你好鸭,june0620!
@yanhan赠送1枚SHOP币给你!
![](https://cdn.steemitimages.com/DQmadPZSURDoChgXH9xMdFHEHApQBacDnDK1oUJy5MwVgGF/shop.jpg)

目前你总共有: 81枚SHOP币
<p><sup>查看或者交易 <code>SHOP币</code> 请到 <a href="https://steem-engine.com/?p=market&t=SHOP">steem-engine.com</a>.</sup></p>
无聊吗?跟我猜拳吧! **石头,剪刀,布~**
properties (22)
authorteamcn-shop
permlinkwherein-1570698003473
categoryzzan
json_metadata"{"app":"teamcn-shop bot/1.0"}"
created2019-10-10 09:00:15
last_update2019-10-10 09:00:15
depth1
children0
last_payout2019-10-17 09:00:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length288
author_reputation11,393,746,055,281
root_title"[Python #1] Learning Python with Selenium - Log in to steemit [CN]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id91,408,922
net_rshares0
@yanhan ·
@tipu curate

来自于 [WhereIn Android] (http://www.wherein.io)
properties (22)
authoryanhan
permlinkwherein-1570697930862
categoryzzan
json_metadata""
created2019-10-10 08:58:57
last_update2019-10-10 08:58:57
depth1
children1
last_payout2019-10-17 08:58:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length59
author_reputation323,731,670,852,703
root_title"[Python #1] Learning Python with Selenium - Log in to steemit [CN]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id91,408,894
net_rshares0
@tipu ·
Upvoted &#128076;
properties (22)
authortipu
permlinkre-wherein-1570697930862-20191010t085927
categoryzzan
json_metadata""
created2019-10-10 08:59:27
last_update2019-10-10 08:59:27
depth2
children0
last_payout2019-10-17 08:59:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length17
author_reputation55,941,821,114,916
root_title"[Python #1] Learning Python with Selenium - Log in to steemit [CN]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id91,408,904
net_rshares0
@yanhan ·
昨天韩文 今天发中文是这意思吗👍😉

来自于 [WhereIn Android] (http://www.wherein.io)
properties (22)
authoryanhan
permlinkwherein-1570697990723
categoryzzan
json_metadata""
created2019-10-10 08:59:54
last_update2019-10-10 08:59:54
depth1
children2
last_payout2019-10-17 08:59:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length64
author_reputation323,731,670,852,703
root_title"[Python #1] Learning Python with Selenium - Log in to steemit [CN]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id91,408,916
net_rshares0
@june0620 ·
对的~ 谢谢你的tipu
!shop

Posted using [Partiko Android](https://partiko.app/referral/june0620)
properties (22)
authorjune0620
permlinkjune0620-re-yanhan-wherein-1570697990723-20191010t090157349z
categoryzzan
json_metadata{"app":"partiko","client":"android"}
created2019-10-10 09:01:57
last_update2019-10-10 09:01:57
depth2
children0
last_payout2019-10-17 09:01:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length89
author_reputation118,592,211,436,406
root_title"[Python #1] Learning Python with Selenium - Log in to steemit [CN]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id91,408,975
net_rshares0
@teamcn-shop ·
你好鸭,小萍萍!
@june0620赠送1枚SHOP币给你!
![](https://cdn.steemitimages.com/DQmadPZSURDoChgXH9xMdFHEHApQBacDnDK1oUJy5MwVgGF/shop.jpg)

目前你总共有: 26枚SHOP币
<p><sup>查看或者交易 <code>SHOP币</code> 请到 <a href="https://steem-engine.com/?p=market&t=SHOP">steem-engine.com</a>.</sup></p>
无聊吗?跟我猜拳吧! **石头,剪刀,布~**
properties (22)
authorteamcn-shop
permlinkjune0620-re-yanhan-wherein-1570697990723-20191010t090157349z
categoryzzan
json_metadata"{"app":"teamcn-shop bot/1.0"}"
created2019-10-10 09:02:06
last_update2019-10-10 09:02:06
depth2
children0
last_payout2019-10-17 09:02:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length285
author_reputation11,393,746,055,281
root_title"[Python #1] Learning Python with Selenium - Log in to steemit [CN]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id91,408,981
net_rshares0
@yanhan ·
!shop

来自于 [WhereIn Android] (http://www.wherein.io)
properties (22)
authoryanhan
permlinkwherein-1570698003473
categoryzzan
json_metadata""
created2019-10-10 09:00:06
last_update2019-10-10 09:00:06
depth1
children0
last_payout2019-10-17 09:00:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length52
author_reputation323,731,670,852,703
root_title"[Python #1] Learning Python with Selenium - Log in to steemit [CN]"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id91,408,920
net_rshares0