dot.Py dot.Py - 5 months ago 9
HTML Question

How can I scrape data from a <span> locked tag?

I'm using

bs4
and
urllib2
to fetch some info from a website.

Here's the webpage.

I must fetch the rest of the telephone
3610...
... but first I must press this button to show the rest of the telephone.

img example

This information is located inside this
div
:

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


Is it possible to achieve this by using
bs4
with
urllib2
?

Answer

The phone number is loaded from the response to the http://www.ribeiraosaude.com.br/home/GetTelefone/<id> url, make this request with requests and extract the phone number from the JSON response:

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)