0% found this document useful (0 votes)
63 views5 pages

Amazon

The document describes code for scraping product information from Amazon.com using Selenium and Beautiful Soup. It imports the necessary libraries, defines a function to generate URLs based on search keywords, and scrapes the page source after navigating to a laptop charger search URL. It finds 19 search results, examines the first result element, and extracts the product title, rating, and number of reviews.

Uploaded by

Henoc GAKPETO
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views5 pages

Amazon

The document describes code for scraping product information from Amazon.com using Selenium and Beautiful Soup. It imports the necessary libraries, defines a function to generate URLs based on search keywords, and scrapes the page source after navigating to a laptop charger search URL. It finds 19 search results, examines the first result element, and extracts the product title, rating, and number of reviews.

Uploaded by

Henoc GAKPETO
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Amazon

October 26, 2023

[1]: import selenium


from selenium import webdriver

[2]: pip install chromedriver_binary==118.0.5993.70.0

Requirement already satisfied: chromedriver_binary==118.0.5993.70.0 in


/Users/wansouwe/opt/anaconda3/lib/python3.9/site-packages (118.0.5993.70.0)
Note: you may need to restart the kernel to use updated packages.

[3]: import chromedriver_binary

[4]: driver = webdriver.Chrome()

[5]: url = "https://www.amazon.com"

[6]: lk = driver.get(url)

[7]: #dir(lk)

[8]: def my_url(keyword):

# temp = 'https://www.amazon.com/s?k=phone+case&ref=nb_sb_noss_1' #let's␣


,→ get rid of the 'phone+case' and replace it with {} to make the url generic.
temp = 'https://www.amazon.com/s?k={}&ref=nb_sb_noss_1' # a template url
keyword = keyword.replace(' ', '+')
return temp.format(keyword)

[9]: l_laptop = my_url("laptop charger")

[10]: driver.get(l_laptop)

[11]: from bs4 import BeautifulSoup

soup = BeautifulSoup(driver.page_source,'html.parser')

[12]: soup_results=soup.find_all('div',{'data-component-type':'s-search-result'})

1
[13]: len(soup_results)

[13]: 19

[14]: obj=soup_results[0]
obj

[14]: <div class="sg-col-20-of-24 s-result-item s-asin sg-col-0-of-12 sg-col-16-of-20


AdHolder sg-col s-widget-spacing-small sg-col-12-of-16" data-asin="B07CQ7NVBY"
data-cel-widget="search_result_1" data-component-id="7" data-component-
type="s-search-result" data-index="2" data-
uuid="6dff439e-23ff-42d9-8ad0-0fdfa2fe62f7"><div class="sg-col-inner"><div
cel_widget_id="MAIN-SEARCH_RESULTS-2" class="s-widget-container s-spacing-small
s-widget-container-height-small celwidget slot=MAIN template=SEARCH_RESULTS
widgetId=search-results_1" data-cel-widget="MAIN-SEARCH_RESULTS-2" data-csa-c-
id="34789k-bjyt6j-wyllqw-wa19rd" data-csa-c-item-id="amzn1.asin.1.B07CQ7NVBY"
data-csa-c-pos="1" data-csa-c-type="item" data-csa-op-log-render="">
<div class="rush-component" data-component-id="31" data-component-props='{"perce
ntageShownToFire":"50","batchable":true,"requiredElementSelector":".s-image:visi
ble","url":"https://unagi-na.amazon.com/1/events/com.amazon.eel.SponsoredProduct
sEventTracking.prod?qualifier=1698304654&amp;id=6243794780806663&amp;widgetName=
sp_atf&amp;adId=200019784239561&amp;eventType=1&amp;adIndex=0"}' data-component-
type="s-impression-logger">
<div class="rush-component s-featured-result-item" data-component-id="2" data-
component-props='{"presenceCounterName":"sp_delivered","testElementSelector":".s
-image","hiddenCounterName":"sp_hidden"}' data-component-type="s-impression-
counter">
<div class="puis-card-container s-card-container s-overflow-hidden aok-relative
puis-include-content-margin puis puis-v1k579acibq3g12dxe3xrr3x1qq s-latency-cf-
section puis-card-border"><div class="a-section"><div class="puisg-row"><div
class="puisg-col puisg-col-4-of-12 puisg-col-4-of-16 puisg-col-4-of-20 puisg-
col-4-of-24 puis-list-col-left"><div class="puisg-col-inner"><div
class="a-section a-spacing-none aok-relative puis-status-badge-container s-list-
status-badge-container"></div><div class="s-product-image-container aok-relative
s-text-center s-image-overlay-grey puis-image-overlay-grey s-padding-left-small
s-padding-right-small puis-flex-expand-height puis
puis-v1k579acibq3g12dxe3xrr3x1qq"><div class="aok-relative"><span class="rush-
component" data-component-type="s-product-image" data-render-
id="rq6fic0nz6bve1zz8iswk8dnai" data-version-id="v1k579acibq3g12dxe3xrr3x1qq"><a
class="a-link-normal s-no-outline" href="/sspa/click?ie=UTF8&amp;spc=MTo2MjQzNzk
0NzgwODA2NjYzOjE2OTgzMDQ2NTQ6c3BfYXRmOjIwMDAxOTc4NDIzOTU2MTo6MDo6&amp;url=%2FKen
sington-Universal-Tablet-Station-K62878NA%2Fdp%2FB07CQ7NVBY%2Fref%3Dsr_1_1_sspa%
3Fkeywords%3Dlaptop%2Bcharger%26qid%3D1698304654%26sr%3D8-1-spons%26sp_csd%3Dd2l
kZ2V0TmFtZT1zcF9hdGY%26psc%3D1"><div class="a-section aok-relative s-image-
fixed-height"><img alt="Sponsored Ad - Kensington Universal Tablet and Laptop AC
Charge Station (K62878NA)" class="s-image" data-image-index="1" data-image-
latency="s-product-image" data-image-load="" data-image-source-density="1"

