天猫单页手机价格爬取

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()
发布了84 篇原创文章 · 获赞 10 · 访问量 8570

猜你喜欢

转载自blog.csdn.net/AK47red/article/details/103777568
今日推荐