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

标签 python web-scraping beautifulsoup

我正在尝试使用 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)

enter image description here

关于python - 为什么当我按类别搜索时 BeautifulSoup 的 findAll 返回一个空列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55858625/

相关文章:

python - 如何在另一个笔记本中运行 Jupyter 笔记本

python - 如何在 python 中将一个列表中的项目与另一个列表进行匹配?

python - 获取 Selenium Python 中 Html 按钮生成的内容

python - 如何使用 Python 从列表中的项目中删除 <br> 和 </br> 标签?

python - 如何在循环中跳过某个值而不删除Python中的值

Windows 上的 Python3 和 GTK3

python - 在 python 中使用 swagger codegen 客户端向 api 调用添加 header 的具体细节尚不清楚

c# - HttpClient - 返回的内容与浏览器不同

python - 如何使用 beautifulsoup 提取字体标签中的文本

Python Web Scraping 使用验证码登录