2
src="https://m.media-amazon.com/images/I/61toqj6DmtL._AC_UY218_.jpg"
srcset="https://m.media-amazon.com/images/I/61toqj6DmtL._AC_UY218_.jpg 1x,
https://m.media-amazon.com/images/I/61toqj6DmtL._AC_UY327_FMwebp_QL65_.jpg 1.5x,
https://m.media-amazon.com/images/I/61toqj6DmtL._AC_UY436_FMwebp_QL65_.jpg 2x,
https://m.media-amazon.com/images/I/61toqj6DmtL._AC_UY545_FMwebp_QL65_.jpg 2.5x,
https://m.media-amazon.com/images/I/61toqj6DmtL._AC_UY654_FMwebp_QL65_.jpg
3x"/></div></a></span></div></div></div></div><div class="puisg-col puisg-
col-4-of-12 puisg-col-8-of-16 puisg-col-12-of-20 puisg-col-12-of-24 puis-list-
col-right"><div class="puisg-col-inner"><div class="a-section a-spacing-small
a-spacing-top-small"><div class="a-section a-spacing-none puis-padding-right-
small s-title-instructions-style"><div class="a-row a-spacing-micro"><span
class="a-declarative" data-a-popover='{"name":"sp-info-popover-B07CQ7NVBY","posi
tion":"triggerVertical","closeButton":"true","dataStrategy":"preload"}' data-
action="a-popover" data-csa-c-func-deps="aui-da-a-popover" data-csa-c-
id="ecfiem-4o50u1-fjd5ue-owff9a" data-csa-c-type="widget" data-render-
id="rq6fic0nz6bve1zz8iswk8dnai" data-version-id="v1k579acibq3g12dxe3xrr3x1qq"><a
aria-label="View Sponsored information or leave ad feedback" class="puis-label-
popover puis-sponsored-label-text" href="javascript:void(0)" role="button"
style="text-decoration: none;"><span class="puis-label-popover-default"><span
class="a-color-secondary">Sponsored</span></span><span class="puis-label-
popover-hover"><span class="a-color-base">Sponsored</span></span> <span
class="aok-inline-block puis-sponsored-label-info-icon"></span></a></span><div
class="a-popover-preload" id="a-popover-sp-info-popover-B07CQ7NVBY"><div
class="puis puis-v1k579acibq3g12dxe3xrr3x1qq"><span>You’re seeing this ad based
on the product’s relevance to your search query.</span><div class="a-row"><span
class="a-declarative" data-action="s-safe-ajax-modal-trigger" data-csa-c-func-
deps="aui-da-s-safe-ajax-modal-trigger" data-csa-c-
id="rjid45-fismav-2q0y85-31l11y" data-csa-c-type="widget" data-render-
id="rq6fic0nz6bve1zz8iswk8dnai" data-s-safe-ajax-modal-trigger='{"header":"Leave
feedback","dataStrategy":"ajax","ajaxUrl":"/af/sp-loom/feedback-form?pl=%7B%22ad
PlacementMetaData%22%3A%7B%22searchTerms%22%3A%22bGFwdG9wIGNoYXJnZXI%3D%22%2C%22
pageType%22%3A%22Search%22%2C%22feedbackType%22%3A%22sponsoredProductsLoom%22%2C
%22slotName%22%3A%22TOP%22%7D%2C%22adCreativeMetaData%22%3A%7B%22adProgramId%22%
3A1024%2C%22adCreativeDetails%22%3A%5B%7B%22asin%22%3A%22B07CQ7NVBY%22%2C%22titl
e%22%3A%22Kensington+Universal+Tablet+and+Laptop+AC+Charge+Station+%28K62878NA%2
9%22%2C%22priceInfo%22%3A%7B%22amount%22%3A320.03%2C%22currencyCode%22%3A%22USD%
22%7D%2C%22sku%22%3A%22B07CQ7NVBY%22%2C%22adId%22%3A%22A0480368MZ959V95FDWZ%22%2
C%22campaignId%22%3A%22A06915411GPYIOFF99HN%22%2C%22advertiserIdNS%22%3Anull%2C%
22selectionSignals%22%3Anull%7D%5D%7D%7D"}' data-version-
id="v1k579acibq3g12dxe3xrr3x1qq"><a class="a-link-normal s-underline-text
s-underline-link-text s-link-style" href="#"><span>Leave ad feedback</span> </a>
</span></div></div></div></div><h2 class="a-size-mini a-spacing-none a-color-
base s-line-clamp-2"><a class="a-link-normal s-underline-text s-underline-link-
text s-link-style a-text-normal" href="/sspa/click?ie=UTF8&amp;spc=MTo2MjQzNzk0N
zgwODA2NjYzOjE2OTgzMDQ2NTQ6c3BfYXRmOjIwMDAxOTc4NDIzOTU2MTo6MDo6&amp;url=%2FKensi
ngton-Universal-Tablet-Station-K62878NA%2Fdp%2FB07CQ7NVBY%2Fref%3Dsr_1_1_sspa%3F
keywords%3Dlaptop%2Bcharger%26qid%3D1698304654%26sr%3D8-1-spons%26sp_csd%3Dd2lkZ

