Python中文
首页
教程
问答
标签
搜索
登录
注册
python双循环attrs issu
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>因此,我有以下两个美丽的发现和网页,我的网页拉屎:</p> <pre><code>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'}) </code></pre> <p>我需要的是一个双for循环,它将列表打印到.csv文件中的不同列。你知道吗</p> <p>我的问题是:</p> <pre><code>>>> for elem, hehe in zip(elems, hehes): ... nazwa = hehe.get('title') ... qty = elem.attrs.get('data-qty') ... print(nazwa, qty) </code></pre> <p>给我输出吼声。这是错误的,因为列1中的每个元素(即“Arkusze PLANO”和下面的所有内容)应该相邻,并且只有列2中的第一个数字(“100”)也应该在一行中。你知道吗</p> <p>输出错误:</p> <pre><code>('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') </code></pre> <p>预期产量:</p> <pre><code>'Arkusze PLANO';'A1+ 880 x 630 mm';'Dwustronnie kolorowe 4+4 CMYK';u'Kreda b\u0142ysk 130g';'Bez uszlachetniania 0+0';'100' </code></pre> <p>我要做的是使用.attrs函数,如下所示:</p> <pre><code>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) </code></pre> <p>……但我犯了以下错误,不知道如何继续:</p> <pre><code>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 </code></pre> <p>我很抱歉这么长的帖子,但我想提供尽可能多的细节。你知道吗</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>我添加了另一个循环以获得完全需要的输出。你知道吗</p> <pre><code>for elem in elems: qty = elem.attrs.get('data-qty') print(results, qty, pricenum) </code></pre> <p>谢谢大家的帮助!你知道吗</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何添加虚拟方法
1 回答
如何添加表示整数的擦边字符串?
4 回答
如何添加要在Bokeh中使用的新font.ttf文件?
7 回答
如何添加要显示的矩阵XY轴编号和XY轴
1 回答
如何添加计数?
2 回答
如何添加计数器函数?
6 回答
如何添加计数器列来计算数据帧中另一列中的特定值?
9 回答
如何添加计数器来跟踪while循环中的月份和年份?
9 回答
如何添加计数并删除countplot的顶部和右侧脊椎?
10 回答
如何添加计时器wx.应用程序更新窗口对象的主循环?
7 回答
如何添加评论到帖子?PostDetailVew,Django 2.1.5
4 回答
如何添加评论拉梅尔亚姆
10 回答
如何添加诸如矩阵Python/Pandas之类的数据帧?
4 回答
如何添加谷歌地点自动完成到Flask?
6 回答
如何添加超时、python discord bot
6 回答
如何添加超过1dp的检查
1 回答
如何添加距离方法
6 回答
如何添加跟随游戏的敌人精灵
6 回答
如何添加路径以便python可以找到程序?
8 回答
如何添加身份验证/安全性以使用happybase访问HBase?
9 回答