Skip to main content
 首页 » 编程设计

python之为什么当我按类别搜索时 BeautifulSoup 的 findAll 返回一个空列表

2024年11月24日10pander-it

我正在尝试使用 h2 标签进行网络抓取,但 BeautifulSoup 返回一个空列表。

<h2 class="iCIMS_InfoMsg iCIMS_InfoField_Job"> 
 
html=urlopen("https://careersus-endologix.icims.com/jobs/2034/associate-supplier-quality-engineer/job") 
bs0bj=BeautifulSoup(html,"lxml") 
nameList=bs0bj.findAll("h2",{"class":"iCIMS_InfoMsg iCIMS_InfoField_Job"}) 
print(nameList) 

请您参考如下方法:

内容在 iframe 中并通过 js 更新(因此不存在于初始请求中)。您可以使用页面用于获取 iframe 内容的相同链接(iframe src)。然后从具有信息的脚本标签中提取字符串并使用 json 加载,提取 description (这是 html)并传回 bs 然后选择 h2 标签。如果需要,您现在还可以将其余信息存储在第二个 soup 对象中。

import requests 
from bs4 import BeautifulSoup as bs 
import json 
 
r = requests.get('https://careersus-endologix.icims.com/jobs/2034/associate-supplier-quality-engineer/job?mobile=false&width=1140&height=500&bga=true&needsRedirect=false&jan1offset=0&jun1offset=60&in_iframe=1') 
soup = bs(r.content, 'lxml') 
script = soup.select_one('[type="application/ld+json"]').text 
data = json.loads(script) 
soup = bs(data['description'], 'lxml') 
headers = [item.text for item in soup.select('h2')] 
print(headers)