Python在表D中循环

2024-09-24 04:22:27 发布

您现在位置:Python中文网/ 问答频道 /正文

对Python来说非常陌生。我正试图从这个页面this page捕获一些数据。我试图在两个列表中获取项目名称和项目类型。我以后可以想办法把它们合并到一个表中。任何帮助都太好了!p>

代码行自己工作,但循环对我不起作用。 这将成功生成两行代码:

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)

这会反复重复第一个项目名称的值:

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
...

Tags: oftextnamedivtypemagicfinddetails
3条回答

您可以在details部分的一个循环中执行此操作,而不是将它们保存在不同的列表中并进行匹配

item_details = []
for sections in soup.select('.item-details'):
    item_name = sections.select_one('h3[class*="subheader-"]').text.strip()  # partial match subheader-1, subheader-2, ....
    item_type = sections.select_one('ul[class="item-type"]').text.strip()
    item_details.append([item_name, item_type])

print(item_details)

输出

[['Veil of Steel', 'Magic Helm'], ["Leoric's Crown", 'Legendary Helm'], ....

这样做有效:

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

item_names = soup.find_all('div', class_='item-details')
for ele in item_names:
   print(ele.h3.a.text)

item_type = soup.find_all('ul', class_='item-type')
for ele in item_type:
    print(ele.span.text)

为什么你的代码不起作用:

看起来您的代码没有遍历所有元素,而是不断获取相同的元素(为所有元素查找所有元素)

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

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)

相关问题 更多 >