如何单击按钮使用python进行投票 [英] How to click a button to vote with python
问题描述
我正在练习使用 Python 进行网页抓取.我想在一个网站上按下一个按钮来对一个项目进行投票.这是代码
I'm practicing with web scraping in python. I'd like to press a button on a site that votes an item. Here is the code
<html>
<head></head>
<body role="document">
<div id="static page" class="container-fluid">
<div id="page" class="row"></div>
<div id="faucets-list">
<tbody>
<tr class=""></tr>
<tr class=""></tr>
<tr class=""></tr>
<tr class=""></tr>
# an infinite number of nodes, until there's mine
<tr class="">
<td class="vote-col">
<div class="vote-box">
<div class="vote-links">
<a class="vote-link up" data-original-title="I like this faucet" href="#" data-faucet"39274" data-vote"up" data-toggle"tooltip" data-placement="top" title=""></a>
这是最后一部分,但是当我手动单击按钮时:
And this it the final part but when I manually click on the button:
<a data-original-title="I like this faucet" href="#" class="vote-link up voted" data-faucet="39274" data-vote="up" data-toggle="tooltip" data-placement="top" title=""></a>
我可以用python中的脚本模拟它吗?我还是个新手,最近开始学习python.P.S:网站是https.而且我不能使用 http 导致它强制在 https 中重定向.
Can i simulate it with a script in python? I'm still a newbie and I've started learning python recently. P.S: the site is in https. And I cant's use http cause it force to redirect in https.
--UDPATE--我正在尝试使用硒..
--UDPATE-- I'm trying with selenium..
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://faucetbox.com/en/list/BTC")
element = driver.find_element_by_css_selector(".vote-link.up")
element_attribute_value = element.get_attribute("data-faucet")
if element_attribute_value == "39274":
print ("Value: {0}".format(element_attribute_value))
driver.quit()
但是由于每个投票有多个数字,它总是显示第一个...所以它永远不会打印该打印...我该如何选择我的html源代码行结束将其替换为我想要的行?
But since there are multiple number for each vote, it always shows the first one... so it never prints that print... How can I do to select my line of html sourcecode end replace it to a line I want?
推荐答案
您可能想查看 Selenium一>.它是 Python 的一个模块,允许您自动执行上面提到的任务.尝试这样的事情:
You might want to check out Selenium. It is a module for python that allows you to automate tasks like the one you have mentioned above. Try something like this:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("https://faucetbox.com/en/list/BTC")
elements = driver.find_elements_by_css_selector(".vote-link.up")
for element in elements:
element_attribute_value = element.get_attribute("data-faucet")
if element_attribute_value == "39274":
print("Value: {0}".format(element_attribute_value))
element.click()
break
driver.quit()
你所做的和我所做的不同在于我使用了find_elements_by_css_selector
方法而不是find_element_by_css_selector
.前者返回与 css 选择器匹配的所有元素的列表,而后者仅返回第一个匹配的元素.然后我只是用一个简单的 for
循环遍历该列表,并使用与您所做的相同的检查.我还单击了循环中的元素.希望这会有所帮助!
The difference between what you did and what I did is that I used the find_elements_by_css_selector
method instead of find_element_by_css_selector
. The former returns a list of all elements that match the css selector while the latter returns only the first matching element. Then I just iterated through that list with a simple for
loop and used the same check that you did. I also clicked the element in the loop. Hope this helps!
这篇关于如何单击按钮使用python进行投票的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!