首页 > 解决方案 > Webscraping:在循环中用 0 替换 None 值

问题描述

我是构建房屋网络刮板的初学者。我正在构建不同的函数来提取不同的数据(价格、网址、图像、卧室等)

我对卧室有疑问,因为有些房源没有列出卧室。可能是一块土地,或者他们忘记了卧室的数量。当代码遍历列表中的所有卧室时,如果它没有卧室,这是我收到的错误消息:

    Traceback (most recent call last):
  File "get_address.py", line 27, in <module>
    print(get_bedrooms())
  File "get_address.py", line 17, in get_bedrooms
    html_bedrooms = listing.find('h3', {'class': 'listing-results-attr'}).find('span', {'class': 'num-beds'})
AttributeError: 'NoneType' object has no attribute 'find'

这是代码:

from urllib.request import urlopen
from bs4 import BeautifulSoup
import re

html = urlopen('https://www.zoopla.co.uk/for-sale/property/london/')
bs = BeautifulSoup(html, 'html.parser')


    def get_bedrooms():
    
        bedrooms = []
    
        # Finds all the listings_<id> using a regular expression
        for listing in bs.find_all('li', id=re.compile(r'listing.[0-9]+')):
    
            # Finds this code <span class="num-icon num-beds" title="2 bedrooms"><span class="interface"></span>2</span> and stores it in html_bedroom
            html_bedrooms = listing.find('h3', {'class': 'listing-results-attr'}).find('span', {'class': 'num-beds'})
            
            # Extracts the text in the span tags
            no_of_bedrooms = html_bedrooms.get_text(strip=True)
    
            # Updates the empty list with number of bedrooms
            bedrooms.append(no_of_bedrooms)
    
        return bedrooms 
    
    print(get_bedrooms())

如何检查是否有无类型值,然后将其替换为 0?

标签: pythonweb-scrapingbeautifulsoup

解决方案


你可以使用这个:

[0 if x is None else x for x in listing]

将返回另一个列表,其中 None 被替换为 0。


推荐阅读