While Web Scraping As We Know It Today Has Existed For Well Over A Decade Now
While Web Scraping As We Know It Today Has Existed For Well Over A Decade Now
relevance has never been more profound. The ability to make it useful and value-
generating is currently applicable to almost all slices of business and beyond.
Here are some of the factors that provide evidence for web scraping’s ever-increasing
necessity in businesses:
The main question is how will the already successful data scraping techniques be
affected by machine learning?
Such features are rather convenient since they allow for a more hands-off approach,
i.e., instead of hand-coding various software routines or instructions, this will enable you
to achieve a specific given task primarily through a machine with little interference from
a developer.
The next question is how the benefits of ML translate to real-life scenarios? To answer
this, let’s look at some use cases.
Customer service: chatbots are starting to replace human agents, with FAQs often
being answered without a human reply. The Virtual Agents of Slack and Facebook are
prime examples of this.
Web Unblocker: an AI-powered proxy solution, allows for block-free data gathering.
This proxy solution contains ML-driven proxy management as well as ML-powered
response recognition both of which ensure an effortless data collection process.
Computer Vision: the AI, and as such, ML technology, allows for the extraction of
meaningful information taken solely from visual data, upon which recognition tasks can
be achieved. A prime example of this use is ML integration within self-driving cars.
Stock trading: enables automated trading that optimizes stock portfolios by potentially
making millions of automated trades per day.
Primarily, web scraping in ML is centered around the core problem of gathering quality
data.
While the internal information gathered on day-to-day business can provide valuable
insights, such data is insufficient. Therefore, gathering from external sources is
essential, although a more complex task. Inaccuracy/poor data quality becomes a
severe concern when scraping, and a final clean-up step must always be included
within any scraping project, though this will be discussed in greater detail later on in this
guide.
Thankfully, numerous websites provide such data, and it’s usually, conveniently,
presented in a table. Typically, you’ll see the HTML code that renders these tables,
such as the following image.
With that in mind, let’s get started with the first step of web scraping.
Project requirements
In this blog, we’ll be working with Python 3.9. However, this code will work with Python
3.7 and 3.8 as well.
There are two sets of requirements for this project. Firstly, libraries for web scraping and
secondly, libraries for machine learning.
For web scraping, we’ll need Requests-HTML and BeautifulSoup4. Install these from
the terminal as follows:
For web scraping, we only need Requests-HTML. The primary reason is that Requests-
HTML is a powerful library that can handle all our web scraping tasks, such as
extracting the HTML code from websites and parsing this code into Python objects.
Further benefits come from the library’s ability to function as an HTML parser, meaning
collecting data and labeling can be performed using the same library.
Next, we use Pandas for loading the data in a DataFrame for further processing.
In the next cell, create a session and get the response from your target URL.
url = 'http://your-target-url'
session = HTMLSession()
r = session.get(url)
After this, use XPath to select the desired data. It’ll be easier if each row is represented
as a dictionary where the key is the column name. All these dictionaries can then be
added to a list.
rows = r.html.xpath('//table/tbody/tr')
symbol = 'AAPL'
data = []
for row in rows:
if len(row.xpath('.//td')) < 7:
continue
data.append({
'Symbol':symbol,
'Date':row.xpath('.//td[1]/span/text()')[0],
'Open':row.xpath('.//td[2]/span/text()')[0],
'High':row.xpath('.//td[3]/span/text()')[0],
'Low':row.xpath('.//td[4]/span/text()')[0],
'Close':row.xpath('.//td[5]/span/text()')[0],
'Adj Close':row.xpath('.//td[6]/span/text()')[0],
'Volume':row.xpath('.//td[7]/span/text()')[0]
})
The results of web scraping are being stored in the variable data. To understand why
such actions are taken, we must consider that these variables are a list of dictionaries
that can be easily converted to a data frame. Furthermore, completing the steps
mentioned above will also help to complete the vital step of data labeling.
The provided example’s data frame is not yet ready for the machine learning step. It still
needs additional cleaning.
As evident from the above screen-print, all the columns have data type as object. For
machine learning algorithms, these should be numbers.
Dates can be handled using Pandas.to_datetime. It’ll take a series and convert the
values to datetime. This can then be used as follows:
df['Date'] = pd.to_datetime(df['Date'])
The issue we ran into now is that the other columns were not automatically converted to
numbers because of comma separators.
Thankfully, there are multiple ways to handle this. The easiest one is to remove the
comma by calling str.replace() function. The astype function can also be called in
the same line which will then return a float.
Finally, if there are any None or NaN values, these can be deleted by calling
the dropna().
df.dropna(inplace=True)
As the last step, set the Date column as the index and preview the data frame.
df = df.set_index('Date')
df.head()
The data frame is now clean and ready to be sent to the machine learning model.
Next, enter the following lines to plot the Adj Close, which is the adjusted closing
price:
plt.figure(figsize=(15, 6))
df['Adj Close'].plot()
plt.ylabel('Adj Close')
plt.xlabel(None)
plt.title('Closing Price of AAPL')
In this example, the “Adjusted Close” is dependent on the “Close” part. Therefore, we’ll
ignore the Close column and focus on Adj Close.
The features are usually stored in a variable named X and the values that we want to
predict are stored in a variable y.
The next step we have to consider is feature scaling. It’s used to normalize the features,
i.e., the independent variables. Within our example, we can use MinMaxScaler. This
class is part of the preprocessing module of the Sci Kit Learn library.
First, we’ll create an object of this class. Then, we’ll train and transform the values using
the fit_transform method as follows:
The next step is splitting the data we have received into two datasets, test and training.
The example we’re working with today is a time-series data, meaning data that changes
over a time period requires specialized handling. The TimeSeriesSplit function from
SKLearn’s model_selection module will be what we need here.
Continue by creating an instance of the Sequential model and adding two layers. The
first layer will be an LSTM with 32 units while the second will be a Dense layer.
model = Sequential()
model.add(LSTM(32, activation='relu', return_sequences=False))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
y_pred= model.predict(X_test)
Finally, let’s plot the actual values and predicted values with the following:
plt.figure(figsize=(15, 6))
plt.plot(y_test, label='Actual Value')
plt.plot(y_pred, label='Predicted Value')
plt.ylabel('Adjusted Close (Scaled)')
plt.xlabel('Time Scale')
plt.legend()