AttributeError:“ NoneType”对象没有属性“ a”

时间:2018-12-07 21:12:40

标签: python html web web-scraping

我试图在下图中使用python获取标题content("GIGABYTE")

我用过:

containers= page_soup.findAll("div",{"class":"item-container"})
brand = containers[0].div.div.a.img["title"]

但我根据此行得到此错误:

'NoneType' object has no attribute 'a' 

this picture is an html content of a site i try to use web scraping method

2 个答案:

答案 0 :(得分:0)

您可以将attribute selectors中的css descendant combinationclass selector一起使用。 .item-brand img[title]表示属性为img的{​​{1}}个元素的父类为title

.item-brand

答案 1 :(得分:0)

item-containers中的每个项目都有一个关联的<img>标签,您可以从中提取其title=属性:

from bs4 import BeautifulSoup
import requests

url = 'https://www.newegg.com/Product/ProductList.aspx?Submit=ENE&DEPA=0&Order=BESTMATCH&Description=graphics+card&N=-1&isNodeId=1'
r = requests.get(url)
data = r.text
page_soup = BeautifulSoup(r.text, 'html.parser')
containers = page_soup.find_all("div",{"class":"item-container"})
titles = [str(c.img['title']) for c in containers]

应创建标题列表:

>>> print(titles)
['LITE-ON DVD Burner Black SATA Model iHAS124-14', 'ASUS DRW-24B1ST/BLK/B/AS Black SATA 24X DVD Burner - Bulk - OEM', ... 'GIGABYTE GeForce RTX 2070 GAMING OC WHITE 8G Video Card, GV-N2070GAMINGOC WHITE-8GC']