商品爬取

  1. #CrowTaobaoPrice.py
  2. import requests
  3. import re
  4.  
  5. def getHTMLText(url):
  6.     try:
  7.         = requests.get(url, timeout=30)
  8.         r.raise_for_status()
  9.         r.encoding = r.apparent_encoding
  10.         return r.text
  11.     except:
  12.         return ""
  13.      
  14. def parsePage(ilt, html):
  15.     try:
  16.         plt = re.findall(r'\"view_price\"\:\"[\d\.]*\"',html)
  17.         tlt = re.findall(r'\"raw_title\"\:\".*?\"',html)
  18.         for in range(len(plt)):
  19.             price = eval(plt[i].split(':')[1])
  20.             title = eval(tlt[i].split(':')[1])
  21.             ilt.append([price , title])
  22.     except:
  23.         print("")
  24.  
  25. def printGoodsList(ilt):
  26.     tplt = "{:4}\t{:8}\t{:16}"
  27.     print(tplt.format("序号""价格""商品名称"))
  28.     count = 0
  29.     for in ilt:
  30.         count = count + 1
  31.         print(tplt.format(count, g[0], g[1]))
  32.          
  33. def main():
  34.     goods = '书包'
  35.     depth = 3
  36.     start_url = 'https://s.taobao.com/search?q=' + goods
  37.     infoList = []
  38.     for in range(depth):
  39.         try:
  40.             url = start_url + '&s=' + str(44*i)
  41.             html = getHTMLText(url)
  42.             parsePage(infoList, html)
  43.         except:
  44.             continue
  45.     printGoodsList(infoList)
  46.      
  47. main()

​​​​​​​

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值