试图制作一个在python3中提取电子邮件的功能

时间:2019-06-18 00:08:42

标签: python python-3.x

我做了这个简单的代码,从网页中提取电子邮件。 我想将其转换为单个功能 遇到问题时,我观看了每个YouTube视频,如何将此代码转换为函数?

   import bs4 as bs
import urllib.request
import re
import sys
sauc = urllib.request.urlopen('https://bluerayheatingandcooling.com/contact.htm').read()
    soup = bs.BeautifulSoup(sauc,'lxml')

vars = soup.get_text()
varr = re.findall('\w+@\w+\.[a-z]{3}',vars)


print(varr)

1 个答案:

答案 0 :(得分:0)

尝试:

import bs4 as bs
import urllib.request
import re
import sys
def extract_mails(url):
  sauc = urllib.request.urlopen(url).read()
  soup = bs.BeautifulSoup(sauc,'lxml')

  vars = soup.get_text()
  varr = re.findall('\w+@\w+\.[a-z]{3}',vars)
  return varr


print(extract_mails('https://bluerayheatingandcooling.com/contact.htm'))