import requests
from bs4 import BeautifulSoup
import lxml
url = "https://list.tmall.com/search_product.htm?q=%E6%89%8B%E6%9C%BA&ali_trackid=2:mm_26632258_3504122_57418735:1577712017_241_341152467&clk1=87e613aada9370b3fbfb1effeacd31fa&upsid=87e613aada9370b3fbfb1effeacd31fa"
req = requests.get(url)
print("status_code: ", req.status_code)
while req.status_code != 200:
req = requests.get(url)
html = req.text
bf = BeautifulSoup(html, "lxml")
soup = bf.find_all('div', class_ = 'product')
for item in soup:
price_list = item.select('em')
price = str(price_list)
n = price.find('=')
n_ = price.find('.')
price = price[n+2 : n_] + '¥'
name_list = item.select('a')
name = name_list[1].string
txt = "%s : %s" %(name, price)
with open("手机价格.txt", "w", encoding="utf-8") as file:
file.write(txt)
file.close()