我是一個關於文本挖掘和處理玩具項目的新手,用於從網站中分割文本並將其分割爲令牌。然而,使用Beautifulsoup下載內容後,我沒把它與.split
方法用下面的代碼如何在使用Beautifulsoup時獲取文本標記
# -*- coding: utf-8 -*-
import nltk
import operator
import urllib3
from bs4 import BeautifulSoup
http = urllib3.PoolManager()
url= 'http://python.org/'
response = http.request('GET',url)
# nltk.clean_html is dropped by NTLK
clean = BeautifulSoup(response.data,"html5lib")
# clean will have entire string removing all the html noise
tokens = [tok for tok in clean.split()]
print tokens[:100]
分裂的Python告訴我,
TypeError: 'NoneType' object is not callable
根據以前stackoverflow question,這是由於事實
clean不是一個字符串,它是一個bs4.element.Tag。當你試圖查找 拆分它,它會發揮它的魔力,並試圖找到一個名爲 拆分的子元素,但沒有。你打電話說沒有
在這種情況下,我應該如何調整我的代碼以實現獲取令牌的目標?謝謝。
它幾乎在我看來,你沒有讀過的BeautifulSoup文檔:https://www.crummy.com/software/BeautifulSoup/bs4/doc/。沒有一種方法可以以有用的方式從頁面獲取令牌。有必要對每一頁進行研究。 –
[BeautifulSoup Grab Visible Webpage Text]的可能重複(https://stackoverflow.com/questions/1936466/beautifulsoup-grab-visible-webpage-text) – Kos