3
2V0TmFtZT1zcF9hdGY%26psc%3D1"><span class="a-size-medium a-color-base a-text-
normal">Kensington Universal Tablet and Laptop AC Charge Station
(K62878NA)</span> </a> </h2></div><div class="a-section a-spacing-none
a-spacing-top-micro"><div class="a-row a-size-small"><span aria-label="4.2 out
of 5 stars"><span class="a-declarative" data-a-popover='{"position":"triggerBott
om","popoverLabel":"","url":"/review/widgets/average-customer-review/popover/ref
=acr_search__popover?ie=UTF8&amp;asin=B07CQ7NVBY&amp;ref=acr_search__popover&amp
;contextId=search","closeButton":false,"closeButtonLabel":""}' data-
action="a-popover" data-csa-c-func-deps="aui-da-a-popover" data-csa-c-
id="fh60dn-5240ir-473k9h-g4yylt" data-csa-c-type="widget" data-render-
id="rq6fic0nz6bve1zz8iswk8dnai" data-version-id="v1k579acibq3g12dxe3xrr3x1qq"><a
class="a-popover-trigger a-declarative" href="javascript:void(0)"
role="button"><i class="a-icon a-icon-star-small a-star-small-4 aok-align-
bottom"><span class="a-icon-alt">4.2 out of 5 stars</span></i><i class="a-icon
a-icon-popover"></i></a></span> </span><span aria-label="78"><a class="a-link-
normal s-underline-text s-underline-link-text s-link-style" href="/sspa/click?ie
=UTF8&amp;spc=MTo2MjQzNzk0NzgwODA2NjYzOjE2OTgzMDQ2NTQ6c3BfYXRmOjIwMDAxOTc4NDIzOT
U2MTo6MDo6&amp;url=%2FKensington-Universal-Tablet-Station-K62878NA%2Fdp%2FB07CQ7
NVBY%2Fref%3Dsr_1_1_sspa%3Fkeywords%3Dlaptop%2Bcharger%26qid%3D1698304654%26sr%3
D8-1-spons%26sp_csd%3Dd2lkZ2V0TmFtZT1zcF9hdGY%26psc%3D1#customerReviews"><span
class="a-size-base s-underline-text">78</span> </a> </span></div></div><div
class="puisg-row"><div class="puisg-col puisg-col-4-of-12 puisg-col-4-of-16
puisg-col-4-of-20 puisg-col-4-of-24"><div class="puisg-col-inner"><div
class="a-section a-spacing-none a-spacing-top-micro puis-price-instructions-
style"><div class="a-row a-size-base a-color-base"><a class="a-size-base a-link-
normal s-no-hover s-underline-text s-underline-link-text s-link-style a-text-
normal" href="/sspa/click?ie=UTF8&amp;spc=MTo2MjQzNzk0NzgwODA2NjYzOjE2OTgzMDQ2NT
Q6c3BfYXRmOjIwMDAxOTc4NDIzOTU2MTo6MDo6&amp;url=%2FKensington-Universal-Tablet-St
ation-K62878NA%2Fdp%2FB07CQ7NVBY%2Fref%3Dsr_1_1_sspa%3Fkeywords%3Dlaptop%2Bcharg
er%26qid%3D1698304654%26sr%3D8-1-spons%26sp_csd%3Dd2lkZ2V0TmFtZT1zcF9hdGY%26psc%
3D1"><span class="a-price" data-a-color="base" data-a-size="xl"><span
class="a-offscreen">$320.03</span><span aria-hidden="true"><span class="a-price-
symbol">$</span><span class="a-price-whole">320<span class="a-price-
decimal">.</span></span><span class="a-price-fraction">03</span></span></span>
</a> </div></div><div class="a-section a-spacing-none a-spacing-top-micro"><div
class="a-row a-size-base a-color-secondary s-align-children-center"><span aria-
label="Delivery Tue, Oct 31 "><span class="a-color-base">Delivery </span><span
class="a-color-base a-text-bold">Tue, Oct 31 </span></span></div><div
class="a-row a-size-base a-color-secondary s-align-children-center"><span
class="a-size-small a-color-base">Ships to Cameroon</span></div><div
class="a-row a-size-base a-color-secondary"><span aria-label="Only 13 left in
stock - order soon."><span class="a-size-base a-color-price">Only 13 left in
stock - order soon.</span></span></div></div></div></div><div class="puisg-col
puisg-col-4-of-12 puisg-col-4-of-16 puisg-col-8-of-20 puisg-col-8-of-24"><div
class="puisg-col-inner"></div></div></div></div></div></div></div></div></div>
</div>
</div>

4
</div></div></div>

[15]: atag = obj.h2.a


atag

[15]: <a class="a-link-normal s-underline-text s-underline-link-text s-link-style


a-text-normal" href="/sspa/click?ie=UTF8&amp;spc=MTo2MjQzNzk0NzgwODA2NjYzOjE2OTg
zMDQ2NTQ6c3BfYXRmOjIwMDAxOTc4NDIzOTU2MTo6MDo6&amp;url=%2FKensington-Universal-
Tablet-Station-K62878NA%2Fdp%2FB07CQ7NVBY%2Fref%3Dsr_1_1_sspa%3Fkeywords%3Dlapto
p%2Bcharger%26qid%3D1698304654%26sr%3D8-1-spons%26sp_csd%3Dd2lkZ2V0TmFtZT1zcF9hd
GY%26psc%3D1"><span class="a-size-medium a-color-base a-text-normal">Kensington
Universal Tablet and Laptop AC Charge Station (K62878NA)</span> </a>

[16]: des = atag.text.strip()


des

[16]: 'Kensington Universal Tablet and Laptop AC Charge Station (K62878NA)'

You might also like