Python 从 href 链接中抓取电子邮件地址



我想从这些学校获取所有电子邮件地址(绿色链接(: http://www.schulliste.eu/schule/

现在我有获取所有 href 链接的代码,但我如何单击每个链接并从每个单击的链接中废弃电子邮件地址?

from bs4 import BeautifulSoup
from urllib.request import urlopen
import re
import requests
def getLinks(url):
html_page = urlopen(url)
soup = BeautifulSoup(html_page)
links = []
for link in soup.findAll('a', attrs={'href': re.compile("^http://")}):
links.append(link.get('href',))
return links
print(getLinks("http://www.schulliste.eu/schule/"))

您可以找到每所学校的所有链接,然后在每所学校上运行请求:

import requests
from bs4 import BeautifulSoup as soup
def get_emails(_links:list, _r = [0, 10]):
for i in range(*_r):
new_d = soup(requests.get(_links[i]).text, 'html.parser').find_all('a', {'class':'my_modal_open'})
if new_d:
yield new_d[-1]['title']
d = soup(requests.get('http://www.schulliste.eu/schule/').text, 'html.parser')
results = [i['href'] for i in d.find_all('a')][52:-9]
print(list(get_emails(results)))

输出:

['schuleamhasenwald-gue@freenet.de', 'kita-stmartin@htp-tel.de', 'wundertuete@stephansstift.de', 'a.haeupl@igs-baltic-schule.de', 'kindergarten@bothel.de']

你需要有另一个类似于getLinks的函数,例如称为getEmail,你向它传递子页面的URL,它使用urlopen和BeautifulSoup(就像你在第一个函数中所做的那样(来获取HTML内容并从该页面中提取电子邮件地址。

然后,您的主代码需要为从getLinks检索的每个链接调用getEmail

最新更新