Python 3.x 我想打印出所有的项目,但我只得到第一个

Python 3.x 我想打印出所有的项目,但我只得到第一个,python-3.x,beautifulsoup,Python 3.x,Beautifulsoup,我尝试放弃此网站: 我试图从item title div收集所有的项目名称。 问题是当我想打印出来时,我只得到第一项 import requests from bs4 import BeautifulSoup page = requests.get('https://www.notebook.hu/notebook/acer-notebook/aspire- sorozat') soup = BeautifulSoup(page.text, 'html.parser') cikkCime

我尝试放弃此网站:

我试图从item title div收集所有的项目名称。 问题是当我想打印出来时,我只得到第一项

import requests
from bs4 import BeautifulSoup

page = requests.get('https://www.notebook.hu/notebook/acer-notebook/aspire- 
sorozat')
soup = BeautifulSoup(page.text, 'html.parser')

cikkCimek = soup.find(class_='item-title')
cikkCimek_items = cikkCimek.find_all('a')

for cikkCimek in cikkCimek_items:
print(cikkCimek.prettify())
将仅返回具有类项标题的第一个元素/块。在第一个块中,只有一个元素带有标签

更改为
。全部查找

import requests
from bs4 import BeautifulSoup

page = requests.get('https://www.notebook.hu/notebook/acer-notebook/aspire-sorozat')
soup = BeautifulSoup(page.text, 'html.parser')

cikkCimek = soup.find_all(class_='item-title')


for elem in cikkCimek:
    cikkCimek_items = elem.find_all('a')
    for elem_items in cikkCimek_items:
        print(elem_items.prettify())

@chitown88解释了问题,并建议在内部循环中使用另一个
find\u all()
。有一种更有效的方法可以一次性完成:

for cikkCimek in soup.select(".item-title a"):
    print(cikkCimek.prettify())

其中
.item title a
是一个CSS选择器,它将元素中的所有
a
元素与类
item title
匹配,这是一个更好的解决方案。我必须记住
。如果这是我的问题,请选择
。只是轻轻推了一下@Csaba Kocsis。这是更好的答案though@chitown88我把所有的答案和问题都混在一起了,太多了:谢谢你们的帮助。我刚开始容忍编程,也许这不是从网络垃圾开始的最佳方式,但如果这能激励我,我能做什么呢一点也不坏!您必须学习所有关于列表、循环、字典/json结构、表/dataframes等的知识,所以一定要学习。而且很有趣!你在实践中学习。
for cikkCimek in soup.select(".item-title a"):
    print(cikkCimek.prettify())