Python BeautifulSoup遍历表数据 [英] Python BeautifulSoup Looping Through Table Data

查看:74
本文介绍了Python BeautifulSoup遍历表数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是Python的新手.我正在尝试从此页面此页面中捕获一些数据.我正在尝试获取在两个列表中捕获的项目名称和项目类型.我可以弄清楚以后如何将它们加入一个表中.任何帮助都会很棒!

Very new to Python here. I'm trying to capture some data from this page this page. I'm trying to get the item name and the item type captured in two lists. I can figure out how to join them into one table later. Any help would be great!

代码行可以单独工作,但是循环对我而言不起作用. 这样可以成功生成两行代码:

The lines of code work on their own but the loop doesn't work for me. This produces two lines of code successfully:

import urllib
import bs4 as bs

sauce = urllib.request.urlopen('https://us.diablo3.com/en/item/helm/').read()
soup = bs.BeautifulSoup(sauce, 'lxml')

item_details =  soup.find('tbody')
print(item_details) 

item_name = item_details.find('div', class_='item-details').h3.a.text
print(item_name)

item_type = item_details.find('ul', class_='item-type').span.text
print(item_type)

一遍又一遍地重复第一个item_name的值:

This repeats the value of the first item_name over and over:

for div in soup.find_all('div', class_='item-details'):
    item_name = item_details.find('div', class_='item-details').h3.a.text
    print(item_name)
    item_type = item_details.find('ul', class_='item-type').span.text
    print(item_type)

这是输出:

Veil of Steel
Magic Helm
Veil of Steel
Magic Helm
Veil of Steel
Magic Helm
Veil of Steel
Magic Helm
Veil of Steel
Magic Helm
Veil of Steel
Magic Helm
Veil of Steel
Magic Helm
...

推荐答案

您需要使用find_all(返回列表)而不是find(返回单个元素):

You need to use find_all (returns list) instead of find (returns single element):

for i, j in zip(item_details.find_all('div', class_='item-details'), item_details.find_all('ul', class_='item-type')):
    print(i.h3.a.text, " - ", j.span.text)

输出为:

Veil of Steel  -  Magic Helm
Leoric's Crown  -  Legendary Helm
Harlequin Crest  -  Magic Helm
The Undead Crown  -  Magic Helm
...

或更可读的格式:

names = item_details.find_all('div', class_='item-details')
types = item_details.find_all('ul', class_='item-type')

for name, type in zip(names, types):
    print(name.h3.a.text, " - ", type.span.text)

这篇关于Python BeautifulSoup遍历表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