APPLICATION - Handling Website Pagination When Extracting Data

Python 3: Automating Your Job Tasks Superhero Level: Automate Web Scraping with Python 3
6 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€93.41
List Price:  €130.79
You save:  €37.37
£79.90
List Price:  £111.86
You save:  £31.96
CA$137.16
List Price:  CA$192.03
You save:  CA$54.87
A$153.08
List Price:  A$214.33
You save:  A$61.24
S$135.90
List Price:  S$190.27
You save:  S$54.36
HK$781.66
List Price:  HK$1,094.36
You save:  HK$312.69
CHF 91.23
List Price:  CHF 127.73
You save:  CHF 36.49
NOK kr1,107.76
List Price:  NOK kr1,550.92
You save:  NOK kr443.15
DKK kr696.86
List Price:  DKK kr975.63
You save:  DKK kr278.77
NZ$168.60
List Price:  NZ$236.05
You save:  NZ$67.44
د.إ367.24
List Price:  د.إ514.16
You save:  د.إ146.91
৳10,972.60
List Price:  ৳15,362.09
You save:  ৳4,389.48
₹8,346.11
List Price:  ₹11,684.89
You save:  ₹3,338.77
RM475.54
List Price:  RM665.78
You save:  RM190.23
₦123,487.65
List Price:  ₦172,887.65
You save:  ₦49,400
₨27,846.85
List Price:  ₨38,986.71
You save:  ₨11,139.85
฿3,694.58
List Price:  ฿5,172.56
You save:  ฿1,477.98
₺3,236.41
List Price:  ₺4,531.11
You save:  ₺1,294.69
B$519.35
List Price:  B$727.11
You save:  B$207.76
R1,861
List Price:  R2,605.48
You save:  R744.47
Лв182.73
List Price:  Лв255.83
You save:  Лв73.10
₩137,535.16
List Price:  ₩192,554.72
You save:  ₩55,019.56
₪374.71
List Price:  ₪524.61
You save:  ₪149.90
₱5,763.02
List Price:  ₱8,068.46
You save:  ₱2,305.43
¥15,525.12
List Price:  ¥21,735.79
You save:  ¥6,210.67
MX$1,695.96
List Price:  MX$2,374.42
You save:  MX$678.45
QR364.20
List Price:  QR509.89
You save:  QR145.69
P1,420.18
List Price:  P1,988.32
You save:  P568.13
KSh13,372.99
List Price:  KSh18,722.73
You save:  KSh5,349.73
E£4,798.26
List Price:  E£6,717.75
You save:  E£1,919.49
ብር5,719.92
List Price:  ብር8,008.12
You save:  ብር2,288.20
Kz83,641.63
List Price:  Kz117,101.63
You save:  Kz33,460
CLP$96,028.39
List Price:  CLP$134,443.59
You save:  CLP$38,415.20
CN¥723.89
List Price:  CN¥1,013.48
You save:  CN¥289.58
RD$5,819.58
List Price:  RD$8,147.64
You save:  RD$2,328.06
DA13,455.35
List Price:  DA18,838.03
You save:  DA5,382.68
FJ$226.17
List Price:  FJ$316.65
You save:  FJ$90.48
Q777.12
List Price:  Q1,088.01
You save:  Q310.88
GY$20,929.88
List Price:  GY$29,302.67
You save:  GY$8,372.79
ISK kr14,022.59
List Price:  ISK kr19,632.19
You save:  ISK kr5,609.60
DH1,011.06
List Price:  DH1,415.53
You save:  DH404.46
L1,768.33
List Price:  L2,475.73
You save:  L707.40
ден5,750.59
List Price:  ден8,051.06
You save:  ден2,300.46
MOP$805.69
List Price:  MOP$1,128
You save:  MOP$322.31
N$1,863.42
List Price:  N$2,608.86
You save:  N$745.44
C$3,672.13
List Price:  C$5,141.13
You save:  C$1,469
रु13,425.24
List Price:  रु18,795.88
You save:  रु5,370.63
S/376.35
List Price:  S/526.91
You save:  S/150.55
K385.89
List Price:  K540.26
You save:  K154.37
SAR375.01
List Price:  SAR525.03
You save:  SAR150.02
ZK2,677.10
List Price:  ZK3,748.04
You save:  ZK1,070.94
L464.84
List Price:  L650.79
You save:  L185.95
Kč2,347.91
List Price:  Kč3,287.17
You save:  Kč939.26
Ft36,370.18
List Price:  Ft50,919.71
You save:  Ft14,549.52
SEK kr1,092.76
List Price:  SEK kr1,529.91
You save:  SEK kr437.15
ARS$87,614.14
List Price:  ARS$122,663.30
You save:  ARS$35,049.16
Bs690.85
List Price:  Bs967.22
You save:  Bs276.36
COP$390,329.27
List Price:  COP$546,476.60
You save:  COP$156,147.32
₡51,017.42
List Price:  ₡71,426.43
You save:  ₡20,409.01
L2,476.75
List Price:  L3,467.55
You save:  L990.79
₲749,236.02
List Price:  ₲1,048,960.40
You save:  ₲299,724.38
$U3,833.15
List Price:  $U5,366.56
You save:  $U1,533.41
zł404.64
List Price:  zł566.52
You save:  zł161.87
Already have an account? Log In

