两个相似命令中只有一个会引发错误“'NoneType' 对象没有属性 'replace'”

Only one of two similar commands induces error "'NoneType' object has no attribute 'replace'"

我正在尝试用命令 content1.string.replace(str(remove1), '') 替换另一个字符串中的一个字符串。完整代码为

from bs4 import BeautifulSoup
import urllib3
http = urllib3.PoolManager()

url = 'https://www.collinsdictionary.com/dictionary/french-english/aimer'
r1 = http.request('get', url)
r2 = BeautifulSoup(r1.data, 'html.parser')

entry_name1 = r2.find('span', {'class' : 'orth'})
print(type(entry_name1))

entry_name2 = entry_name1.string.replace('<span class="orth">', '').replace('</span>', '')
print(type(entry_name2))

content1 = r2.find('div', {'class' : 'res_cell_center'})
print(type(content1))

remove1 = content1.find('div', {'class' : 'cB cB-hook'})
print(type(str(remove1)))

content2 = content1.string.replace(str(remove1), '')

结果是

<class 'bs4.element.Tag'>
<class 'str'>
<class 'bs4.element.Tag'>
<class 'str'>
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-14-58c961c32cdb> in <module>
     19 print(type(str(remove1)))
     20 
---> 21 content2 = content1.string.replace(str(remove1), '')

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

entry_name1.string.replace('<span class="orth">', '').replace('</span>', '')content1.string.replace(str(remove1), '')两个命令的对象类型相同

能否请您详细说明后者是如何导致错误的?


更新: 应@Andrej Kesely 的要求,我尝试抓取那个 url 的主要内容。首先,我抓取了 div class = "res_cell_center" 标记的内容,随后我从中删除了 div class = "cB cB-hook".

标记的内容

要获取页面内容,可以使用.get_text()方法,不需要用空字符串替换sections soup:

import requests
from bs4 import BeautifulSoup

url = 'https://www.collinsdictionary.com/dictionary/french-english/aimer'
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0'}
soup = BeautifulSoup(requests.get(url, headers=headers).content, 'html.parser')

print(soup.h2.text)
print(soup.select_one('.hom').get_text(strip=True, separator=' '))

打印:

aimer
Full verb table transitive verb 1. ( d’amour ) to love Elle aime ses enfants. She loves her children. 2. ( d’amitié, par affection ) to like bien aimer qn to like sb J’aime bien Paul, on peut vraiment compter sur lui. I really like Paul, he’s so reliable. Je n’aime pas beaucoup Marie. I don’t like Marie very much. 3. ( par goût ) [ aliment, divertissement, auteur ] to like Tu aimes le chocolat ? Do you like chocolate? bien aimer qch to like sth J’aime bien jouer au tennis. I like playing tennis. aimer faire qch to like doing sth J’aime assez aller au cinéma. I quite like going to the cinema. 4. ( préférence ) aimer mieux qn que qn to prefer sb to sb aimer mieux qch que qch to prefer sth to sth J’aime mieux Paul. I prefer Paul. J’aime mieux Paul que Pierre. I prefer Paul to Pierre. Il aime mieux faire la cuisine qu’aller au restaurant. He’d rather cook than go to a restaurant. j’aime mieux vous dire que , j’aime autant vous dire que I may as well tell you that 5. ( conditionnel : souhait ) j’aimerais ... I would like ... J’aimerais aller en Écosse. I’d like to go to Scotland. Aimeriez-vous que je vous accompagne ? Would you like me to come with you? j’aimerais bien ... I would like ... J’aimerais bien m’en aller. I’d like to go. j’aimerais mieux faire ... I’d rather do ... J’aimerais mieux ne pas y aller. I’d rather not go. J’aimerais mieux y aller maintenant. I’d rather go now. J’aimerais autant y aller maintenant. I’d rather go now.

编辑:要获得 HTML 标记,您可以这样做:

import requests
from bs4 import BeautifulSoup

url = 'https://www.collinsdictionary.com/dictionary/french-english/aimer'
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0'}
soup = BeautifulSoup(requests.get(url, headers=headers).content, 'html.parser')

for script in soup.select('script, .hcdcrt, #ad_contentslot_1, #ad_contentslot_2'):
    script.extract()

print(soup.h2.text)
print(''.join(map(str, soup.select_one('.hom').contents)))

打印:

