import bs4
import requests
import xlwt
import datetime
params={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
}
date = datetime.datetime.now().strftime('%Y-%m-%d') # 给文件打上时间戳,便于数据更新
url = 'https://uland.taobao.com/sem/tbsearch?refpid=mm_26632360_8858797_29866178&keyword=%E4%B9%A6%E7%B1%8D&clk1=da382fc3cc2efef28fd1c72638a78aca&upsId=da382fc3cc2efef28fd1c72638a78aca&spm=a2e0b.20350158.search.1&pid=mm_26632360_8858797_29866178&union_lens=recoveryid%3A201_11.170.86.131_4623454_1620196433004%3Bprepvid%3A201_11.170.86.131_4623454_1620196433004' # 网址
# payload = {'SearchText': 'taob', 'page': '1', 'ie': 'utf8', 'g': 'y'} # 字典传递url参数
resp = requests.get(url,headers=params)
soup = bs4.BeautifulSoup(resp.text, "html.parser")
print(resp.url) # 打印访问的网址
# print(resp.text)
print(resp.status_code)
resp.encoding = 'utf-8' # 设置编码
title=[]
# 标题
all_title = soup.find_all('span',class_="title-text")
print(all_title)
# for j in all_title:
# soup_title = bs4.BeautifulSoup(str(j), "html.parser")
# title.append(soup_title.span.string)
# print(title)
#
# # 店铺名称
# all_store = soup.find_all('span', class_="shopNick")
# for k in all_store:
# soup_store = bs4.BeautifulSoup(str(k), "html.parser", )
# store.append(soup_store.span.string)
#
# # 价格
# all_price = soup.find_all('span', class_="pricedetail")
# for l in all_price:
# soup_price = bs4.BeautifulSoup(str(l), "html.parser")
# price.append(soup_price.strong.string)
#
# # 销售量
# all_paynum = soup.find_all('span', class_="payNum")
# for m in all_paynum:
# soup_paynum = bs4.BeautifulSoup(str(m), "html.parser")
# paynum.append(soup_paynum.span.string)
#
# # 数据验证
# print(len(title))
# print(len(store))
# print(len(price))
# print(len(paynum))