我开始学习在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'
可能是您想要做的:
使用css选择器
'spna.title'
首先,您应该使用PEP8 styling。很难读懂你的代码
如果要以最少的代码更改量解决此问题,请执行以下操作:
这就是说,我专业地使用bs4(以及其他工具)浏览网站,我会选择这样的方式:
输出:
如果您已经对
bs4
有依赖关系,那么还可以获得requests
相关问题 更多 >
编程相关推荐