Transcript

Okay, great to see you in this lecture where we are going to upgrade the application we've just built in the previous video. Actually, I'm going to have this new code in a separate file, web scraper underscore pagination dot p y in the same folder. And I'm only going to highlight the differences as compared to the code you've seen earlier. Basically, this new application is going to perform the exact same tasks, meaning extracting the name, link and price of each of the 21 products from a given test website and save them to an Excel spreadsheet. The new thing here is that this time, we don't have all the products listed on a single page. Instead, we have pagination enabled on the website, meaning there are multiple pages in this case, four of them that contain our products, and our web scrapers should be able to handle such a scenario for the purpose.

Of this lecture, I'm going to use another link from the same website. This is the link right here, you can find this link attached to this video, as well as in the notebook that follows. So before seeing and testing the code for our new application version, let's take a look at how the information is structured. This time, we have the first six products listed on page one, then the next six products on page two, then yet another batch of six tablets listed on page three. And finally, the last three products residing on page four. So this means that our application will have to automatically iterate over all these four pages and extract the product information from each page, as we already did in the previous video.

To enable this iterating behavior, let's try and find an identifier interlink of each page that will uniquely reference that particular page. Going back to the original link As you can see it on the screen right now, we don't see any specific identifier. However, as soon as you click on each page number, let's say page number two, the link changes accordingly. And this text gets appended to the initial link. So we have question mark page equals two. And this happens for page three, and page four as well.

So the first thing we should do is define the common part of this link in our code. So this should be this part right here, up to the equal sign, including the equal sign. So that's exactly what I did here in the application using a variable called link. After importing the modules we need Of course, Next I have created an empty list. This one right here called products that will eventually hold all the products extracted from all the pages. Now in order to extract all the products listed on Each of the four pages, we need to use the unique link of each page inside the parentheses of the request dot get method.

Therefore, we have to iterate over the four pages using a basic for loop and the range function. As you can see it right here, where range of one comma five equates to 123, and four, which are our page numbers, right. So for each page in this range, we're getting that page by concatenating, the string referenced by the link variable with the corresponding page number, which it is, by the way converted from integer to string, otherwise, we wouldn't be able to compose the necessary link. Then we are just passing the string obtained as a result of the concatenation to the get method from within the requests module. Next, as we iterate over each page, we are also loading and parsing the content of that page. Then we have to identify the div tags corresponding to the products that are listed on that particular page.

This is done the same way as we did in the previous lecture using the Find all method and the correct class value. Finally, inside the same for loop, we have to write another for loop that will iterate through all the products that have been identified on each page, and append each product to the general list of products up here using the append method. And this process is performed for each of the four pages. As soon as the list of products is complete, meaning all four pages have been scanned and all 21 products have been saved to the products list. Then the rest of the code is exactly the same as in the previous video, performing the same tasks, meaning extracting the name, link and price for each product to three different lists. Then zipping the list together in a list of stocks.

Building the panda's data frame. And finally writing the data frame to the Excel file. Now it's time to test our new application version to see if the iteration through all four pages is done correctly. And if all the data is indeed saved to the Excel file, I'm going to use the windows cmd. Again, and run the script. Python D web scraping web scraper underscore pagination dot p y.

This would be the second Python script in my folder. And we have web data successfully written to excel quitting the program. So no exceptions have been raised. Let's check the folder as well. And there's our products underscore pagination dot XLS. x file.

Let's open it and success. We have all 21 products saved to the Excel spreadsheet along with their names, links and prices. Basically the result is identical to the one we got in The previous video, only that this time we scraped multiple web pages instead of a single page. Feel free to check out the notebook following this video and download the Python script attached to that notebook to save the upgraded version of our web scrapping application. So I hope you enjoyed this section on web scrapping with Python. And I will see you soon bye

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.