“Beautiful Soup: AttributeError: ‘NoneType’ object has no attribute ‘text'”

huangapple go评论92阅读模式
英文:

Beautiful soup: AttributeError: 'NoneType' object has no attribute 'text'

问题

我正在尝试从这个网站提取前16个粗体句子,然后将它们插入到一个数据框中,但我一直遇到这个错误。我已经尝试了我所能做的一切,而且我是一个网页抓取的初学者。

import requests
import pandas as pd
from bs4 import BeautifulSoup

res = requests.get('https://www.nairaland.com/2838393/owe-ile-yoruba-some-lovely')
soup = BeautifulSoup(res.content, 'html')

yoruba = []
for word in soup.findAll('b'):
    name = word.find('i')
    yoruba.append(name.text)

AttributeError Traceback (most recent call last)
in
7 for word in soup.findAll('b'):
8 name = word.find('i')
----> 9 yoruba1.append(name.text)

AttributeError: 'NoneType' object has no attribute 'text'


<details>
<summary>英文:</summary>

I am trying to extract the first 16 **bold sentences** via this [website](https://www.nairaland.com/2838393/owe-ile-yoruba-some-lovely) before inserting them into a dataframe, and i kept having this error. I have tried everything i could, also i&#39;m a beginner in webscraping.

import requests
import pandas as pd
from bs4 import BeautifulSoup

res = requests.get('https://www.nairaland.com/2838393/owe-ile-yoruba-some-lovely')
soup = BeautifulSoup(res.content,'html')

yoruba = []
for word in soup3.findAll('b'):
name = word.find('i')
yoruba.append(name.text)


AttributeError Traceback (most recent call last)
<ipython-input-61-9d3379b8790a> in <module>
7 for word in soup3.findAll('b'):
8 name = word.find('i')
----> 9 yoruba1.append(name.text)

AttributeError: 'NoneType' object has no attribute 'text'



</details>


# 答案1
**得分**: 0

```python
import requests
from bs4 import BeautifulSoup

res = requests.get('https://www.nairaland.com/2838393/owe-ile-yoruba-some-lovely')
soup = BeautifulSoup(res.content, 'html.parser')

yoruba = []
for word in soup.findAll('b'):
    name = word.find('i')
    if name:
        yoruba.append(name.text)

print(set(yoruba))

output:

{'Atari ajanaku kii seru omode', 'Igi gogoro ma gun mi loju, lati okere laati wo', 'Enibama ba esu jeun sibi e a gun', "Ori leja fi l'abu ja", 'Aseju ni irun aya, irun abe tito', 'Ki tan lara were koma ku "HOI"', 'Foriti foriti lomu ki ori agba pa', 'Omi titun ti ru, eja titun ti wonu e', "Eni bama m'obo akoko se bi lagido", "Pai lotun pai losi, t'oju o ba fo, a ko ma wo bai bai ni", "Ejawo ninu apon tio yo, elogbomi'ila kana", "Ninu odo adagun ni alakan ti le fo epo, t'odo ba di agadangba, a gbe alakan lo", 'Igi gogoro ma gun mi loju, lati okere laati ye', 'Aje ke lana omo ku loni, tani o sai mope aje ana lo pa omo je', "Isiro l'oko dido", 'Oro yi so simi lenu o buyo si. Iso o se ponla, iyo o se tu danu', 'Moja mosa laa mo akinkanju loju ogun'}


<details>
<summary>英文:</summary>

import requests
from bs4 import BeautifulSoup

res = requests.get('https://www.nairaland.com/2838393/owe-ile-yoruba-some-lovely')
soup = BeautifulSoup(res.content,'html.parser')

yoruba = []
for word in soup.findAll('b'):
name = word.find('i')
if name:
yoruba.append(name.text)

print(set(yoruba))

output:

&gt; {&#39;Atari ajanaku kii seru omode&#39;, &#39;Igi gogoro ma gun mi loju, lati
&gt; okere laati wo&#39;, &#39;Enibama ba esu jeun sibi e a gun&#39;, &quot;Ori leja fi
&gt; l&#39;abu ja&quot;, &#39;Aseju ni irun aya, irun abe tito&#39;, &#39;Ki tan lara were koma
&gt; ku &quot;HOI&quot;&#39;, &#39;Foriti foriti lomu ki ori agba pa&#39;, &#39;Omi titun ti ru, eja
&gt; titun ti wonu e&#39;, &quot;Eni bama m&#39;obo akoko se bi lagido&quot;, &quot;Pai lotun pai
&gt; losi, t&#39;oju o ba fo, a ko ma wo bai bai ni&quot;, &quot;Ejawo ninu apon tio yo,
&gt; elogbomi&#39;ila kana&quot;, &quot;Ninu odo adagun ni alakan ti le fo epo, t&#39;odo ba
&gt; di agadangba, a gbe alakan lo&quot;, &#39;Igi gogoro ma gun mi loju, lati okere
&gt; laati ye&#39;, &#39;Aje ke lana omo ku loni, tani o sai mope aje ana lo pa omo
&gt; je&#39;, &quot;Isiro l&#39;oko dido&quot;, &#39;Oro yi so simi lenu o buyo si. Iso o se
&gt; ponla, iyo o se tu danu&#39;, &#39;Moja mosa laa mo akinkanju loju ogun&#39;}

</details>



huangapple
  • 本文由 发表于 2020年1月6日 22:56:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614266.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定