python - 我如何解决''NoneType'对象不可下标'
问题描述
我正在编写一个用于抓取的小脚本,但遇到了错误“TypeError:'NoneType' 对象不可下标。
我以前从未见过这个错误,所以我不知道它是什么意思。
import bs4
import requests
myUrl = "https://www.houzz.com/professionals/searchDirectory? topicId=11785&query=Interior+Designers+%26+Decorators&location=Texas&distance=0&sort=4"
data=requests.get(myUrl)
soup=bs4.BeautifulSoup(data.text,'html.parser')
listing = soup.find_all('div', class_="hz-pro-search-result__profile- desc")
for li in listing:
myurl = li
res = myurl.a['href']
print(res)
错误:
File "C:\Users\Hp\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/Hp/.spyder-py3/houzz.py", line 20, in <module>
res = myurl.a['href']
TypeError: 'NoneType' object is not subscriptable
解决方案
我的 URL 可能正在返回None
,因此您无能为力。使用这样的类检查此 div 中的 li 对象的 HTML 代码。我认为 y 不存在
推荐阅读
- alexa - 没有 Alexa NLU 的 Alexa
- laravel - Laravel 护照创建令牌在 plesk 服务器上不起作用
- rest - 如何使用 apache-poi 从放心的 json 响应中更新 excel 值
- mysql - SQL 查询以获取该用户的最新记录
- maven - 下载 mvn install 上的所有传递依赖项
- svg - 使用单个 SVG 路径绘制平行曲线线 svg?
- java - Java - 用微秒获取 unix 纪元
- javascript - Select2 在输入时取消选择
- python-3.x - 如何使用 Python 将二叉树转换为 Newick 树?
- arrays - 如何从 Node 中的外部函数返回数组