我试图通过此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,拉蒂
  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 are 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,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,gbe alakan lo”,“ Igi gogoro ma gun mi loju,拉提奥克雷
  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。等值
  ponla,iyo o se tu danu',“ Moja mosa laa mo akinkanju loju ogun”}

10-04 22:27