如何抓取无唯一类标识<span>标签中的指定文本?
解决方法:通过标签的唯一类精准提取目标数据
Hey there! Since you're new to web scraping, let's break this down simply. The key here is that even though the target <span> tags don't have unique classes, the <i> tags right before them do have distinct class names (like house, bed, locationpin) that we can use to pinpoint exactly the data we want.
具体实现步骤
Instead of grabbing all text from the extra div, we'll target each piece of data using its associated <i> tag's class:
- First, initialize empty lists to store your data:
property_types = [] bedroom_counts = [] locations = []
- Modify your loop to extract each field individually:
results = requests.get(url) soup = BeautifulSoup(results.text, "html.parser") desc_div = soup.find_all('div', attrs={"data-itemid": True}) for property in desc_div: extra = property.find('div', class_='extra') if not extra: continue # Skip if the extra div doesn't exist # Extract property type (Apartment) using the 'house' icon class type_icon = extra.find('i', class_='house') if type_icon: property_type = type_icon.parent.parent.find_next_sibling('span').text.strip() property_types.append(property_type) else: property_types.append(None) # Handle missing data # Extract bedroom count (2) using the 'bed' icon class bed_icon = extra.find('i', class_='bed') if bed_icon: bedroom_count = bed_icon.parent.parent.find_next_sibling('span').text.strip() bedroom_counts.append(bedroom_count) else: bedroom_counts.append(None) # Extract location (Birmingham) using the 'locationpin' icon class location_icon = extra.find('i', class_='locationpin') if location_icon: location = location_icon.parent.parent.find_next_sibling('span').text.strip() locations.append(location) else: locations.append(None)
代码解释
- We use
extra.find('i', class_='house')to find the icon linked to the property type. - From that icon, we traverse up to its parent
<label>and then the parent<span class="item">, then usefind_next_sibling('span')to grab the adjacent span holding the actual text we need. - The
ifchecks make sure your code won't crash if a field is missing from some listings.
After running this, you'll have three separate lists with exactly the data you need—no extra "For Sale" text included!
内容的提问来源于stack exchange,提问作者python_newbie
相关产品推荐
相关产品推荐

