Python 出现 "self.attrs[key]]" 错误
Python having the "self.attrs[key]]" error
我开始在 python 上学习一些复杂的代码,今天我决定使用 BeautifulSoup。当我试图获取产品的标题时出现问题,我尝试将“.find”更改为“.findAll”但找不到解决方案。有人请帮助我。
这是我的代码:
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as Soup
ListaSteam = "https://store.steampowered.com/search/?sort_by=Price_ASC&category1=998%2C996&category2=29"
#PAGINA - OBTENCION - CERRADA
Pagina = uReq(ListaSteam)
PaginaHtml = Pagina.read()
Pagina.close()
#1 PASO
PaginaSoup = Soup(PaginaHtml, "html.parser")
CodigoJuegos = PaginaSoup.find("div",{"id":"search_resultsRows"})
PRUEBA = CodigoJuegos.a.span["title"]
print(PRUEBA)
错误如下:
This is the error:
`Traceback (most recent call last):
File "C:\Users\Usuario\Desktop\******", line 14, in <module>
PRUEBA = CodigoJuegos.a.span["title"]
File "C:\Users\Usuario\AppData\Local\Programs\Python\Python39\lib\site-packages\bs4\element.py", line 1406, in __getitem__
return self.attrs[key]
KeyError: 'title'
可能是你想做的:
PRUEBA = CodigoJuegos.a.get_text("title")
首先,你应该使用PEP8 styling。很难阅读你的代码。
如果您想用最少的代码更改来解决它,请执行以下操作:
PRUEBA = CodigoJuegos.a.span.text
也就是说,我专业地使用(在其他工具 bs4 中)抓取网站,我会选择这样的东西:
import requests
from bs4 import BeautifulSoup
search_url = "https://store.steampowered.com/search"
category1 = ('998', '996')
category2 = '29'
params = {
'sort_by': 'Price_ASC',
'category1': ','.join(category1),
'category2': category2,
}
response = requests.get(
search_url,
params=params
)
soup = BeautifulSoup(response.text, "html.parser")
elms = soup.find_all("span", {"class": "title"})
for elm in elms:
print(elm.text)
输出:
Barro F
The Last Hope: Trump vs Mafia - North Korea
Ninja Stealth
Tetropunk
Oracle
Legend of Himari
Planes, Bullets and Vodka
Shift
Blast-off
...
如果您已经依赖于 bs4
,您也可以得到 requests
。
使用 css 选择器 'spna.title'
CodigoJuegos = PaginaSoup.select('span.title')
for t in CodigoJuegos:
print(t.text)
我开始在 python 上学习一些复杂的代码,今天我决定使用 BeautifulSoup。当我试图获取产品的标题时出现问题,我尝试将“.find”更改为“.findAll”但找不到解决方案。有人请帮助我。 这是我的代码:
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as Soup
ListaSteam = "https://store.steampowered.com/search/?sort_by=Price_ASC&category1=998%2C996&category2=29"
#PAGINA - OBTENCION - CERRADA
Pagina = uReq(ListaSteam)
PaginaHtml = Pagina.read()
Pagina.close()
#1 PASO
PaginaSoup = Soup(PaginaHtml, "html.parser")
CodigoJuegos = PaginaSoup.find("div",{"id":"search_resultsRows"})
PRUEBA = CodigoJuegos.a.span["title"]
print(PRUEBA)
错误如下:
This is the error:
`Traceback (most recent call last):
File "C:\Users\Usuario\Desktop\******", line 14, in <module>
PRUEBA = CodigoJuegos.a.span["title"]
File "C:\Users\Usuario\AppData\Local\Programs\Python\Python39\lib\site-packages\bs4\element.py", line 1406, in __getitem__
return self.attrs[key]
KeyError: 'title'
可能是你想做的:
PRUEBA = CodigoJuegos.a.get_text("title")
首先,你应该使用PEP8 styling。很难阅读你的代码。
如果您想用最少的代码更改来解决它,请执行以下操作:
PRUEBA = CodigoJuegos.a.span.text
也就是说,我专业地使用(在其他工具 bs4 中)抓取网站,我会选择这样的东西:
import requests
from bs4 import BeautifulSoup
search_url = "https://store.steampowered.com/search"
category1 = ('998', '996')
category2 = '29'
params = {
'sort_by': 'Price_ASC',
'category1': ','.join(category1),
'category2': category2,
}
response = requests.get(
search_url,
params=params
)
soup = BeautifulSoup(response.text, "html.parser")
elms = soup.find_all("span", {"class": "title"})
for elm in elms:
print(elm.text)
输出:
Barro F
The Last Hope: Trump vs Mafia - North Korea
Ninja Stealth
Tetropunk
Oracle
Legend of Himari
Planes, Bullets and Vodka
Shift
Blast-off
...
如果您已经依赖于 bs4
,您也可以得到 requests
。
使用 css 选择器 'spna.title'
CodigoJuegos = PaginaSoup.select('span.title')
for t in CodigoJuegos:
print(t.text)