简体   繁体   English

Python 美丽的汤 TypeError: find() 没有关键字 arguments

[英]Python beautiful soup TypeError : find() takes no keyword arguments

When I try find_all I get that error AttributeError:当我尝试 find_all 时,我得到那个错误 AttributeError:

'NavigableString' object has no attribute 'find_all' 'NavigableString' object 没有属性 'find_all'

    import requests
    from bs4 import BeautifulSoup as bs 

    url = "https://www.gittigidiyor.com/bilgisayar-tablet/huawei-matebook-d-14-amd-53010wpx-dizustu-bilgisayar-laptop_pdp_555531393"

    r = requests.get(url)

    soup = bs(r.content, "lxml")
    data = soup.find("div", attrs = {"class":"gg-w-24 gg-d-24 gg-t-24 gg-m-24 padding-none-m"})

    for i in data:
        price = i.find("div", attrs = {"id":"sp-price-lowPrice"})
        print(price.text)

There is only one item with class "gg-w-24 gg-d-24 gg-t-24 gg-m-24 padding-none-m" and only one item with id "sp-price-lowPrice" on that page.在该页面上只有一件带有 class "gg-w-24 gg-d-24 gg-t-24 gg-m-24 padding-none-m" 的商品,并且只有一件带有 id "sp-price-lowPrice" 的商品. As such why not simply do:因此,为什么不简单地做:

price = soup.find("div", attrs = {"id":"sp-price-lowPrice"})

If you do expect multiple items having "gg-w-24 gg-d-24 gg-t-24 gg-m-24 padding-none-m", then amend data to:如果您确实希望多个项目具有“gg-w-24 gg-d-24 gg-t-24 gg-m-24 padding-none-m”,则将数据修改为:

data = soup.find_all("div", attrs = {"class":"gg-w-24 gg-d-24 gg-t-24 gg-m-24 padding-none-m"})
# for i in data: 
#... rest of your code here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM