【发布时间】:2021-12-31 20:50:51
【问题描述】:
我有一个 pandas 数据框,其中有一列包含每个电子邮件地址的主机名(超过 1000 行):
email hostname
email@example.com example.com
email2@example.com example.com
email@example2.com example2.com
email@example3.com example3.com
我想检查每个主机名并检查它是否真实存在。
email hostname valid_hostname
email@example.com example.com True
email2@example.com example.com False
email@example2.com example2.com False
email@example3.com example3.com False
首先,我提取了每个电子邮件地址的主机名:
df['hostname'] = df['email'].str.split('@').str[1]
然后,我尝试使用 pyIsEmail 检查 DNS,但这太太慢了:
from pyisemail import is_email
df['valid_hostname'] = df['hostname'].apply(lambda x: is_email(x, check_dns=True))
然后,我尝试了一个多线程函数:
import requests
from requests.exceptions import ConnectionError
def validate_hostname_existence(hostname:str):
try:
response = requests.get(f'http://{hostname}', timeout=0.5)
except ConnectionError:
return False
else:
return True
from concurrent.futures import ThreadPoolExecutor
with ThreadPoolExecutor() as executor:
df['valid_hostname'] = pd.Series(executor.map(validate_hostname_existence, df['hostname']),index=df['hostname'].index)
但这也不太顺利,因为我对并行函数还很陌生。它有多个错误,我相信如果我能以某种方式首先检查此主机名是否已被检查并再次跳过整个请求,它会更有益。我想在不发送电子邮件的情况下尽我所能。
有没有图书馆或方法可以做到这一点?因为到目前为止我还没有找到合适的解决方案。
【问题讨论】:
-
发送一个获取请求验证他们有一个网站而不是他们有电子邮件设置,所以我会放弃这种方法。你可以有一个网站而不是电子邮件,反之亦然。你想做多少验证? 1. 此电子邮件的语法可能有效 2. DNS 此域已启用电子邮件 3. 地址,此特定地址@此域接受邮件。对于 3,你真的只需要发送一封电子邮件。
-
感谢您的澄清请求,@nlta。我想在不发送电子邮件的情况下尽可能快地进行。
标签: python-3.x pandas multithreading dns hostname-verifier