错误:AttributeError:'NoneType'对象没有属性'strip'

时间:2020-01-29 07:58:51

标签: python web-scraping

在尝试提取一些文本并通过此site删除文本escape characters时,我遇到一些错误,将不胜感激。 该错误也适用于第二个循环,我正在使用strip删除escape characters。我想将['We prepare the saddle, and the goat presents itself; is it a burden for the lineage of goats?','You have been crowned a king, and yet you make good-luck charms; would you be crowned God?','We lift a saddle and the goat (kin) scowls; it is no burden for a sheep.']附加到list并将the text in the bracket附加到另一个list

html = '
<p xmlns:ino="http://namespaces.softwareag.com/tamino/response2" xmlns:xq="http://namespaces.softwareag.com/tamino/XQuery/result" xmlns:xql="http://metalab.unc.edu/xql/">
A di g&#224;&#225;r&#236; s&#237;l&#232;&#803; ew&#250;r&#233;&#803; &#324;y&#7885;j&#250;; &#7865;r&#249; &#236;ran r&#232;&#803; ni?<br>
We prepare the saddle, and the goat presents itself; is it a burden for the lineage of goats?<br>
(Goats that know their place do not offer their backs to be saddled.)<br>
This is a variant of A gb&#233; g&#224;&#225;r&#236; &#7885;m&#7885; ew&#250;r&#233;&#803; &#324;roj&#250; . . .<br>
</p>  

<p xmlns:ino="http://namespaces.softwareag.com/tamino/response2" xmlns:xq="http://namespaces.softwareag.com/tamino/XQuery/result" xmlns:xql="http://metalab.unc.edu/xql/">
A fi o&#769;&#803; j&#7885;ba &#242; &#324;&#7779;&#224;w&#250;re o f&#233;&#803; j&#7865; &#7884;lo&#769;&#803;run ni?<br>
You have been crowned a king, and yet you make good-luck charms; would you be crowned God?<br>
(Being crowned a king is about the best fortune a mortal could hope for.)<br>
</p>

<p xmlns:ino="http://namespaces.softwareag.com/tamino/response2" xmlns:xq="http://namespaces.softwareag.com/tamino/XQuery/result" xmlns:xql="http://metalab.unc.edu/xql/">
A fij&#243; gba Aw&#224;; a f&#236;j&#224; gba Aw&#224;; b&#237; a &#242; b&#225; j&#243;, b&#237; a &#242; b&#225; j&#224;, b&#237; a b&#225; ti gba Aw&#224;, k&#242; t&#225;n b&#237;?<br> 
By dancing we take possession of Aw&#224;; through fighting we take possession of Aw&#224;; if we neither dance nor fight, but take possession of Aw&#224; anyway, is the result not the same?<br>
(Why make a huge production of a matter that is easily taken care of?)<br>
</p>

<p xmlns:ino="http://namespaces.softwareag.com/tamino/response2" xmlns:xq="http://namespaces.softwareag.com/tamino/XQuery/result" xmlns:xql="http://metalab.unc.edu/xql/">
A gb&#233; g&#224;&#225;r&#236; &#7885;m&#7885; ewur&#7865; &#324;roj&#250;; k&#236; &#237; &#7779;e &#7865;r&#249; &#224;g&#249;nt&#224;n.<br>
We lift a saddle and the goat (kin) scowls; it is no burden for a sheep.<br>
(The goat has no cause to scowl, because no one will condescend to ride it anyway.)<br>
This is a variant of A di g&#224;&#225;r&#236; s&#237;l&#232;&#803; . . .<br>
</p>'

from bs4 import BeautifulSoup
import requests

res = request.get(html)
soup = BeautifulSoup(res.content,'html.parser')

edu = {'Yoruba':[],'Translation':[],'Meaning':[]}

for i in range(0,220):
    # first loop
    for br in soup.select('p > br:nth-of-type(2)'):
        text = br.previous_sibling
        edu['Translation'].append(text.strip())
    # second loop
    for br in soup.select('p > br:nth-of-type(3)'):
        text = br.previous_sibling.strip()
        edu['Meaning'].append(text)

错误

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-186-6a48c8c3b845> in <module>
      9     for br in soup8.select('p > br:nth-of-type(2)'):
     10         text = br.previous_sibling
---> 11         edu['Translation'].append(text.strip())
     12     # third loop
     13     for br in soup8.select('p > br:nth-of-type(3)'):```

1 个答案:

答案 0 :(得分:2)

append没有return语句,因此它返回None。改为对插入的值调用strip()。如果返回的值是str

,您还应该添加检查
for i in range(0,220):
    # first loop
    for br in soup.select('p > br:nth-of-type(2)'):
        text = br.previous_sibling
        if isinstance(text, str):
            edu['Translation'].append(text.strip())

    # second loop
    for br in soup.select('p > br:nth-of-type(3)'):
        text = br.previous_sibling
        if isinstance(text, str):
            edu['Meaning'].append(text.strip())