> 文章列表 > 服务器上使用python调用Wikipedia API

服务器上使用python调用Wikipedia API

服务器上使用python调用Wikipedia API

文章目录

  • 前言
  • 一、Wikipedia API
    • 1.安装Wikipedia
    • 2.调用API得到文章摘要
    • 3.调用API更改语言模式
    • 4.调用API搜索内容得到文章标题
    • 5.调用API得到文章URL
    • 6.调用API得到文章页面
      • 6.1 得到页面标题
      • 6.2 得到完整文章
      • 6.3 获取图片
  • 二、设置代理

前言

在使用深度学习的过程中时常需要使用Wikipedia的数据,有时候需要实时访问Wikipedia API,而在中国大陆可能无法直接访问,需要使用代理进行访问。


一、Wikipedia API

官方说明文档

1.安装Wikipedia

命令行中输入

pip install wikipedia

2.调用API得到文章摘要

# To Get the Summary of an Article
print(wikipedia.summary("Trump", sentences=1))

其中参数sentences=1表示返回的句子数目
输出:

The trumpet is a brass instrument commonly used in classical and jazz ensembles.

3.调用API更改语言模式

# Change the Language of the Article
wikipedia.set_lang("fr")
print(wikipedia.summary("Trump", sentences=1))

输出:

Donald Trump (/ˈdɑnəld tɹʌmp/), né le 14 juin 1946 à New York, est un homme d'affaires milliardaire, animateur de télévision et homme d'État américain.

4.调用API搜索内容得到文章标题

# Search to Get the Titles of the Articles
print(wikipedia.search("Trump"))

输出:

['Donald Trump', 'Trump', 'Ivana Trump', 'Melania Trump', 'Ivanka Trump', 'Barron Trump', 'Trump Tower', 'Fred Trump', 'Tiffany Trump', 'Présidence de Donald Trump']

5.调用API得到文章URL

# Get the URL of the Article
page = wikipedia.page("Trump")
print(page.url)

输出:

https://fr.wikipedia.org/wiki/Donald_Trump

6.调用API得到文章页面

page = wikipedia.page("Trump")

6.1 得到页面标题

# Get the Title of the Article
print(page.title)

输出:

Donald Trump

6.2 得到完整文章

# Get Complete Article
print(page.content)

输出(截取了部分内容):

