如何使用beautifulsoup从span和em标签提取数据

时间:2018-07-28 17:25:25

标签: python html web-scraping beautifulsoup em

我正在研究从网页提取数据的代码

# first is task.py
import requests
from bs4 import BeautifulSoup

url = ('https://www.naukri.com/job-listings-Python-Developer-Cloud-Analogy-Softech-Pvt-Ltd-Noida-Sector-63-Noida-1-to-2-years-250718003152?src=rcntSrchWithoutCount&sid=15327965116011&xp=1&px=1&qp=python%20developer&srcP 
ge=s')
response = requests.get(url)
page = response.text
soup = BeautifulSoup(page, 'html.parser')
links = soup.find_all("div", {"id":"viewContact"})
for link in links:
    print(link.text)

我想在此页面上检索联系方式。在“查看联系方式”页面的底部 该网页包含:

<div class="jDisc viewContact" id="viewContact" style="display: block;"><p> 
<em>Recruiter Name:</em><span>Malika Pathak, Himani Adhikari</span></p><p> 
<em>Contact Company:</em><span>Cloud Analogy Softech Pvt Ltd</span></p><p> 
<em>Address:</em><span>H-77, H Block, Sector 63, Noida, UP-201307NOIDA,Uttar 
Pradesh,India 201307</span></p><p><em>Email Address:</em><span><img 
title="himani.adhikari@cloudanalogy.com , malika.pathak@cloudanalogy.com" 
src="data:image/jpeg;base64,"></span></p><p><em>Website:</em><a 
target="_blank" 
rel="nofollow" href="http://cloudanalogy.com/">http://cloudanalogy.com/</a> 
</p> 
<p><em>Telephone:</em><span>9319155392</span></p></div>

结果我什么都没得到

1 个答案:

答案 0 :(得分:2)

对于第一个链接,您可以通过recSum div访问信息:

import requests, re
from bs4 import BeautifulSoup
d = soup(requests.get('https://www.naukri.com/job-listings-Python-Developer-Cloud-Analogy-Softech-Pvt-Ltd-Noida-Sector-63-Noida-1-to-2-years-250718003152?src=rcntSrchWithoutCount&sid=15327965116011&xp=1&px=1&qp=python%20developer&srcP%20ge=s').text, 'html.parser')
results = [i.text for i in d.find('div', {'class':'recSum'}).find_all(re.compile('p|span'))]
print(dict(zip(['name', 'title', 'company', 'location', 'followers'], results)))

输出:

{'name': ' Malika Pathak Senior Human Resource Executive Cloud Analogy Softech Pvt Ltd Noida ', 'title': 'Senior Human Resource Executive', 'company': 'Cloud Analogy Softech Pvt Ltd', 'location': 'Noida', 'followers': '11'}

但是,对于第二个链接,您正在尝试访问受密码保护的邮件服务器。为此,您将需要通过requests向您发送帐户凭据,或使用smtplib之类的邮件连接客户端。