python - 感觉同样的两段代码,运行起来不一样
阿神
阿神 2017-04-18 10:20:23
0
4
741
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)下面的可以运行

阿神
阿神

闭关修行中......

reply all(4)
伊谢尔伦
soup.select 返回的是符合条件的列表(list),列表当然没有那个属性(因为那个属性是bs4 Tag对象上的属性)
而下面可以是因为列表里面的元素就是Tag对象
左手右手慢动作

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

阿神

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

大家讲道理

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template