【发布时间】:2021-01-26 19:34:15
【问题描述】:
我正在尝试从该索引页面抓取所有电子邮件地址 - http://www.uschess.org/assets/msa_joomla/AffiliateSearch/clubresultsnew.php?st=AL
我修改了一个python脚本来定义字符串,用BS4解析内容并将每个唯一地址保存到一个xls文件中:
import requests
from bs4 import BeautifulSoup
import xlwt
wb = xlwt.Workbook()
ws = wb.add_sheet('Emails')
ws.write(0,0,'Emails')
emailList= []
r=0
#add url of the page you want to scrape to urlString
urlString='http://www.uschess.org/assets/msa_joomla/AffiliateSearch/clubresultsnew.php?st=AL'
#function that extracts all emails from a page you provided and stores them in a list
def emailExtractor(urlString):
getH=requests.get(urlString)
h=getH.content
soup=BeautifulSoup(h,'html.parser')
mailtos = soup.select('a[href^=mailto]')
for i in mailtos:
href=i['href']
try:
str1, str2 = href.split(':')
except ValueError:
break
emailList.append(str2)
emailExtractor(urlString)
#adding scraped emails to an excel sheet
for email in emailList:
r=r+1
ws.write(r,0,email)
wb.save('emails.xls')
xls 文件按预期导出,但没有电子邮件值。如果有人能解释为什么或如何简化此解决方案,将不胜感激!
【问题讨论】:
-
请检查我的回答是否满足您的要求
标签: python web-scraping beautifulsoup