简体   繁体   中英

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

When I try find_all I get that error AttributeError:

'NavigableString' object has no attribute '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. 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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