Is there a way to extract information from a <span> element with restricted access?

I'm currently utilizing bs4 and urllib2 to extract data from a website.

Check out the webpage here.

The goal is to retrieve the remaining digits of the telephone number 3610...... but beforehand, it's necessary to click on a button to reveal the full phone number.

https://i.stack.imgur.com/IvevI.png

This specific information can be found within this designated div:

<div class="telefones">
        Phone(s): <span id="telefones">3610...
        <span><input type="button" id="verTel" value="ver telefone completo"/></span></span>
</div>

Would it be feasible to accomplish this task using bs4 along with urllib2?

Answer №1

To retrieve the phone number, you can fetch it from the JSON response of a request made to the following URL:

http://www.ribeiraosaude.com.br/home/GetTelefone/<id>
. Use the Python library requests to make this request and extract the phone number.

import requests
from bs4 import BeautifulSoup

page_id = 937
with requests.Session() as session:  # maintaining web-scraping session
    response = session.get("http://www.ribeiraosaude.com.br/detalhe/%d" % page_id)
    soup = BeautifulSoup(response.content, "html.parser")

    phone_number = session.get("http://www.ribeiraosaude.com.br/home/GetTelefone/%d" % page_id).json()["telefone"]
    print(phone_number)

Answer №2

To approach this situation, you need to consider how the button behaves when clicked.

If clicking the button triggers JavaScript that reveals a number, you can extract the relevant JavaScript code associated with the button.

For example:

function displayNumber(){ document.body.yourSpan.innerHTML = 'NUMBER'

On the other hand, if clicking the button results in an ajax request, you can simulate the page's actions using Python's built-in request library to communicate directly with the server.

For instance:

phone_number = session.get("http://www.ribeiraosaude.com.br/home/GetTelefone/%d" % page_id).json()["telefone"]
(Credit: alecxe)

Regardless of how the button functions, there is another alternative. Many people also opt for using Selenium, which simulates browser behavior and can be controlled via Python. For guidance on clicking buttons using Selenium, refer to this answer

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

``The background color will dynamically change according to the result of a function

My function named shift_color generates different color codes like "#FF5F74", "#5FFF66", and "#5F8AFF". I am looking to use this output to style a navigation menu background. I have tried the following code: .topnav { background-color: <?php echo shi ...

gem5 is throwing an "ImportError: No module named six" error, despite the fact that the module six has already been successfully

I'm currently facing an issue while trying to execute a basic system in gem5[SHA: 332a9de33db603e0aefedae1e05134db4257ea3e] as per instructions from this Link. The error message displayed is as follows: File "< string>", line 1, in < module> F ...

Animated Gradient Header with CSS

I've been attempting to add an animated gradient to my header class, but so far I haven't been successful. I want the gradient to apply only to the header, not the body. Here's the link I'm using for reference: Can anyone offer some i ...

CSS navigation bar (background gradient problem)

I have encountered an issue with my multi-tier navigation menu where tiers below tier 1 are displaying an unexpected block to the left of the options. I suspect this problem is related to the background gradient applied, but I haven't been able to fin ...

Utilizing regular expressions or BeautifulSoup to locate a specific word or numerical value following a designated keyword

My goal is to extract data from Bloomberg in a structured format to create concise lists. The data structure on Bloomberg's website looks like this: <script type="text/javascript"> var ClientApp = require('app/ClientApp'); var cli ...

Disallow negative numbers but allow decimals in HTML input

I need help restricting user input to prevent negative numbers while still allowing floating point numbers in my Angular project. Any suggestions using Angular tools would be greatly appreciated. Thanks! ...

Is it possible to create a button that can bring in a .css file?

Is there a way to create a button that imports styles from a .css file, or is it possible to change multiple CSS properties with buttons to create different website themes? This is the CSS file I have: .body { background-image: url("example.png"); } ...

Exploring the potential of HTML5 canvas for speedy pixel manipulation

Is there a method to interact with canvas pixels in real-time without using getImageData or createImageData due to their slow performance? ...

Disable the outer div scrolling in VueJS, but re-enable it once the inner div has reached the bottom of

I am currently working on a webpage that contains multiple divs stacked vertically. Here is the concept I am working with: Once the scrollbar reaches the bottom of the first div, the outer scrollbar will be disabled and the inner scrollbar will be enabled ...

What is the best way to choose the next adjacent element using a CSS selector with Python Selenium?

The structure of the DOM is as shown below: <ul> <li> <a href="#" role="button" class="js-pagination link" data-page="1">1</a> </li> <li> <a href="#" role="button" class="js-pagination link active" data ...

Examining data within individual groups in a Python dataframe and making comparisons

I have a dataset that has the following structure - id amount date category code a201 100 12-10-2022 a a201 a101 70 12-10-2022 a a201 a102 90 12-10-2022 a a201 b24 150 12-10-2022 b b24 b13 120 12-10-2022 b b24 c71 10 12-10-2022 c c71 c1 ...

Combining various postponed JavaScript file imports in the HTML header into a single group

I've been facing an issue with my code structure, particularly with the duplication of header script imports in multiple places. Every time I need to add a new script, I find myself manually inserting <script type="text/javascript" src=&q ...

Ways to incorporate vertical alignment while adjusting the size of a <div> block

Seeking advice on how to vertically align <div> blocks (green blocks in my example) when changing block size. What adjustments are needed in my example for the vertical alignment (middle) of side blocks to be maintained when resizing the browser win ...

Is the presence of the selenium webdriver being registered?

Is there a method to detect if a website can tell I am using a bot while running Selenium WebDriver in Python or Puppeteer in JavaScript? Are there any resources that indicate whether a bot test would be failed, such as Cloudflare or Captcha? Appreciate a ...

Tips for choosing one row at a time using Python and SQLite?

Currently, I am facing an issue with the enrollment system I am developing using SQLite3 in Python. The problem arises when I attempt to search for a specific student based solely on their first name. For instance, if my "students" table includes both Gabr ...

Requesting HTML content from a PHP file using JQuery Ajax callback

For an ajax request, I am entering a name in an input box to search for data from a database. Everything is functioning properly. <script> $(document).ready(function(){ $('input#name-submit').on('click', function() { ...

Leveraging the Power of Ajax Button for Django Model Filtering

My goal is to create buttons on my website that, once clicked, trigger a specific filter on my database of models. Specifically, I am trying to sort the "Clothes_Item" model and apply various filters. To start off, I want to keep it simple and create a but ...

Ensure that a distinct cross button is included within the text or search input and positioned to float alongside the text for

Is there a simple method to include a clear button that hovers after the text in an input field when using a search type input? <input type="search"> ...

Methods for deleting all instances of a substring within a collection of lists

I have a collection of strings in Python obtained from reading a .DAT file, structured like this: datContent = [['\x00\x00\x00\x00\x00\x00NGDUID\x00\x00\x00\x00\x00C\SAMPLEx00\x00\x ...

When the click event is triggered, the second function guess() does not execute

I am currently developing a number guessing application. It consists of two functions, the first one called startGame() works correctly (it receives the maximum number and then disappears by adding the hidden class). However, the second function that is ...