仅从公司名称列表中抓取电子邮件

问题描述 投票:0回答:1

我在 Excel 电子表格上有 10,000 个公司名称的列表,没有 URL,我正在寻找一个提取器 用python编写,提取每个公司名称的电子邮件地址,提取后保存。

我希望看到公司名称列表及其电子邮件 旁边

python email screen-scraping
1个回答
0
投票

自动提取 Excel 电子表格中存储的公司列表的电子邮件地址,这是一个基于 Python 的解决方案,可以提供帮助。此方法包括从 Excel 文件中读取公司名称,使用 Google 搜索每个公司的联系页面,然后抓取该页面以查找电子邮件地址。下面,我将流程分解为多个步骤,并提供了一个完整的脚本来处理该任务。

第 1 步:安装所需的库

首先,您需要安装一些 Python 库,以便于从 Excel 读取、搜索网络以及解析 HTML 内容。您可以使用以下 pip 命令安装这些库:

pip install pandas googlesearch-python beautifulsoup4 openpyxl

第 2 步:用于电子邮件提取的 Python 脚本

这是一个详细的脚本,概述了如何自动化该过程:

import pandas as pd
import re
from googlesearch import search
from bs4 import BeautifulSoup
import requests

# Load the Excel file containing company names
df = pd.read_excel('companies.xlsx')  # Make sure to use your actual file path

# Function to search for emails on a webpage
def find_email(url):
    try:
        response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
        soup = BeautifulSoup(response.text, 'html.parser')
        text = soup.get_text()
        emails = re.findall(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b', text)
        if emails:
            return emails[0]  # Return the first found email address
    except Exception as e:
        return f"Error: {e}"

# Function to find a company's contact page URL via Google search
def get_contact_page(company_name):
    query = f"{company_name} contact email"
    for result in search(query, num_results=1):
        return result
    return "No URL found"

# Append the results to the DataFrame
df['Contact URL'] = df['Company Name'].apply(get_contact_page)
df['Email'] = df['Contact URL'].apply(find_email)

# Save the updated DataFrame to a new Excel file
df.to_excel('updated_companies.xlsx', index=False)

备注: 法律注意事项:确保您有权抓取您正在访问的网站。遵守网站的robots.txt和隐私政策。

速率限制和阻止:自动搜索可能会导致您的 IP 被 Google 阻止。负责任地使用这些脚本,或者考虑使用合法提供您所需数据的 API。 错误处理:该脚本包括基本的错误处理,但您可能需要根据您正在抓取的网站的结构对其进行调整以处理各种边缘情况。

© www.soinside.com 2019 - 2024. All rights reserved.