Web Scraping
Study guide: DISC-DA
Web Scraping is the automated extraction of data from web pages: a program requests each page, parses its HTML and pulls out the values it needs into a structured table. Insurers use online sources to add external data such as building characteristics, business descriptions and public records to their own policy and claim data.
Scraping is one of two ways to collect data from online sources. The other is an application programming interface (API), which returns the data already structured under the provider's terms.
| API | Web scraping | |
|---|---|---|
| What it returns | Structured fields, such as JSON | Whatever the page displays |
| Field definitions | Documented | None |
| Terms of access | Granted by the provider, with stated terms and rate limits | Set by the site's terms of use, which may forbid it |
| When the site changes its layout | Unaffected | The scraper breaks |
Where an API or a licensed data feed exists, it is usually the better source.
Usable share of records
If a page is found for a share m of the insurer's records (the match rate), and a field is present on a share c of the pages found (its completeness), the share of records that end up with a usable value is
u = m \times c
Scraped values are self-reported by whoever wrote the page, can be out of date, and cover only the properties or businesses that happen to be listed. The listed records are rarely a random sample of the book, so a low match rate can bias a model. The values should be validated against an independent source, such as assessment records, before use.
How many policies get each field? An insurer wants year built and square footage for 12,000 homeowners policies. It plans to match each insured address to a public real estate listing and scrape the two fields. A pilot finds a listing for 65% of addresses. On the listings found, year built is present 92% of the time and square footage 85% of the time.
Answer
- The usable share for year built is
0.65 \times 0.92 = 0.598, so about 7,176 policies get a year built.- The usable share for square footage is
0.65 \times 0.85 = 0.5525, so about 6,630 policies get a square footage.- Four in ten policies get no value, and listed homes tend to be the ones recently sold, so the insurer should compare the matched and unmatched policies before using the fields.
Parsing
A scraper locates each value by the page's structure, such as an HTML tag, a class name or a table position, and then cleans it into a typed field. Free text like "Built 1978, renovated 2015" has to be turned into a year built and a renovation year, and units such as square feet and square metres have to be standardized.
What values of
year_built,renovation_yearandsq_ftshould a scraper record from these listings? One listing page shows<li class="facts">Built: 1978 (renovated 2015)</li>and<li class="facts">Size: 1,850 sq ft</li>. Another listing on the same site shows<li class="facts">Size: 172 m2</li>.Answer
- Once the comma and the units are stripped, the first listing gives
year_built= 1978,renovation_year= 2015 andsq_ft= 1,850.- The second listing gives an area in square metres, which converts at 10.7639 square feet per square metre:
172 \times 10.7639 = 1{,}851square feet.- The parser must read the unit, not just the number, or the second home would be recorded as 172 square feet and fail a range check.
- Both values are what the seller wrote, so they are self-reported and unverified.
Terms of use and legal limits
A site's terms of use may forbid automated collection, and its robots.txt file sets allow and disallow rules for the pages crawlers may visit (the Robots Exclusion Protocol, RFC 9309). A crawl-delay line asking crawlers to slow down is a non-standard extension that some crawlers honour. Waiting between requests is good practice either way. A crawl that waits d seconds between requests takes, in seconds, a time of
T_{\text{crawl}} = N_{\text{requests}} \times d
N_{\text{requests}}is the number of requests the crawl makes.
Copyright, contract, computer misuse and privacy laws can all apply, especially where the pages hold personal information. In the United States, R for Data Science gives a general principle that public, non-personal, factual data is likely to be lower risk. Courts have generally not held browse-wrap terms of service binding without some explicit assent, such as creating an account, and copyright does not protect facts. Personal information is the main exception, and the position differs elsewhere, as in Europe. Collecting the data legally does not settle whether using it for underwriting or pricing is permitted, so legal and compliance review comes before the crawl.
A site asks for 5 seconds between requests, and each of 12,000 addresses needs 2 requests. How long does the crawl take?
- The crawl makes
12{,}000 \times 2 = 24{,}000requests, a search and a listing page for each address.- It takes
24{,}000 \times 5 = 120{,}000seconds, about 33.3 hours.
Ethical limits
Collecting data people did not expect an insurer to use raises the consent and transparency concerns of data ethics, and a scraped variable can act as a proxy for a protected characteristic. The responsible approach is to collect only the fields the model needs and to document their source. Scraped data are loaded through the same pipeline as other sources, often into a data mart for the project, and pass through the same validation checks.
Treating public data as free to use Data visible on a public page can still be restricted, by terms of service the collector has agreed to, by copyright in content that is more than facts, or by privacy law where it is personal. Public, non-personal, factual data is the lower-risk case, and being able to collect data does not settle whether it may be used for pricing.