How to click a button to vote with python

2024/10/13 14:25:51

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 webdriverdriver = 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?

Answer

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 webdriverdriver = 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!

https://en.xdnf.cn/q/118074.html

Related Q&A

Python 2.7 connection to Oracle: loosing (Polish) characters

I connect from Python 2.7 to Oracle data base. When I use:cursor.execute("SELECT column1 FROM table").fetchall()]I have got almost proper values for column1 because all Polish characters (&qu…

getting friendlist from facebook graph-api

I am trying to get users friend list from facebook Graph-api. So after getting access token when I try to open by urlopen byhttps://graph.facebook.com/facebook_id/friends?access_token=authentic_access…

Sorting Angularjs ng-repeat by date

I am relatively new to AngularJS. Could use some helpI have a table with the following info<table><tr><th><span ng-click="sortType = first_name; sortReverse = !sortReverse&quo…

Html missing when using View page source

Im trying to extract all the images from a page. I have used Mechanize Urllib and selenium to extract the Html but the part i want to extract is never there. Also when i view the page source im not abl…

Move file to a folder or make a renamed copy if it exists in the destination folder

I have a piece of code i wrote for school:import ossource = "/home/pi/lab" dest = os.environ["HOME"]for file in os.listdir(source):if file.endswith(".c")shutil.move(file,d…

Segmentation fault after removing debug printing

I have a (for me) very weird segmentation error. At first, I thought it was interference between my 4 cores due to openmp, but removing openmp from the equation is not what I want. It turns out that wh…

numpy get 2d array where last dimension is indexed according to a 2d array

I did read on numpy indexing but I didnt find what I was looking for.I have a 288*384 image, where each pixel can have a labelling in [0,15]. It is stored in a 3d (288,384,16)-shaped numpy array im.Wit…

Error sending html email with mailgun python API

I can send text email with the Mailgun python API fine:def send_simple_message(mailtext, filename=""):requests.post("https://api.mailgun.net/v3/mydomain.in/messages",auth=("api…

How to take HTML user input and query it via Python SQL?

Is there a way to take user input from HTML, and use python to run the input through to a SQL database? Does the input need to be parsed? I want the the user to be able to type in a store name, and f…

Reading and taking specific file contents in a list in python

I have a file containing:name: Sam placing: 2 quote: Ill win.name: Jamie placing: 1 quote: Be the best.and I want to read the file through python and append specific contents into a list. I want my fir…