- Haskell Data Analysis Cookbook
- Nishant Shukla
- 284字
- 2021-12-08 12:43:33
Understanding how to perform HTTP GET requests
One of the most resourceful places to find good data is online. GET requests are common methods of communicating with an HTTP web server. In this recipe, we will grab all the links from a Wikipedia article and print them to the terminal. To easily grab all the links, we will use a helpful library called HandsomeSoup
, which lets us easily manipulate and traverse a webpage through CSS selectors.
Getting ready
We will be collecting all links from a Wikipedia web page. Make sure to have an Internet connection before running this recipe.
Install the HandsomeSoup
CSS selector package, and also install the HXT library if it is not already installed. To do this, use the following commands:
$ cabal install HandsomeSoup $ cabal install hxt
How to do it...
- This recipe requires
hxt
for parsing HTML and requiresHandsomeSoup
for the easy-to-use CSS selectors, as shown in the following code snippet:import Text.XML.HXT.Core import Text.HandsomeSoup
- Define and implement
main
as follows:main :: IO () main = do
- Pass in the URL as a string to HandsomeSoup's
fromUrl
function:let doc = fromUrl "http://en.wikipedia.org/wiki/Narwhal"
- Select all links within the
bodyContent
field of the Wikipedia page as follows:links <- runX $ doc >>> css "#bodyContent a" ! "href" print links
How it works…
The HandsomeSoup
package allows easy CSS selectors. In this recipe, we run the #bodyContent a
selector on a Wikipedia article web page. This finds all link tags that are descendants of an element with the bodyContent
ID.
See also…
Another common way to obtain data online is through POST requests. To find out more, refer to the Learning how to perform HTTP POST requests recipe.
- 少兒人工智能趣味入門:Scratch 3.0動畫與游戲編程
- Visual Studio 2012 Cookbook
- LabVIEW入門與實戰開發100例
- AWS Serverless架構:使用AWS從傳統部署方式向Serverless架構遷移
- AngularJS Web Application Development Blueprints
- Java技術手冊(原書第7版)
- Keras深度學習實戰
- Visual Studio 2015高級編程(第6版)
- R用戶Python學習指南:數據科學方法
- Android嵌入式系統程序開發:基于Cortex-A8(第2版)
- Visual Studio Code 權威指南
- 大學計算機基礎
- Sails.js Essentials
- Docker:容器與容器云(第2版)
- Android技術內幕(系統卷)