【问题标题】:Returning body text using BeautifulSoup使用 BeautifulSoup 返回正文
【发布时间】: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


    【解决方案1】:

    你可以尝试Find_all获取所有font标签值,然后迭代。

    from bs4 import BeautifulSoup
    html="""<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>"""
    
    soup = BeautifulSoup(html, "html.parser")
    for span in soup.find_all('font'):
          print(span.text)
    

    输出:

    Hey John,
    
    Here is a test email
    

    【讨论】:

    • 这成功了!输出正是你所说的那样。谢谢!
    【解决方案2】:

    您需要打印字体标签内容。您可以使用select 方法并将其传递给font 元素的类型选择器。

    from bs4 import BeautifulSoup as bs
    
    html = '''
    <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>
    '''
    
    soup = bs(html, 'lxml')
    
    textStuff = [item.text for item in soup.select('font') if item.text != ' ']
    print(textStuff)
    

    【讨论】:

    • 试过了,不得不把bs(html, 'lxml')改成BeautifulSoup(html, 'lxml')。我的新输出是:['Hey John,', '\xa0', 'Here is a test email'] 好多了,但仍然需要做一些小修正
    • 我在 import 语句中将 BeautifulSoup 别名为 bs,这就是它不同的原因。
    • 我没有得到 '\xa0' 值,所以我想可能与您显示的有所不同?这些剩余的修复是什么?目前我似乎得到了预期的结果。 select方法也很快。
    • 您始终可以使用 .replace('\xa0', ' ') 删除 unicode,但我仍然不知道您是如何得到的,因为我的打印效果很好。你的语言设置有什么不同吗?
    猜你喜欢
    • 2013-04-29
    • 2017-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-06
    • 2020-03-12
    • 1970-01-01
    相关资源
    最近更新 更多