- 读取Excel文件“filtered_keywords_with_emails.xlsx”,内容示例如下:
姓名 | 单位 | Keywords |
---|
张三 | 上海交通大学 | 张三 上海交通大学 |
李四 | 家里蹲大学 | 李四 家里蹲大学 |
李四 | 家里蹲大学 | 李四 家里蹲大学 |
王五 | 家里蹲大学 | 王五 家里蹲大学 |
赵六 | 西北工业大学 | 赵六 西北工业大学 |
- 根据“Keywords”一列,在谷歌搜索引擎中进行批量检索
- 在检索到的前三个结果中遍历查找,按照常用的电子邮件正则表达式进行匹配。注意! 找到的email不一定是正确的,需要人工进行辨别
- 最后将检索整理好的结果保存在“filtered_keywords_with_emails.xlsx”文件中,它比“filtered_keywords_with_emails.xlsx”多出“Found Emails”一列,内容示例如下:
姓名 | 单位 | Keywords | Found Emails |
---|
张三 | 上海交通大学 | 张三 上海交通大学 | sz@sjtu.edu.cn |
李四 | 家里蹲大学 | 李四 家里蹲大学 | sl@jld.edu.cn |
李四 | 家里蹲大学 | 李四 家里蹲大学 | sl@jld.edu.cn |
王五 | 家里蹲大学 | 王五 家里蹲大学 | ww@jld.edu.cn |
赵六 | 西北工业大学 | 赵六 西北工业大学 | lz@nwpu.edu.cn |
- 找到的email不一定是正确的,在代码运行完之后需要人工进行辨别删减
代码如下:
import time
import re
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import TimeoutException
def close_other_tabs(driver):
while len(driver.window_handles) > 1:
driver.switch_to.window(driver.window_handles[-1])
driver.close()
driver.switch_to.window(driver.window_handles[0])
options = Options()
options.page_load_strategy = 'eager'
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
driver = webdriver.Chrome(options=options)
df = pd.read_excel('filtered_keywords_with_emails.xlsx')
if 'Found Emails' not in df.columns:
df['Found Emails'] = None
email_patterns = [r'[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+',
r'[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+',
r'[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}']
unwanted_emails = {'service@x-mol.com', 'xueshu_support@baidu.com'}
keyword_emails = {}
for index, row in df.iterrows():
keyword = row['Keywords']
if pd.notna(row['Found Emails']):
print(f"Skipping '{keyword}', emails already found.")
continue
if keyword in keyword_emails:
emails = keyword_emails[keyword]
else:
close_other_tabs(driver)
print(f"Searching for '{keyword}'...")
emails = set()
email_found = False
driver.get("http://www.google.com")
time.sleep(2)
search_box = driver.find_element(By.NAME, 'q')
search_box.send_keys(keyword)
search_box.send_keys(Keys.RETURN)
time.sleep(2)
results = driver.find_elements(By.CSS_SELECTOR, 'h3')
for i in range(3):
if i > 0:
results = driver.find_elements(By.CSS_SELECTOR, 'h3')
link = results[i].find_element(By.XPATH, '..')
href = link.get_attribute('href')
driver.execute_script(f"window.open('{href}');")
driver.switch_to.window(driver.window_handles[-1])
time.sleep(2)
try:
page_source = driver.page_source
for email_pattern in email_patterns:
found_emails = re.findall(email_pattern, page_source)
if found_emails:
break
if not found_emails:
page_source = page_source.replace('(at)', '@').replace('(dot)', '.')
page_source = page_source.replace('( at )', '@').replace('( dot )', '.')
for email_pattern in email_patterns:
found_emails = re.findall(email_pattern, page_source)
if found_emails:
break
found_emails = set(found_emails) - unwanted_emails
if found_emails:
email_found = True
emails.update(found_emails)
keyword_emails[keyword] = emails
print("Found emails:")
for email in emails:
print(email)
break
except TimeoutException:
print("Timed out waiting for page to load")
continue
finally:
driver.close()
driver.switch_to.window(driver.window_handles[0])
time.sleep(2)
df.at[index, 'Found Emails'] = '; '.join(emails)
if not email_found:
keyword_emails[keyword] = 'None'
print("No emails found on the first 3 search results.")
time.sleep(5)
df.to_excel('filtered_keywords_with_emails.xlsx', index=False)
driver.quit()