
Több információ található az interneten, mint amennyit bármely ember képes felvenni egy életen át. Amire nincs szükséged, nem ezekhez az információkhoz kell hozzáférni, hanem skálázható módon kell gyűjteni, rendszerezni és elemezni azokat.
Szüksége van webes kaparásra.
A webes kaparás automatikusan kivonja az adatokat és olyan formátumban mutatja be, amelyet könnyen értelmezhet. Ebben az oktatóanyagban a pénzügyi piacon történő alkalmazásaira összpontosítunk, de a webes kaparás sokféle helyzetben alkalmazható.
Ha lelkes befektető vagy, a napi záró árak megszerzése fájdalmat okozhat, különösen, ha a szükséges információk több weboldalon megtalálhatók. Megkönnyítjük az adatok kinyerését egy webkaparó felépítésével, amely automatikusan lekéri a részvényindexeket az internetről.

Elkezdeni
A Python-t fogjuk használni kaparós nyelvként, egy egyszerű és hatékony könyvtárral, a BeautifulSoup-tal együtt.
- Mac felhasználók számára a Python előre telepítve van az OS X rendszerbe. Nyissa meg a Terminált és írja be
python --version
. Látnia kell, hogy a python verziója 2.7.x. - Windows felhasználók számára telepítse a Python-t a hivatalos webhelyen keresztül.
Ezután meg kell szereznünk a BeautifulSoup könyvtárat pip
, a Python csomagkezelő eszközének használatával.
Írja be a terminálba:
easy_install pip pip install BeautifulSoup4
Megjegyzés : Ha nem sikerül végrehajtani a fenti parancssort, próbálja meg hozzáadni sudo
minden sor elé.
Az alapok
Mielőtt elkezdenénk a kódba ugrani, ismerjük meg a HTML alapjait és a kaparás néhány szabályát.
HTML címkék
Ha már ért a HTML-címkékhez, hagyja ki nyugodtan ezt a részt.
First Scraping
Hello World
Ez egy HTML weboldal alapvető szintaxisa. Mindegyik blokkot szolgál a weboldalon belül:
1 .: A HTML dokumentumoknak típusdeklarációval kell kezdődniük.
2. A HTML dokumentum és között található
.
3. A HTML dokumentum meta és script deklarációja és között van
.
4. A látható része a HTML dokumentum között és
címkéket.
5. A címsorokat a

Original text
keresztül
címkék.
címkék.6. A bekezdések a
Other useful tags include
for hyperlinks,
for tables,
for table rows, and
táblázat oszlopaihoz. Ezenkívül a HTML-címkék néha tartalmaznak A HTML-címkékről, azonosítóról és osztályról további információt a W3Schools oktatóanyagok című részben talál. Kaparási szabályok
Az oldal ellenőrzéseVegyünk példaként egy oldalt a Bloomberg Quote webhelyről. A tőzsdét követőként szeretnénk erről az oldalról megkapni az index nevét (S&P 500) és annak árát. Először kattintson a jobb gombbal, és nyissa meg a böngésző ellenőrzőjét a weboldal ellenőrzéséhez. ![]() Próbálja meg az egérmutatót az áron mozgatni, és látnia kell egy kék négyzetet, amely körülveszi. Ha rákattint, a kapcsolódó HTML-t kiválasztja a böngésző konzol. ![]() From the result, we can see that the price is inside a few levels of HTML tags, which is Similarly, if you hover and click the name “S&P 500 Index”, it is inside .![]() Now we know the unique location of our data with the help of Jump into the CodeNow that we know where our data is, we can start coding our web scraper. Open your text editor now! First, we need to import all the libraries that we are going to use.
Next, declare a variable for the url of the page.
Then, make use of the Python urllib2 to get the HTML page of the url declared.
Finally, parse the page into BeautifulSoup format so we can use BeautifulSoup to work on it.
Now we have a variable, Remember the unique layers of our data? BeautifulSoup can help us get into these layers and extract the content with
After we have the tag, we can get the data by getting its
Similarly, we can get the price too.
When you run the program, you should be able to see that it prints out the current price of the S&P 500 Index. ![]() Export to Excel CSVNow that we have the data, it is time to save it. The Excel Comma Separated Format is a nice choice. It can be opened in Excel so you can see the data and process it easily. But first, we have to import the Python csv module and the datetime module to get the record date. Insert these lines to your code in the import section.
At the bottom of your code, add the code for writing data to a csv file.
Now if you run your program, you should able to export an ![]() So if you run this program everyday, you will be able to easily get the S&P 500 Index price without rummaging through the website! Going Further (Advanced uses)Multiple Indices So scraping one index is not enough for you, right? We can try to extract multiple indices at the same time. First, modify the
Then we change the data extraction code into a
Also, modify the saving section to save data row by row.
Rerun the program and you should be able to extract two indices at the same time! Advanced Scraping TechniquesBeautifulSoup is simple and great for small-scale web scraping. But if you are interested in scraping data at a larger scale, you should consider using these other alternatives:
Adopt the DRY Method![]() DRY stands for “Don’t Repeat Yourself”, try to automate your everyday tasks like this person. Some other fun projects to consider might be keeping track of your Facebook friends’ active time (with their consent of course), or grabbing a list of topics in a forum and trying out natural language processing (which is a hot topic for Artificial Intelligence right now)! If you have any questions, please feel free to leave a comment below. References //www.gregreda.com/2013/03/03/web-scraping-101-with-python/ //www.analyticsvidhya.com/blog/2015/10/beginner-guide-web-scraping-beautiful-soup-python/ This article was originally published on Altitude Labs’ blog and was written by our software engineer, Leonard Mok. Altitude Labs is a software agency that specializes in personalized, mobile-first React apps. |