from urllib.request import Request
from urllib.request import urlopen
from bs4 import BeautifulSoup
url = "https://www.amazon.com/s?k=led+strip+lights&page=1"
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) ……", #保密,按F12即可知道自己的专属User-Agent
"referer": "https://www.amazon.com/Nike-Womens-Pegasus-Running-Shoes/dp/B07CXGMVJL/ref=cts_sh_2_vtp",
"authority": "www.amazon.com",
"path": "/s?k=led+strip+lights&ref=nb_sb_noss_1",
"cookie": ""} #此处 保密
req = Request(url=url, headers=headers)
response = urlopen(req)
print(type(response))
soup = BeautifulSoup(response, "lxml")
print(type(soup))
products = soup.find("div", {"class":"s-result-list s-search-results sg-row"})
想要知道每页展示的灯数,但结果不对
print(len(products.div.div))
print(products.div.div)
print(products.get_text())
<span class="a-size-base-plus a-color-base a-text-normal"
header = products.find("span",{"class":"a-size-base-plus a-color-base a-text-normal"})
print("header:", header.string.strip())
score = products.find("span", {"class":"a-icon-alt"})
print("score:", score.string.strip())
review = products.find("div", {"class":"a-row a-size-small"}).descendants
print(review)
"""
review = review.find("span", {"class":"a-size-base"})
print("review:", review.string.strip())
price = products.find("span", {"class":"a-offscreen"})
print("price:", price.string.strip())
delivery = products.find("i", {"aria-label":"Amazon Prime"})
if delivery:
FBA = True
else:
FBA = False
print("FBA:", FBA)
image_url = products.find("img", {"class":"s-image"})
image_url = image_url.attrs["src"]
print("image_url:", image_url)
coupon = products.find("span", {"class":"a-size-base s-coupon-highlight-color s-highlighted-text-padding aok-inline-block"})
print("coupon:", coupon.string.strip())
Sponsered = products.find("span", {"class":"a-size-base a-color-secondary"})
if Sponsered:
Sponsered = True
else:
Sponsered = False
print("Sponsered:", Sponsered)
"""
"""
Num = 0
for pro in products:
print(Num)
attrs =
#asin = pro.div.div.attrs["data-asin"]
#print(asin, "\n")
No += 1
”“”
未完,ing
原文地址:https://www.cnblogs.com/wyy1480/p/11247779.html