爬取灯带情况

Posted wyy1480

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了爬取灯带情况相关的知识,希望对你有一定的参考价值。

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

以上是关于爬取灯带情况的主要内容,如果未能解决你的问题,请参考以下文章

(参考)爬虫5-爬取中国大学排名情况

断点续传

爬取ofo共享单车信息

怎么用python爬虫爬取可以加载更多的网页

Python3 爬虫06_robots.txt查看网站爬取限制情况

网站爬取工具