刮我想從韋氏字典定義刮。防爆。 http://www.merriam-webster.com/dictionary/abandon從內部類
這是我想刮的代碼片段。
<div class="definition-block def-text">
<ul class="definition-list no-count">
<li>
<p class="definition-inner-item">
<span><span class="intro-colon">:</span> to leave and never return to (someone who needs protection or help)</span>
</p>
</li>
<li>
<p class="definition-inner-item">
<span><span class="intro-colon">:</span> to leave and never return to (something)</span>
</p>
</li>
<li>
<p class="definition-inner-item">
<span><span class="intro-colon">:</span> to leave (a place) because of danger</span>
</p>
</li>
</ul>
</div>
這裏是我的代碼
for element in soup.find(class_="definition-list no-count"):
if(soup.find("li")):
print element
輸出是
<li>
<p class="definition-inner-item">
<span><span class="intro-colon">:</span> to leave and never return to (someone who needs protection or help)</span>
</p>
</li>
<li>
<p class="definition-inner-item">
<span><span class="intro-colon">:</span> to leave and never return to (something)</span>
</p>
</li>
<li>
<p class="definition-inner-item">
<span><span class="intro-colon">:</span> to leave (a place) because of danger</span>
</p>
</li>
但我想<span>
裏面的定義。如果我使用get_text()方法,則會出現類型錯誤。
for element in soup.find(class_="definition-list no-count"):
if(soup.find("li")):
print soup.get_text(element)
輸出:
Traceback (most recent call last):
File "scrape.py", line 18, in <module>
print soup.get_text(element)
File "/usr/lib/python2.7/dist-packages/bs4/element.py", line 852, in get_text
strip, types=types)])
TypeError: 'NoneType' object is not callable
和你的代碼? – dnit13