使用python和beautifulsoup获取联系信息

妮可·巴拉塔(Nicole C.Baratta)

我正在尝试从页面中获取联系信息。我需要姓名,职务,电话和电子邮件地址。

我正在学习Python,并尝试针对我所知道的数据编写代码。我可以拉出带有各个联系人的div块,但是我不确定一旦有了它们,如何爬过它们。

tags = soup.find_all('div', attrs={'class':'tshowcase-inner-box'})

但是后来我想在儿童div上爬行,没有运气。

    fullname = soup.find('div', attrs={'class':'tshowcase-box-title'})
    title = soup('div', attrs={'class':'tshowcase-single-position'})
    phone = soup('div', attrs={'class':'tshowcase-single-telephone'})
    email = soup('div', attrs={'class':'tshowcase-box-social'})

我不确定接下来要做什么,并感谢任何提示。

这是示例HTML:

<div class="tshowcase-inner-box ts-float-left ">
    <div class="tshowcase-box-info ts-align-left  ">
        <div class="tshowcase-box-title">FULL NAME</div>
        <div class="tshowcase-box-details">
            <div class="tshowcase-single-position"><i class="fa fa-chevron-circle-right"></i>JOB TITLE</div>
            <div class="tshowcase-single-telephone"><i class="fa fa-phone-square"></i><a href="tel:PHONE">PHONE</a></div>
        </div>
        <div class="tshowcase-box-social"><a href="mailto:EMAIL" rel="nofollow" target="_blank"><i class="fa fa-envelope-o fa-lg"></i></a></div>
    </div>
</div>
QHarr

如果您遍历每个列表,则可以测试是否存在并采取相应措施

from bs4 import BeautifulSoup as bs
import requests

html = '''
<div class="tshowcase-inner-box ts-float-left ">
    <div class="tshowcase-box-info ts-align-left  ">
        <div class="tshowcase-box-title">FULL NAME</div>
        <div class="tshowcase-box-details">
            <div class="tshowcase-single-position"><i class="fa fa-chevron-circle-right"></i>JOB TITLE</div>
            <div class="tshowcase-single-telephone"><i class="fa fa-phone-square"></i><a href="tel:PHONE">PHONE</a></div>
        </div>
        <div class="tshowcase-box-social"><a href="mailto:EMAIL" rel="nofollow" target="_blank"><i class="fa fa-envelope-o fa-lg"></i></a></div>
    </div>
</div>
<div class="tshowcase-inner-box ts-float-left ">
    <div class="tshowcase-box-info ts-align-left  ">
        <div class="tshowcase-box-title">FULL NAME2</div>
        <div class="tshowcase-box-details">
            <div class="tshowcase-single-position"><i class="fa fa-chevron-circle-right"></i>JOB TITLE2</div>
            <div class="tshowcase-single-telephone"><i class="fa fa-phone-square"></i><a href="tel:PHONE">PHONE2</a></div>
        </div>
        <div class="tshowcase-box-social"><a href="mailto:EMAIL2" rel="nofollow" target="_blank"><i class="fa fa-envelope-o fa-lg"></i></a></div>
    </div>
</div>
'''
soup = bs(html, 'lxml')
results = []

for listing in soup.select('.tshowcase-inner-box'):
    name = listing.select_one('.tshowcase-box-title')
    job = listing.select_one('.tshowcase-single-position')
    tel = listing.select_one('.tshowcase-single-telephone')
    email = listing.select_one('[href^=mailto]')
    if name is None:
        name = 'Not present'
    else:
        name = name.text
    if job is None:
        job = 'Not present'
    else:
        job = job.text
    if tel is None:
        tel = 'Not present'
    else:
        tel = tel.text
    if email is None:
        email = 'Not present'
    else:
        email = email['href'].replace('mailto:','')
    results.append({ 'name' : name, 'job' : job, 'tel': tel, 'email': email })
print(results)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法使用 Request 和 BeautifulSoup 获取 lxml 站点的信息

如何使用BeautifulSoup和Python获取元素

使用Beautifulsoup 4进行Webscrapping-提取联系信息

使用 Beautifulsoup 4 进行网页抓取 - 提取联系信息

BeautifulSoup 获取其中的链接和信息

使用 beautifulsoup 获取 YouTube 视频的信息

无法使用Telegram api和python获取图像信息

网页抓取 - 从使用 BeautifulSoup 和 Python 的类中获取文本?

使用BeautifulSoup和Python获取元标记内容属性

使用BeautifulSoup和Python从item标签获取地址文本

如何使用beautifulsoup和python在span标签中获取文本

如何使用BeautifulSoup和Python获取属性值?

如何使用python和beautifulsoup获取标题属性?

Python 使用 BeautifulSoup & PIL 从 url 获取图像路径和大小

如何使用Python和Beautifulsoup从脚本标签获取JavaScript变量

使用beautifulsoup和python从html页面获取文本

使用python和beautifulsoup从BBB网站获取数据

尝试使用python和beautifulsoup获取onclick属性的文本

如何使用 python 和 BeautifulSoup 获取标签内的文本

动态生成表格时,如何使用Python BeautifulSoup来获取表格信息?

在python 3.6中使用beautifulsoup4抓取网站以获取产品信息时

使用BeautifulSoup从网页获取链接并滚动以获取更多信息

使用Python的BeautifulSoup获取articleBody

无法获取Api 5.0(棒棒糖和棉花糖)及更高版本上的电话联系信息

我如何使用beautifulsoup从网站的表格中获取多个信息

使用BeautifulSoup从html表获取信息时出错

使用BeautifulSoup获取信息并使其可提取

Xamarin访问联系簿以获取信息

Python BeautifulSoup获取图像,文本和URL