考えられるいくつかのアプローチを次に示します。あなたに合ったものを使用してください。以下のコード例はすべて、requests
API への HTTP リクエストに使用されます。requests
Pipがpip install requests
あればインストールできます。また、それらはすべてMediawiki APIを使用し、そのうちの 2 つはクエリエンドポイントを使用します。ドキュメントが必要な場合は、これらのリンクに従ってください。
extracts
1. propを使用して、ページ全体またはページ「抽出」のプレーンテキスト表現を API から直接取得します
このアプローチは、TextExtracts 拡張子を持つ MediaWiki サイトでのみ機能することに注意してください。これには特にウィキペディアが含まれますが、たとえばhttp://www.wikia.com/のような小規模な Mediawiki サイトは含まれません。
次のような URL をヒットしたい
https://en.wikipedia.org/w/api.php?action=query&format=json&titles=Bla_Bla_Bla&prop=extracts&exintro&explaintext
それを分解すると、そこには次のパラメーターがあります ( https://www.mediawiki.org/wiki/Extension:TextExtracts#query+extractsで文書化されています):
action=query
、format=json
、およびtitle=Bla_Bla_Bla
は、すべて標準の MediaWiki API パラメーターです。
prop=extracts
TextExtracts 拡張機能を使用させます
exintro
最初のセクション見出しの前のコンテンツへの応答を制限する
explaintext
応答の抽出を HTML ではなくプレーン テキストにします
次に、JSON 応答を解析し、抜粋を抽出します。
>>> import requests
>>> response = requests.get(
... 'https://en.wikipedia.org/w/api.php',
... params={
... 'action': 'query',
... 'format': 'json',
... 'titles': 'Bla Bla Bla',
... 'prop': 'extracts',
... 'exintro': True,
... 'explaintext': True,
... }
... ).json()
>>> page = next(iter(response['query']['pages'].values()))
>>> print(page['extract'])
"Bla Bla Bla" is the title of a song written and recorded by Italian DJ Gigi D'Agostino. It was released in May 1999 as the third single from the album, L'Amour Toujours. It reached number 3 in Austria and number 15 in France. This song can also be heard in an added remixed mashup with L'Amour Toujours (I'll Fly With You) in its US radio version.
2.parse
エンドポイントを使用してページの完全な HTML を取得し、解析して最初の段落を抽出する
MediaWiki には、https://en.wikipedia.org/w/api.php?action=parse&page=Bla_Bla_Blaのような URL でヒットして、ページの HTML を取得できるparse
エンドポイントがあります。次に、 lxmlなどの HTML パーサーで解析して(最初に でインストールします)、最初の段落を抽出します。pip install lxml
例えば:
>>> import requests
>>> from lxml import html
>>> response = requests.get(
... 'https://en.wikipedia.org/w/api.php',
... params={
... 'action': 'parse',
... 'page': 'Bla Bla Bla',
... 'format': 'json',
... }
... ).json()
>>> raw_html = response['parse']['text']['*']
>>> document = html.document_fromstring(raw_html)
>>> first_p = document.xpath('//p')[0]
>>> intro_text = first_p.text_content()
>>> print(intro_text)
"Bla Bla Bla" is the title of a song written and recorded by Italian DJ Gigi D'Agostino. It was released in May 1999 as the third single from the album, L'Amour Toujours. It reached number 3 in Austria and number 15 in France. This song can also be heard in an added remixed mashup with L'Amour Toujours (I'll Fly With You) in its US radio version.
3. 自分でウィキテキストを解析する
query
API を使用してページのウィキテキストを取得し、 を使用して解析しmwparserfromhell
(最初に を使用してインストールしますpip install mwparserfromhell
)、次に を使用して人間が読めるテキストに縮小できますstrip_code
。strip_code
執筆時点では完全には機能していませんが (以下の例で明確に示されています)、改善されることを願っています。
>>> import requests
>>> import mwparserfromhell
>>> response = requests.get(
... 'https://en.wikipedia.org/w/api.php',
... params={
... 'action': 'query',
... 'format': 'json',
... 'titles': 'Bla Bla Bla',
... 'prop': 'revisions',
... 'rvprop': 'content',
... }
... ).json()
>>> page = next(iter(response['query']['pages'].values()))
>>> wikicode = page['revisions'][0]['*']
>>> parsed_wikicode = mwparserfromhell.parse(wikicode)
>>> print(parsed_wikicode.strip_code())
{{dablink|For Ke$ha's song, see Blah Blah Blah (song). For other uses, see Blah (disambiguation)}}
"Bla Bla Bla" is the title of a song written and recorded by Italian DJ Gigi D'Agostino. It was released in May 1999 as the third single from the album, L'Amour Toujours. It reached number 3 in Austria and number 15 in France. This song can also be heard in an added remixed mashup with L'Amour Toujours (I'll Fly With You) in its US radio version.
Background and writing
He described this song as "a piece I wrote thinking of all the people who talk and talk without saying anything". The prominent but nonsensical vocal samples are taken from UK band Stretch's song "Why Did You Do It"''.
Music video
The song also featured a popular music video in the style of La Linea. The music video shows a man with a floating head and no arms walking toward what appears to be a shark that multiplies itself and can change direction. This style was also used in "The Riddle", another song by Gigi D'Agostino, originally from British singer Nik Kershaw.
Chart performance
Chart (1999-00)PeakpositionIreland (IRMA)Search for Irish peaks23
References
External links
Category:1999 singles
Category:Gigi D'Agostino songs
Category:1999 songs
Category:ZYX Music singles
Category:Songs written by Gigi D'Agostino