首页 > 解决方案 > python double for循环属性问题

问题描述

所以我有以下两个 BeautifulSoup 发现和我正在抓取的网页:

r = requests.get("https://www.viperprint.pl/produkt/arkusze-plano/AP01")
soup = BeautifulSoup(r.content)
elems = soup.find_all('a', {'class': 'tabela_cenowa eprint_product_link add_to_cart_link'})
hehes = soup.find_all('a', {'id': 'dLabel'})

我需要的是一个双 for 循环,它打印列表以分隔 .csv 文件中的列。

这是我的问题:

>>> for elem, hehe in zip(elems, hehes):
...     nazwa = hehe.get('title')
...     qty = elem.attrs.get('data-qty')
...     print(nazwa, qty)

给我下面的输出。这是错误的,因为第 1 列中的每个元素(所以 'Arkusze PLANO' 和下面的所有元素)应该彼此相邻,并且只有第 2 列中的第一个数字('100')也应该在一行中.

错误的输出:

('Arkusze PLANO', '100')

('A1+ (880 x 630 mm)', '250')

('Dwustronnie kolorowe (4+4 CMYK)', '500')

(u'Kreda b\u0142ysk 130g', '1000')

('Bez uszlachetniania (0+0)', '1500')

(None, '2000')

预期输出:

'Arkusze PLANO';'A1+ 880 x 630 mm';'Dwustronnie kolorowe 4+4 CMYK';u'Kreda b\u0142ysk 130g';'Bez uszlachetniania 0+0';'100'

我想做的是像这样使用 .attrs 函数:

for elem, hehe in zip(elems, hehes):
    nazwa = hehe[0].get('title')
    format = hehe[1].get('title')
    qty = elem.attrs.get('data-qty')
    print(nazwa, format, qty)

...但我收到以下错误,不知道如何继续:

Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/usr/lib/python2.7/site-packages/bs4/element.py", line 905, in __getitem__
return self.attrs[key]
KeyError: 0

我很抱歉这么长的帖子,但我想提供尽可能多的细节。

标签: pythonfor-loopbeautifulsoup

解决方案


这将为您提供您在列表中查看的所需输出:

import requests
import bs4


r = requests.get("https://www.viperprint.pl/produkt/arkusze-plano/AP01")
soup = bs4.BeautifulSoup(r.content, 'html.parser')
elems = soup.find_all('a', {'class': 'tabela_cenowa eprint_product_link add_to_cart_link'})
hehes = soup.find_all('a', {'id': 'dLabel'})

results = []

nazwa_list = []
qty_value = None

for elem, hehe in zip(elems, hehes):
    nazwa = hehe.get('title')

    if qty_value == None:
        qty_value = elem.attrs.get('data-qty')

    if nazwa != None:
        nazwa_list.append(nazwa)

nazwa_list.append(qty_value)
results = nazwa_list

输出:

In  [1]: print (results)
Out [1]: ['Arkusze PLANO', 'A1+ (880 x 630 mm)', 'Dwustronnie kolorowe (4+4 CMYK)', 'Kreda błysk 130g', 'Bez uszlachetniania (0+0)', '100']

但是您声明要放入csv。所以你可以把它放在一张桌子上,然后如果你愿意也可以这样处理

import requests
import bs4
import pandas as pd

r = requests.get("https://www.viperprint.pl/produkt/arkusze-plano/AP01")
soup = bs4.BeautifulSoup(r.content, 'html.parser')
elems = soup.find_all('a', {'class': 'tabela_cenowa eprint_product_link add_to_cart_link'})
hehes = soup.find_all('a', {'id': 'dLabel'})


results = pd.DataFrame()

for elem, hehe in zip(elems, hehes):
    nazwa = hehe.get('title')
    qty = elem.attrs.get('data-qty')
    temp_df = pd.DataFrame([[nazwa, qty]], columns = ['title', 'qty'])

    results = results.append(temp_df)

推荐阅读