BeautifulSoup爬虫报错:ResultSet无find属性,求问题排查解决
Jooble ICT职位爬取代码报错修复
问题原因
报错核心:soup.find_all()返回的是ResultSet(元素列表),你直接对整个列表调用.find()方法,但.find()是单个BeautifulSoup Tag对象的专属方法,列表没有这个属性。
原代码还有两处语法错误:
requests.Get[jooble]是错误写法,正确的HTTP请求方法是requests.get()attrs={"class_":"FxQpvm yKsady"}里的class_应改为class,在attrs字典中需使用标准HTML属性名
修复方法
- 遍历
job_card列表,对每个单独的职位卡片(Tag对象)提取信息 - 修正请求方法的语法错误
- 增加容错处理,避免因部分元素缺失导致代码崩溃
- 调整选择器属性名,确保匹配页面元素
修复后的完整代码
from bs4 import BeautifulSoup import requests # 修正请求方法语法 html_page = requests.get('https://ng.jooble.org/SearchResult?ukw=ict').text soup = BeautifulSoup(html_page, 'lxml') # 修正attrs中的class属性名 job_cards = soup.find_all('article', attrs={"data-test-name":"_jobCard","class":"FxQpvm yKsady"}, limit=10) # 遍历每个职位卡片 for card in job_cards: # 提取信息时增加容错,防止元素缺失报错 job_title = card.find('h2', attrs={"class":"_15V35X"}).text.strip() if card.find('h2', attrs={"class":"_15V35X"}) else "未找到职位标题" job_requirement = card.find('div', attrs={"class":"_9jGwm1"}).text.strip() if card.find('div', attrs={"class":"_9jGwm1"}) else "未找到职位要求" company = card.find('p', attrs={"class":"Ya0gV9"}).text.strip() if card.find('p', attrs={"class":"Ya0gV9"}) else "未找到公司名称" post_date = card.find('div', class_='caption e0VAhp').text.strip() if card.find('div', class_='caption e0VAhp') else "未找到发布日期" job_link = card.header.h2.a['href'] if card.header and card.header.h2 and card.header.h2.a else "未找到链接" print('-' * 50) print(f'JOB TITLE: {job_title}') print(f'COMPANY NAME: {company}') print(f'JOB REQUIREMENT: {job_requirement[:50]}... (Read More...)') # 截取部分内容避免输出过长 print(f'POSTED: {post_date}') print(f'MORE INFO: {job_link}') print('-' * 50)
补充说明
- 遍历列表后,每个
card都是单个Tag对象,可正常调用.find()方法 - 用
if ... else判断处理元素缺失场景,避免代码中途崩溃 - 调用
.strip()去除文本多余空格,让输出更整洁 - 职位要求截取前50字符,避免内容过长影响可读性
内容的提问来源于stack exchange,提问作者Samuel Oluwapelumi
相关产品推荐
相关产品推荐

