簡體   English   中英

AttributeError:'NoneType'對象沒有屬性'text'beautifulsoup python

[英]AttributeError: 'NoneType' object has no attribute 'text' beautifulsoup python

import bs4,requests, re

#Get epsiode webpage
epPage = requests.get('http://www.friends-tv.org/zz101.html')
epPage.raise_for_status()

#use the page in bs4
soup = bs4.BeautifulSoup(epPage.text, 'lxml')
results = soup.find_all('dt')

#Populate the list
quotes = []
for result in results:
    character = result.find('b').text
    speech = result.contents[1][1:-2]
    quotes.append((character,speech))

print (quotes)`

我正在嘗試從以下站點獲取報價單和發話人的名單: http : //www.friends-tv.org/zz101.html 但是,我收到錯誤消息:

Traceback (most recent call last):
  File "/Users/yusufsohoye/pythoncode/Friends1.py", line 16, in <module>
    character = result.find('b').text
AttributeError: 'NoneType' object has no attribute 'text' 

當我隔離結果列表中的每個dt項時,它會起作用,但是當我嘗試解析整個頁面並構建列表時,它就不會起作用。

謝謝

這應該有所幫助。

import bs4,requests, re

#Get epsiode webpage
epPage = requests.get('http://www.friends-tv.org/zz101.html')
epPage.raise_for_status()

#use the page in bs4
soup = bs4.BeautifulSoup(epPage.text, 'lxml')
results = soup.find_all('dt')

#Populate the list
quotes = []
for result in results:
    character = result.find('b')
    if character:     #Check Condition to see if character in dt tag
        speech = result.contents[1][1:-2]
        squotes.append((character,speech))

print(quotes)

暫無
暫無

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

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