英文:
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)
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'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:
> {'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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论