0

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&nbsp;like&nbsp;this&nbsp;faucet" href="#" class="vote-link up voted" data-faucet="39274" data-vote="up" data-toggle="tooltip" data-placement="top" title=""></a>

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

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?

1

1 Answer 1

-1

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

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.