aimer
<span class="gramGrp"><span class="xr"> <a class="link-right verbtable" href="https://www.collinsdictionary.com/dictionary/french-english/conjugation/aimer">Full verb table</a>
</span><span class="hi rend-sc pos">transitive verb</span></span><div class="sense"><span class="sensenum bluebold">1. </span> <span class="lbl type-misc"><span class="punctuation">(</span>d’amour<span class="punctuation">)</span></span> <span class="cit type-translation quote">to love</span><div class="cit type-example"><span class="quote">Elle aime ses enfants.</span> <span class="cit type-translation quote">She loves her children.</span></div></div><div class="sense"><span class="sensenum bluebold">2. </span> <span class="lbl type-misc"><span class="punctuation">(</span>d’amitié, par affection<span class="punctuation">)</span></span> <span class="cit type-translation quote">to like</span><div class="re type-phr"><span class="form type-phr orth">bien aimer qn</span> <span class="cit type-translation quote">to like sb</span><div class="cit type-example"><span class="quote">J’aime bien Paul, on peut vraiment compter sur lui.</span> <span class="cit type-translation quote">I really like Paul, he’s so reliable.</span></div><div class="cit type-example"><span class="quote">Je n’aime pas beaucoup Marie.</span> <span class="cit type-translation quote">I don’t like Marie very much.</span></div></div> </div><div class="sense"><span class="sensenum bluebold">3. </span> <span class="lbl type-misc"><span class="punctuation">(</span>par goût<span class="gramGrp colloc"><span class="punctuation">) </span><span class="punctuation">[</span>aliment, divertissement, auteur<span class="punctuation">]</span></span></span> <span class="cit type-translation quote">to like</span><div class="cit type-example"><span class="quote">Tu aimes le chocolat ?</span> <span class="cit type-translation quote">Do you like chocolate?</span></div><div class="re type-phr"><span class="form type-phr orth">bien aimer qch</span> <span class="cit type-translation quote">to like sth</span><div class="cit type-example"><span class="quote">J’aime bien jouer au tennis.</span> <span class="cit type-translation quote">I like playing tennis.</span></div></div><div class="re type-phr"><span class="form type-phr orth">aimer faire qch</span> <span class="cit type-translation quote">to like doing sth</span><div class="cit type-example"><span class="quote">J’aime assez aller au cinéma.</span> <span class="cit type-translation quote">I quite like going to the cinema.</span></div></div> <div class="mpuslot_b-container"> 


</div> </div><div class="sense"><span class="sensenum bluebold">4. </span> <span class="lbl type-misc"><span class="punctuation">(</span>préférence<span class="punctuation">)</span></span><div class="re type-phr"><span class="form type-phr orth">aimer mieux qn que qn</span> <span class="cit type-translation quote">to prefer sb to sb</span></div><div class="re type-phr"><span class="form type-phr orth">aimer mieux qch que qch</span> <span class="cit type-translation quote">to prefer sth to sth</span><div class="cit type-example"><span class="quote">J’aime mieux Paul.</span> <span class="cit type-translation quote">I prefer Paul.</span></div><div class="cit type-example"><span class="quote">J’aime mieux Paul que Pierre.</span> <span class="cit type-translation quote">I prefer Paul to Pierre.</span></div><div class="cit type-example"><span class="quote">Il aime mieux faire la cuisine qu’aller au restaurant.</span> <span class="cit type-translation quote">He’d rather cook than go to a restaurant.</span></div></div><div class="re type-phr"><span class="form type-phr orth">j’aime mieux vous dire que</span><span class="form type-phr"><span class="punctuation">, </span><span class="orth">j’aime autant vous dire que</span></span> <span class="cit type-translation quote">I may as well tell you that</span></div> </div><div class="sense"><span class="sensenum bluebold">5. </span> <span class="lbl type-misc"><span class="punctuation">(</span>conditionnel : souhait<span class="punctuation">)</span></span><div class="re type-phr"><span class="form type-phr orth">j’aimerais ...</span> <span class="cit type-translation quote">I would like ...</span><div class="cit type-example"><span class="quote">J’aimerais aller en Écosse.</span> <span class="cit type-translation quote">I’d like to go to Scotland.</span></div><div class="cit type-example"><span class="quote">Aimeriez-vous que je vous accompagne ?</span> <span class="cit type-translation quote">Would you like me to come with you?</span></div></div><div class="re type-phr"><span class="form type-phr orth">j’aimerais bien ...</span> <span class="cit type-translation quote">I would like ...</span><div class="cit type-example"><span class="quote">J’aimerais bien m’en aller.</span> <span class="cit type-translation quote">I’d like to go.</span></div></div><div class="re type-phr"><span class="form type-phr orth">j’aimerais mieux faire ...</span> <span class="cit type-translation quote">I’d rather do ...</span><div class="cit type-example"><span class="quote">J’aimerais mieux ne pas y aller.</span> <span class="cit type-translation quote">I’d rather not go.</span></div><div class="cit type-example"><span class="quote">J’aimerais mieux y aller maintenant.</span> <span class="cit type-translation quote">I’d rather go now.</span></div><div class="cit type-example"><span class="quote">J’aimerais autant y aller maintenant.</span> <span class="cit type-translation quote">I’d rather go now.</span></div></div> <div class="mpuslot_b-container"> 


</div> </div>