python - AttributeError:“NoneType”对象没有属性“a”
问题描述
我试图content("GIGABYTE")
在下图中使用 python 获取标题。
我用了:
containers= page_soup.findAll("div",{"class":"item-container"})
brand = containers[0].div.div.a.img["title"]
但我根据这一行得到这个错误:
'NoneType' object has no attribute 'a'
解决方案
您可以将 css属性选择器与类选择器结合使用。 表示具有父类的属性的元素。.item-brand img[title]
img
title
.item-brand
import requests
from bs4 import BeautifulSoup
url = 'https://www.newegg.com/Product/ProductList.aspx?Submit=ENE&DEPA=0&Order=BESTMATCH&Description=graphics+card&N=-1&isNodeId=1'
res = requests.get(url, headers = {'User-Agent': 'Mozilla/5.0'})
soup = BeautifulSoup(res.content, 'lxml')
items = [item['title'] for item in soup.select('.item-brand img[title]')]
print(items)
推荐阅读
- typescript - 关于'Hybrid Types',为什么使用':interface name'会报错
- amazon-elb - 在 Kong 中更改日志记录格式
- arkit - 如何在 Swift 4.2 中使用 ARFaceTracking 获得面部网格中的点,如眼睛、眉毛、嘴唇、嘴巴、鼻子?
- scala - 具有多态类型的有限状态机?
- google-apps-script - 如何构建指向 Google 日历活动的链接?
- c# - Autofac - 注册交易
- javascript - 是否可以将 JavaScript 字符串设置为对象点表示法的访问器属性?
- python - 如何在卫星图像中使用高斯核来改变图像的分辨率
- jquery - 导出到 Excel 时将 jQGrid 数字转换为文本
- python - 使用远程服务器开发和调试 Python