【发布时间】:2019-03-01 18:21:00
【问题描述】:
我正在尝试使用 BeautifulSoup 从使用 ExchangeLib 返回的内容中刮取 HTML 标记。我到目前为止是这样的:
from exchangelib import Credentials, Account
import urllib3
from bs4 import BeautifulSoup
credentials = Credentials('myemail@notreal.com', 'topSecret')
account = Account('myemail@notreal.com', credentials=credentials, autodiscover=True)
for item in account.inbox.all().order_by('-datetime_received')[:1]:
soup = BeautifulSoup(item.unique_body, 'html.parser')
print(soup)
按原样,这将使用 exchangeLib 通过 Exchange 从我的收件箱中获取第一封电子邮件,并专门打印包含电子邮件正文的 unique_body。这是print(soup) 的输出示例:
<html><body><div>
<div><span lang="en-US">
<div style="margin:0;"><font face="Calibri,sans-serif" size="2"><span style="font-size:11pt;">Hey John,</span></font></div>
<div style="margin:0;"><font face="Calibri,sans-serif" size="2"><span style="font-size:11pt;"> </span></font></div>
<div style="margin:0;"><font face="Calibri,sans-serif" size="2"><span style="font-size:11pt;">Here is a test email</span></font></div>
</span></div>
</div>
</body></html>
我的最终目标是打印出来:
Hey John,
Here is a test email
根据我在 BeautifulSoup 文档中阅读的内容,抓取过程介于我的“Soup ="”行和最后的print 行之间。
我的问题是,为了运行 BeautifulSoup 的抓取部分,它需要一个 class 和 h1 标签,例如:name_box = soup.find(‘h1’, attrs={‘class’: ‘name’}),但是就我目前拥有的而言,我没有这些。
作为一个 Python 新手,我该怎么做呢?
【问题讨论】:
标签: python email web-scraping beautifulsoup