使用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
相关产品推荐
相关产品推荐

