この記事では、オンラインテキストファイルからテキストを取得し、それらから必要なデータを抽出する方法について説明します。この記事では、ここで入手可能なテキストファイルを使用します< /a>.
現在の本番環境には次のものをインストールする必要があります:
- NLTCライブラリ
- urllib
- BeautifulSoupライブラリ
ステップ1:必要なライブラリをインポートします
import nltk < / p> from bs4 import BeautifulSoup from urllib.request import urlopen 上記のライブラリに関する基本情報: - NLTKライブラリ。ライブラリnltkは、英語処理用に記述され、Pythonプログラミング言語で記述されたライブラリとプログラムのコレクションです。
- urllibライブラリ:はPython用のURL処理ライブラリです...このの詳細はこちら
- BeautifulSoupライブラリ:は、HTMLおよびXMLドキュメントからデータを抽出するために使用されるライブラリです。
ステップ2:テキストファイルの内容全体を抽出します。 raw = urlopen( "https://www.w3.org/TR/PNG/iso_8859-1。txt" )。read () したがって、生データは生変数にロードされます。 ステップ#3:次に、データを処理して、以下を使用してraw変数に存在する可能性のあるすべてのhtml /tagsxmlを削除します。 raw1 = BeautifulSoup(raw) td> tr> tbody> table> ステップ4:「raw」変数のテキストを取得します。 raw2 = raw1.get_text() td> tr> tbody> table> 出力: ステップ#5:次に、テキストを単語に分割します。 トークン = nltk.word_tokenize(raw2) 出力: これは、最終テキストを取得する次のステップの前処理として実行されます。 ステップ#6:最後に、最終テキストを取得します。 。 text2 = ` ` 。join(トークン) 出力: < br> 完全なコードは次のとおりです: #ライブラリのインポート import nltk from bs4 import BeautifulSoup from urllib.request import urlopen #テキストの内容全体を抽出しますファイル。 raw = urlopen( " https: //www。w3.org/TR/PNG/iso_8859-1.txt" )。 read() #すべてのhtml/xmlタグを削除します raw1 = BeautifulSoup(raw) raw2 = raw1.get_text() #テキストを単語に分割します。 トークン = nltk.word_tokenize(raw2 ) text2 = `` 。参加(トークン) td> tr> tbody> table>
Shop
Learn programming in R: courses $
Best Python online courses for 2022 $
Best laptop for Fortnite $
Best laptop for Excel $
Best laptop for Solidworks $
Best laptop for Roblox $
Best computer for crypto mining $
Best laptop for Sims 4 $
Latest questions
NUMPYNUMPY
psycopg2: insert multiple rows with one query
12 answers
NUMPYNUMPY
How to convert Nonetype to int or string?
12 answers
NUMPYNUMPY
How to specify multiple return types using type-hints
12 answers
NUMPYNUMPY
Javascript Error: IPython is not defined in JupyterLab
12 answers
Wiki
Python OpenCV | cv2.putText () method
numpy.arctan2 () in Python
Python | os.path.realpath () method
Python OpenCV | cv2.circle () method
Python OpenCV cv2.cvtColor () method
Python - Move item to the end of the list
time.perf_counter () function in Python
Check if one list is a subset of another in Python
Python os.path.join () method
| |