[Python #2] Learning Python with Selenium - List and Set [CN]
今天学习python的list和set
从steemit收集帖子目录后点赞是学习目的。
当然实际上不会点赞,学习list和set才是目的。
从steemit获取帖子目录后转换list并循环这个list,如果list里有whitelist点赞。再一个list里若有nsfw(Not Safe For Work) 帖子就不点赞飘过。
这个场景应该足够我学习list。
那set怎么办呢? 理论上set这个孩子是不允许重复的目录的。
那在 nsfw 帖不点赞的脚本里再写一个脚本,就是把nsfw帖子作者名另存下。(#先保存起来#以后再看#哈哈)作者名不需要重复,正好用set保存作者名。
Scenario
- 访问steemit并登录
- 获取目录,当有white list帖子时点赞
- 发现 nsfw 贴就飘过并另存为set里
Source
from selenium import webdriver
import time
from selenium.common.exceptions import NoSuchElementException
def vote():
pass
def is_nsfw(parent_ele: object) -> bool:
try:
parent_ele.find_element_by_xpath('descendant::span[text()="nsfw"]')
return True
except NoSuchElementException as err:
return False
# 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')
# chrome.get('https://steemit.com/trending/nsfw')
whiteList = ['jaydih']
nsfw_list = set()
posts_list = chrome.find_elements_by_xpath('//ul[contains(@class,"PostsList")]/li')
for post in posts_list:
username = post.find_element_by_xpath('descendant::span[@class="author"]/strong').text
if username in whiteList:
print('white List: ' + username)
vote()
if is_nsfw(post):
nsfw_list.add(username)
continue
print("nsfw list: " + str(nsfw_list))
chrome.quit()
今天学习内容
Python:
List里有一下几种功能
- remove: 从list搜索并删除该项,这样会减少list的大小,如果找不到就会发生错误警告
- pop: 用索引搜索并删除还返回删除的值, 如果不输入索引就删除最后一项,如果找不到索引会抛出错误警告
- append: 往list最后添加值
- extend: 往list装入另一个list
- insert: 也是添加值,但这个函数需要传索引和对象参数。索引是需要添加的位置,对象是需要添加的值
添加值到set时用add()函数,set还有update(), remove(), pop(), clear(), intersection(), union(), difference() 等函数。
Selenium:
如果找不到网页元素Selenium会抛出 NoSuchElementException 警告。
Reference:
[Python #1] Learning Python with Selenium - Log in to steemit [CN]
!thumbup
恭喜你!您的这篇文章入选 @justyy 今日 (2019-10-13) 榜单 【优秀的文章】, 回复本条评论24小时内领赏,点赞本评论将支持 @dailychina 并增加将来您的奖赏。
@justyy 是CN区的见证人,请支持他,给他投票,或者设置justyy为见证人代理。感谢!@justyy的主要贡献:https://steemyy.com
Congratulations! This post has been selected by @justyy as today's (2019-10-13) 【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
수리수리 마하수리"
앗~ 팁유 감사합니다~
Posted using Partiko Android
Upvoted 👌
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
항상 행복한 💙 오늘 보내셔용~^^
Posted using Partiko Android
팁유 요즘 핫하네요^^
행복한 밤 보내세요~
Posted using Partiko Android
그러게욤^^ ㅋ
편안한 밤~💙
Posted using Partiko Android
你好鸭,june0620!
@bluengel给您叫了一份外卖!
由 @morningshine 老K 迎着台风 坐着火箭 给您送来

冰皮月饼
吃饱了吗?跟我猜拳吧! 石头,剪刀,布~
如果您对我的服务满意,请不要吝啬您的点赞~
@onepagex
Rock
Posted using Partiko Android
It’s a tie! 平局!再来!下回我再出拳头!