【问题标题】:how to extract data from a span and em tag using beautifulsoup如何使用 beautifulsoup 从 span 和 em 标签中提取数据
【发布时间】:2021-09-27 23:07:57
【问题描述】:

我正在编写从网页中提取数据的代码

# 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>

我没有得到任何结果

【问题讨论】:

  • 这里缺少第二个代码的最后一个 HTML 页面,但您可以在网页上查看

标签: python html web-scraping beautifulsoup


【解决方案1】:

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

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 等邮件连接客户端。

【讨论】:

  • 感谢您的帮助!请告诉我代码中 [find_all(re.compile('p|span'))] 和这个 [i.text] 的用途是什么
猜你喜欢
  • 2020-05-19
  • 1970-01-01
  • 1970-01-01
  • 2021-07-15
  • 1970-01-01
  • 2021-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多