You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用BeautifulSoup从slashdot提取monday.com公司所在地失败求助

问题分析与解决方法

问题原因

你使用的div:nth-of-type(4).field-row选择器依赖元素的位置顺序,网页结构可能发生变动,或者该选择器对应的元素包含冗余内容,无法直接提取到目标的“United States”。

解决方法

改用基于语义关联的精准选择器,通过“Location”标签定位对应内容,代码如下:

import requests
from bs4 import BeautifulSoup as bs

url = 'https://slashdot.org/software/p/monday.com/'
profile = requests.get(url)
soup = bs(profile.content, 'lxml')

# 定位包含"Location"的字段行
location_label = soup.find('div', class_='field-row', string=lambda t: t and 'Location' in t)
if location_label:
    # 获取对应的所在地值
    location = location_label.find_next_sibling('div', class_='field-value').text.strip()
    print(location)  # 输出:United States

补充说明

这种方法不依赖元素的排列顺序,而是通过标签的语义关联定位内容,能有效避免页面结构调整导致的选择器失效问题,稳定性更强。

内容的提问来源于stack exchange,提问作者BQuist

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 10:45:37