The trumpet is a brass instrument commonly used in classical and jazz ensembles. The trumpet group ranges from the piccolo trumpet—with the highest register in the brass family—to the bass trumpet, pitched one octave below the standard B♭ or C trumpet.
Trumpet-like instruments have historically been used as signaling devices in battle or hunting, with examples dating back to at least 1500 BC. They began to be used as musical instruments only in the late 14th or early 15th century. Trumpets are used in art music styles, for instance in orchestras, concert bands, and jazz ensembles, as well as in popular music. They are played by blowing air through nearly closed lips (called the player's embouchure), producing a "buzzing" sound that starts a standing wave vibration in the air column inside the instrument. Since the late 15th century, trumpets have primarily been constructed of brass tubing, usually bent twice into a rounded rectangular shape.
There are many distinct types of trumpet, with the most common being pitched in B♭ (a transposing instrument), having a tubing length of about 1.48 m (4 ft 10 in). Early trumpets did not provide means to change the length of tubing, whereas modern instruments generally have three (or sometimes four) valves in order to change their pitch. Most trumpets have valves of the piston type, while some have the rotary type. The use of rotary-valved trumpets is more common in orchestral settings (especially in German and German-style orchestras), although this practice varies by country. A musician who plays the trumpet is called a trumpet player or trumpeter.== Etymology ==The English word "trumpet" was first used in the late 14th century. The word came from Old French "trompette," which is a diminutive of trompe. The word "trump," meaning "trumpet," was first used in English in 1300. The word comes from Old French trompe "long, tube-like musical wind instrument" (12c.), cognate with Provençal tromba, Italian tromba, all probably from a Germanic source (compare Old High German trumpa, Old Norse trumba "trumpet"), of imitative origin."== History ==The earliest trumpets date back to 1500 BC and earlier. The bronze and silver Tutankhamun's trumpets from his grave in Egypt, bronze lurs from Scandinavia, and metal trumpets from China date back to this period. Trumpets from the Oxus civilization (3rd millennium BC) of Central Asia have decorated swellings in the middle, yet are made out of one sheet of metal, which is considered a technical wonder for its time.The Shofar, made from a ram horn and the Hatzotzeroth, made of metal, are both mentioned in the Bible. They were said to have been played in Solomon's Temple around 3,000 years ago. They were said to be used to blow down the walls of Jericho. They are still used on certain religious days. 
The Salpinx was a straight trumpet 62 inches (1,600 mm) long, made of bone or bronze. Salpinx contests were a part of the original Olympic Games.The Moche people of ancient Peru depicted trumpets in their art going back to AD 300. The earliest trumpets were signaling instruments used for military or religious purposes, rather than music in the modern sense; and the modern bugle continues this signaling tradition.Improvements to instrument design and metal making in the late Middle Ages and Renaissance led to an increased usefulness of the trumpet as a musical instrument. The natural trumpets of this era consisted of a single coiled tube without valves and therefore could only produce the notes of a single overtone series. Changing keys required the player to change crooks of the instrument.  The development of the upper, "clarino" register by specialist trumpeters—notably Cesare Bendinelli—would lend itself well to the Baroque era, also known as the "Golden Age of the natural trumpet." During this period, a vast body of music was written for virtuoso trumpeters. The art was revived in the mid-20th century and natural trumpet playing is again a thriving art around the world. Many modern players in Germany and the UK who perform Baroque music use a version of the natural trumpet fitted with three or four vent holes to aid in correcting out-of-tune notes in the harmonic series.The melody-dominated homophony of the classical and romantic periods relegated the trumpet to a secondary role by most major composers owing to the limitations of the natural trumpet. Berlioz wrote in 1844:Notwithstanding the real loftiness and distinguished nature of its quality of tone, there are few instruments that have been more degraded (than the trumpet). Down to Beethoven and Weber, every composer – not excepting Mozart – persisted in confining it to the unworthy function of filling up, or in causing it to sound two or three commonplace rhythmical formulae.== Construction ==The trumpet is constructed of brass tubing bent twice into a rounded oblong shape. As with all brass instruments, sound is produced by blowing air through closed lips, producing a "buzzing" sound into the mouthpiece and starting a standing wave vibration in the air column inside the trumpet. The player can select the pitch from a range of overtones or harmonics by changing the lip aperture and tension (known as the embouchure).
The mouthpiece has a circular rim, which provides a comfortable environment for the lips' vibration. Directly behind the rim is the cup, which channels the air into a much smaller opening (the back bore or shank) that tapers out slightly to match the diameter of the trumpet's lead pipe. The dimensions of these parts of the mouthpiece affect the timbre or quality of sound, the ease of playability, and player comfort. Generally, the wider and deeper the cup, the darker the sound and timbre.Modern trumpets have three (or, infrequently, four) piston valves, each of which increases the length of tubing when engaged, thereby lowering the pitch. The first valve lowers the instrument's pitch by a whole step (two semitones), the second valve by a half step (one semitone), and the third valve by one and a half steps (three semitones). Having three valves provides eight possible valve combinations (including "none"), but only seven different tubing lengths, because the third valve alone gives essentially the same tubing length as the 1–2 combination. (In practice there is often a deliberately designed slight difference between "1–2" and "3", and in that case trumpet players will select the alternative that gives the best tuning for the particular note being played.) When a fourth valve is present, as with some piccolo trumpets, it usually lowers the pitch a perfect fourth (five semitones). Used singly and in combination these valves make the instrument fully chromatic, i.e., able to play all twelve pitches of classical music. For more information about the different types of valves, see Brass instrument valves.
The pitch of the trumpet can be raised or lowered by the use of the tuning slide. Pulling the slide out lowers the pitch; pushing the slide in raises it. To overcome the problems of intonation and reduce the use of the slide, Renold Schilke designed the tuning-bell trumpet. Removing the usual brace between the bell and a valve body allows the use of a sliding bell; the player may then tune the horn with the bell while leaving the slide pushed in, or nearly so, thereby improving intonation and overall response.A trumpet becomes a closed tube when the player presses it to the lips; therefore, the instrument only naturally produces every other overtone of the harmonic series. The shape of the bell makes the missing overtones audible. Most notes in the series are slightly out of tune and modern trumpets have slide mechanisms for the first and third valves with which the player can compensate by throwing (extending) or retracting one or both slides, using the left thumb and ring finger for the first and third valve slides respectively.
Trumpets can be constructed from other materials, including plastic.== Types ==The most common type is the B♭ trumpet, but A, C, D, E♭, E, low F, and G trumpets are also available. The C trumpet is most common in American orchestral playing, where it is used alongside the B♭ trumpet. Orchestral trumpet players are adept at transposing music at sight, frequently playing music written for the A, B♭, D, E♭, E, or F trumpet on the C trumpet or B♭ trumpet.The smallest trumpets are referred to as piccolo trumpets. The most common models are built to play in both B♭ and A, with separate leadpipes for each key. The tubing in the B♭ piccolo trumpet is one-half the length of that in a standard B♭ trumpet. Piccolo trumpets in G, F and C are also manufactured, but are less common. Many players use a smaller mouthpiece on the piccolo trumpet, which requires a different sound production technique from the B♭ trumpet and can limit endurance. Almost all piccolo trumpets have four valves instead of three—the fourth valve usually lowers the pitch by a fourth, making some lower notes accessible and creating alternate fingerings for certain trills. Maurice André, Håkan Hardenberger, David Mason, and Wynton Marsalis are some well-known trumpet players known for their virtuosity on the piccolo trumpet.Trumpets pitched in the key of low G are also called sopranos, or soprano bugles, after their adaptation from military bugles. Traditionally used in drum and bugle corps, sopranos employ either rotary valves or piston valves.
The bass trumpet is at the same pitch as a trombone and is usually played by a trombone player, although its music is written in treble clef. Most bass trumpets are pitched in either C or B♭. The C bass trumpet sounds an octave lower than written, and the  B♭ bass sounds a major ninth (B♭) lower, making them both transposing instruments.
The historical slide trumpet was probably first developed in the late 14th century for use in alta cappella wind bands. Deriving from early straight trumpets, the Renaissance slide trumpet was essentially a natural trumpet with a sliding leadpipe. This single slide was awkward, as the entire instrument moved, and the range of the slide was probably no more than a major third. Originals were probably pitched in D, to fit with shawms in D and G, probably at a typical pitch standard near A=466 Hz. No known instruments from this period survive, so the details—and even the existence—of a Renaissance slide trumpet is a matter of debate among scholars. While there is documentation (written and artistic) of its existence, there is also conjecture that its slide would have been impractical.Some slide trumpet designs saw use in England in the 18th century.The pocket trumpet is a compact B♭ trumpet. The bell is usually smaller than a standard trumpet bell and the tubing is more tightly wound to reduce the instrument size without reducing the total tube length. Its design is not standardized, and the quality of various models varies greatly. It can have a unique warm sound and voice-like articulation. Since many pocket trumpet models suffer from poor design as well as poor manufacturing, the intonation, tone color and dynamic range of such instruments are severely hindered. Professional-standard instruments are, however, available. While they are not a substitute for the full-sized instrument, they can be useful in certain contexts. The jazz musician Don Cherry was renowned for his playing of the pocket instrument.
The herald trumpet has an elongated bell extending far in front of the player, allowing a standard length of tubing from which a flag may be hung; the instrument is mostly used for ceremonial events such as parades and fanfares.
David Monette designed the flumpet in 1989 for jazz musician Art Farmer. It is a hybrid of a trumpet and a flugelhorn, pitched in B♭ and using three piston valves.Other variations include rotary-valve, or German, trumpets (which are commonly used in professional German and Austrian orchestras), alto and Baroque trumpets, and the Vienna valve trumpet (primarily used in Viennese brass ensembles and orchestras such as the Vienna Philharmonic and Mnozil Brass).
The trumpet is often confused with its close relative the cornet, which has a more conical tubing shape compared to the trumpet's more cylindrical tube. This, along with additional bends in the cornet's tubing, gives the cornet a slightly mellower tone, but the instruments are otherwise nearly identical. They have the same length of tubing and, therefore, the same pitch, so music written for cornet and trumpet is interchangeable. Another relative, the flugelhorn, has tubing that is even more conical than that of the cornet, and an even richer tone. It is sometimes augmented with a fourth valve to improve the intonation of some lower notes.

6.3 获取图片

# Get the Images Included in Article 
print(page.images[0])

输出:

https://upload.wikimedia.org/wikipedia/commons/e/ea/Baroque_repro_trumpet.jpeg

二、设置代理

通常来说,在服务器上直接调用Wikipedia API或出现连接超时的报错

requests.exceptions.ConnectionError: HTTPSConnectionPool(host='en.wikipedia.org', port=443): Max retries exceeded with url: /wiki/Web_scraping (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f238a1c7a58>: Failed to establish a new connection: [Errno 110] Connection timed out'))

所以,需要设置代理来进行访问。

  1. 本地设置代理:
    对于Windows系统,“设置”-》“网络和Internet”-》“代理”
    记住此时的代理ip和端口
  2. 打开代理软件(常说的“梯子”或“VPN”),开全局模式
  3. 在python代码中加入以下命令(根据自己代理的ip和端口进行设置):
import os
os.environ["http_proxy"] = "http://127.0.0.1:1080"
os.environ["https_proxy"] = "http://127.0.0.1:1080"

注意,不要频繁使用代理访问Wikipedia API,否则会被拒绝访问,因此,在连续两次访问之间需要设置间隔时间以防止频繁访问:

time.sleep(6)  # 等待6秒