簡體   English   中英

prettify 適用於 Soup 但不適用於 Soup1=Soup.find_all

[英]prettify works on Soup but not on Soup1=Soup.find_all

MyAlphaSoup = BeautifulSoup(website_to_crawl.text, 'html.parser')

MyAlphaSoup.prettify()

MyAlphaAlphaSoup = MyAlphaSoup.find_all("div", {"class": "lister-item featurefilm"})

MyAlphaAlphaSoup.prettify()

我在第 4 行遇到錯誤

raise AttributeError( AttributeError: ResultSet object has no attribute 'prettify'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?

soup.find_all 不只是將湯減少到更小的子集嗎? 所以類型沒有改變?

錯誤

代碼圖片

那是因為 function find_all 返回一個列表。 因此,如果您指定列表的元素或遍歷它們,您應該能夠做到這一點:

my_alpha_soup = BeautifulSoup(website_to_crawl.text, 'html.parser')

my_alpha_soup.prettify()

list_of_elements = my_alpha_soup.find_all("div", {"class": "lister-item featurefilm"})

for element in list_of_elements:
    print(element.prettify())

此外,約定僅對類使用首字母大寫字母。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM