Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当找不到元素时如何处理属性错误_Python_Web_Web Scraping_Beautifulsoup - Fatal编程技术网

Python 当找不到元素时如何处理属性错误

Python 当找不到元素时如何处理属性错误,python,web,web-scraping,beautifulsoup,Python,Web,Web Scraping,Beautifulsoup,我正试图搜刮黎巴嫩的一个房地产网站,有些酒店有关于其可用设施的描述,有些则没有 在将要刮取的链接存储在excel列中之后,我对它们进行迭代以访问它们并提取该属性的可用便利设施 但有些属性是土地,例如,那些没有任何便利设施,所以当我的代码遇到这种情况时,它会引发一个“属性错误” 下面的代码是try-and-except方案,但我想要的是在列表中设置一个“no-commercies”值,当没有任何commercies soupObject时,我将在列表中存储我的commercies soupObje

我正试图搜刮黎巴嫩的一个房地产网站,有些酒店有关于其可用设施的描述,有些则没有

在将要刮取的链接存储在excel列中之后,我对它们进行迭代以访问它们并提取该属性的可用便利设施

但有些属性是土地,例如,那些没有任何便利设施,所以当我的代码遇到这种情况时,它会引发一个“属性错误”

下面的代码是try-and-except方案,但我想要的是在列表中设置一个“no-commercies”值,当没有任何commercies soupObject时,我将在列表中存储我的commercies soupObject

任何帮助都将不胜感激

import bs4
import requests


amenity_soupObject=[]
for cellObj in sheet['A']: ##here the A represents the column name
    try:
        link = cellObj.value
        ress = requests.get(link)
        time.sleep(0.2)
        soup_finder = bs4.BeautifulSoup(ress.text) 


        propAmenities=soup_finder.find("div", class_="amenities_container")
        propAmenities1 = bs4.BeautifulSoup(propAmenities.text)
        amenity_soupObject.append(propAmenities1)
        print(propAmenities1)

     except: "AttributeError"
         amenity_soupObject.append("no amenities here")

你的代码怎么了?(删除“AttributeError”周围的冒号和引号)
除了:“AttributeError”
看起来错误。这是真的密码吗?它应该是
除了AttributeError:
欢迎来到Stack Overflow,Camille。查看如何创建。请尝试除AttributeError as error:之外的
<代码>概率
可能是
None
,尝试在调用
find()
后打印它。