Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python单击按钮进行投票_Python_Python 3.x_Web Scraping - Fatal编程技术网

如何使用python单击按钮进行投票

如何使用python单击按钮进行投票,python,python-3.x,web-scraping,Python,Python 3.x,Web Scraping,我正在用python练习web抓取。我想在一个网站上按下一个按钮来投票。 这是密码 <html> <head></head> <body role="document"> <div id="static page" class="container-fluid"> <div id="page" class="row"></div> <div id="faucets-list"> <tbody&g

我正在用python练习web抓取。我想在一个网站上按下一个按钮来投票。 这是密码

<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>

但由于每次投票都有多个数字,所以它总是显示第一个数字。。。所以它从不打印那个打印。。。如何选择我的html源代码行并将其替换为我想要的行?

您可能需要签出。它是python的一个模块,允许您自动化上面提到的任务。试着这样做:

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\u elements\u by\u css\u selector
方法,而不是
find\u elements\u by\u css\u selector
。前者返回与css选择器匹配的所有元素的列表,而后者仅返回第一个匹配元素。然后我用一个简单的
for
循环遍历了这个列表,并使用了与您相同的检查。我还单击了循环中的元素。希望这有帮助

可能重复的
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()
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()