Search code examples
pythonbeautifulsouppython-requests

python beautifulsoup or javascript problem to find telephone number


I am scraping a site that has links like https://www.europages.co.uk/ARTISAN-4-SEASON-PROJECTS/00000005443350-781559001.html. I can scrape company name , address etc but cant get phone number which is displayed only after clicking a button . Any insight would be helpful. I don't wanna use selenium.

my code

    import requests
    from bs4 import BeautifulSoup

    url="https://www.europages.co.uk/ARTISAN-4-SEASON-PROJECTS/00000005443350-781559001.html"

    res=requests.get(url)
    soup = BeautifulSoup(res.content,'html.parser')
    address  = soup.find('dd').text.strip()
    name= soup.find('h1',class_="ep-epages-header-title text-h6 text-sm-h4").text.strip()
    print(name)
    print(address)



Solution

  • To get a phone number you have to do another Ajax request:

    import re
    
    import requests
    
    
    def get_phone_url(url):
        id_ = re.search(r"(\d+-\d+).html", url).group(1)
        return f"https://www.europages.co.uk/ep-api/v2/epages/{id_}/phones"
    
    
    url = "https://www.europages.co.uk/ARTISAN-4-SEASON-PROJECTS/00000005443350-781559001.html"
    phone_url = get_phone_url(url)
    
    data = requests.get(phone_url).json()
    print(data)
    

    Prints:

    {"phones": [{"category": 14, "items": [{"type": 3, "number": "+90 <REDACTED>"}]}]}