我做了这个简单的代码,从网页中提取电子邮件。 我想将其转换为单个功能 遇到问题时,我观看了每个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)
答案 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'))