search

Home  >  Q&A  >  body text

python - 感觉同样的两段代码,运行起来不一样

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)下面的可以运行

阿神阿神2781 days ago743

reply all(4)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:22:23

    soup.select 返回的是符合条件的列表(list),列表当然没有那个属性(因为那个属性是bs4 Tag对象上的属性)
    而下面可以是因为列表里面的元素就是Tag对象

    reply
    0
  • ringa_lee

    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

    reply
    0
  • 阿神

    阿神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

    reply
    0
  • 大家讲道理

    大家讲道理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

    reply
    0
  • Cancelreply