[Python #4] Learning Python with Selenium - Function [CN]
今天研究函数。反复使用的代码绑在一个函数,需要的时候呼叫即可。
这样容易管理代码, 可读性更佳。
python的函数类似于java的函数,
比起java写法更简单,自由度也高于java。
最大的区别就是不用必须定义type。
下面比较下java和python的函数,传 list 参数并返回object对象。
java:
public Object a(List<Object> name){
... ...
return object
}
python:
def a(name):
... ...
return object
用 python 这么简单就可以写一个函数。
python的自由度高于java。
如果java是iphone那python就是安卓。
但是官方好像不太推荐此类写法, 函数调用虽然没有问题,却会降低可读性。
再说,你不调用这个函数前无法知道参数和返回值的类型是什么。
幸好python提供叫做annotation的功能来修饰函数。
修饰的方法也非常简单,在函数第一行加入几个字符串即可。
参数后面加:type,括号后面加 ->type来修饰函数。如果没有返回值就输入->None
如下
def a(name: list) -> object:
... ...
return object
Source
from selenium import webdriver
import time
from selenium.common.exceptions import NoSuchElementException
import pprint
def login():
"""
log in to steemit
"""
# 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)
def vote():
pass
def is_nsfw(post: webdriver) -> bool:
"""
Check if it is nsfw.
:param post: Post element
"""
try:
post.find_element_by_xpath('descendant::span[text()="nsfw"]')
return True
except NoSuchElementException as err:
return False
def posts_info_list(posts: webdriver) -> list:
"""
Return the information in the posts.
"""
info_list = []
for post in posts:
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
}
info_list.append(post_info)
return info_list
内容整理
Python:
- 函数由def 开头,由 :结束。函数内部必须缩进。
- 可以简单写 python 函数,但最好用annotation和docstring来修饰函数。在函数内部第一行用''' ''' 来添加docstring (图片绿色部分)
- 空函数用pass关键词来创建,但只需临时使用这样的写法,因为函数不能为空(图片的 vote 函数)
pycharm:
- 浏览函数的annotation和docstring 快捷键是 ctrl + Q 。
Reference:
[Python #3] Learning Python with Selenium - Dictionary [KR]
#zzan #jjm #sct #sct-cn #sct-freeboard #palnet #liv #lifestyle #dblog #cn-stem #steemstem #partiko #python #coding
!thumbup
恭喜你!您的这篇文章入选 @justyy 今日 (2019-10-19) 榜单 【优秀的文章】, 回复本条评论24小时内领赏,点赞本评论将支持 @dailychina 并增加将来您的奖赏。
@justyy 是CN区的见证人,请支持他,给他投票,或者设置justyy为见证人代理。感谢!@justyy的主要贡献:https://steemyy.com
Congratulations! This post has been selected by @justyy as today's (2019-10-19) 【Good Posts】, Steem On! Reply to this message in 24 hours to get rewards. Upvote this comment to support the @dailychina and increase your future rewards! ^_^
SteemIt 工具、API接口、机器人和教程
SteemIt Tools, Bots, APIs and Tutorial
If you believe what I am doing, please consider a spare vote voting me here, thank you very much indeed.
@justyy - the author of https://SteemYY.com and I have been a Steem Witness for more than a year now.
谢谢
Posted using Partiko Android
@tipu curate 2
Posted using Partiko Android
Upvoted 👌 (Mana: 6/18)
谢谢 😄
觉得你的tipu等级很高
Posted using Partiko Android
3级而已😉
你懂好多啊 研究函数是数理博士啊
可以加这两个标签cn-stem 及 steemstem
可能会得到 @softmetal或 @robertyan的 cn-stem 审查点赞
Posted using Partiko Android
是吗,谢谢分享 😀
Posted using Partiko Android
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!
!shop
你好鸭,june0620!
![](https://steemitimages.com/640x0/https://cdn.steemitimages.com/DQmadPZSURDoChgXH9xMdFHEHApQBacDnDK1oUJy5MwVgGF/shop.jpg)
@ravenkim赠送1枚SHOP币给你!
目前你总共有: 66枚SHOP币
查看或者交易
无聊吗?跟我猜拳吧! **石头,剪刀,布~**SHOP币
请到 steem-engine.com.Congratulations @june0620! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :
You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word
STOP
Vote for @Steemitboard as a witness to get one more award and increased upvotes!