python - 美丽汤找不到元素时如何处理属性错误
问题描述
我正在尝试抓取黎巴嫩的一个房地产网站,有些房产有其可用设施的描述,有些则没有。
在将要抓取的链接存储在 excel 列中之后,我遍历它们以访问它们并提取该属性的可用设施。
但是有些属性是土地,例如,那些没有任何便利设施,所以当我的代码遇到这种情况时,它会引发“属性错误”。
下面的代码是使用 try and except 方案,但我想要的是在我存储我的设施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")
解决方案
推荐阅读
- javascript - Mobile Hamburger Menu 2 div 关闭/打开菜单
- react-native - fireEvent.press 不工作(反应本机测试库)
- ios - Xamarin iOS 无法从 iCloud 获取文件
- reactjs - 如何在反应中删除从地图创建的特定项目?
- azure-devops - DBT(数据构建工具)- 在 Azure DevOps 中创建 CI/CD 管道
- flutter - Flutter 安全存储测试
- c++ - 第 K 个缺失的正整数
- multithreading - 另一个线程中的 or-tools 给出了奇怪的解决方案 java
- docker - 来自守护程序的错误响应:在节点加入之前达到超时
- python - 一个元素的查找索引在列表中多次出现