deal_way = list(soup.select('p.biaoqian_li').stripped_strings)
#
deal_ways = soup.select('p.biaoqian_li')
for deal_way in deal_ways:
x = list(deal_way.stripped_strings)
上面两个代码不是表示同样的意思吗?
(1)为什么上面报错:AttributeError: 'list' object has no attribute 'stripped_strings'
(2)下面的可以运行
伊谢尔伦2017-04-18 10:22:23
soup.select 返回的是符合条件的列表(list),列表当然没有那个属性(因为那个属性是bs4 Tag对象上的属性)
而下面可以是因为列表里面的元素就是Tag对象
ringa_lee2017-04-18 10:22:23
Yessoup.select()
返回的是什么不是很清楚,但是看你下面的代码soup.select('p.biaoqian_li')
returns a collection. Only the items in the collection have the stripped_strings attribute, not the collection.
The two pieces of code are different
阿神2017-04-18 10:22:23
Obviously the code below uses one more for
than the above code
You can print soup.select('p.biaoqian_li')
and soup.select('p.biaoqian_li')
与 for
中的 deal_way
in deal_way
Compare the differences
大家讲道理2017-04-18 10:22:23
I’m really sorry, I didn’t make it clear at the beginning
soup.select('p.biaoqian_li')解析出来的列表只有一个数据
It’s because I made a mistake in writing the code, so I keep reporting errors. It’s written as:
list(soup.select('p.biaoqian_li')[0].stripped_strings)
这样就没问题了
Thank you everyone