gpt4 book ai didi

python - 美丽的汤 : AttributeError: 'NoneType' object has no attribute 'text'

转载 作者:太空宇宙 更新时间:2023-11-03 19:54:31 25 4
gpt4 key购买 nike

我正在尝试通过此 website 提取前 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 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'

最佳答案

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))

输出:

{'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'}

关于python - 美丽的汤 : AttributeError: 'NoneType' object has no attribute 'text' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59614266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com