[Python] Web scrap data from url [closed]

Discussão em 'Python' iniciado por Stack, Janeiro 18, 2025.

  1. Stack

    Stack Membro Participativo

    I have a CSV file containing a list of companies, with columns for "Company Name" and "Company URL." I want to extract the contact phone numbers of these companies by using their URLs. Can someone please provide a solution for how to this?

    import pandas as pd
    import requests
    from bs4 import BeautifulSoup
    # Provide the absolute path
    file_path = r'D:\VS CODE\scrap_tool\Company_list-1.csv'
    df = pd.read_csv(file_path)

    print(df.head(5))
    print(df.columns)


    headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
    }

    def get_contact_number(url):
    try:
    # Fetch the website content
    headers = {'User-Agent': 'Mozilla/5.0'}
    response = requests.get(url, headers=headers, timeout=10)
    soup = BeautifulSoup(response.text, 'html.parser')

    # Search for phone numbers using regex patterns
    phone_numbers = set()
    for text in soup.stripped_strings:
    if '+91' in text or text.isdigit():
    if len(text) >= 10 and len(text) <= 15: # Typical phone number length
    phone_numbers.add(text)

    return ', '.join(phone_numbers) if phone_numbers else 'Not Found'
    except Exception as e:
    return f"Error:Not found"

    df['Company Phone']=df['Website'].apply(get_contact_number)

    df.to_csv('Company_phone.csv')

    print('Successfully saved the data to Company_phone.csv')


    I tried this code but not get results

    Continue reading...

Compartilhe esta Página