我正在尝试从该索引页中删除所有电子邮件地址-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文件按预期导出,但没有电子邮件值。如果有人能解释为什么或如何简化此解决方案,将不胜感激
您可以使用
pandas
进行此操作。以下是完整的代码:输出:
Excel工作表屏幕截图:
如果希望将链接作为
hyperlinks
输出到excel工作表(单击链接后将重定向到网站),则将emailList.append(href)
更改为emailList.append('=HYPERLINK("'+href+'")')
。 同时,您还应该将文件扩展名更改为.xlsx
。只有这样,你才能得到超链接的链接输出:
希望这有帮助
因为电子邮件是受保护的。我只添加了电子邮件抓取部分。不要添加excel部分,因为你没有问题。将受保护的电子邮件转换为文本贷记将转到https://stackoverflow.com/a/36913154/7518304
相关问题 更多 >
编程相关推荐