python爬京东商品价格

import re
import requests

def getHtmlText(url):
    try:
        r = requests.get(url,timeout=30)
        r.raise_for_status()
        r.encoding = 'utf-8'
        return r.text
    except:
        return ""

def parsePage(ilt,html):
    try:
        tlt=re.findall(r'"raw_title":".*?"',html)
        plt=re.findall(r'"view_price":"\d*\.\d{2}"',html)
        for i in range(len(tlt)):
            price=eval(plt[i].split(':')[1])
            title=eval(tlt[i].split(':')[1])
            ilt.append([price,title])
    except:
        print('error')



def printGoodsList(ilt):
    tplt = "{:4}\t{:8}\t{:16}"
    print(tplt.format('序号','价格','名称'))#把名称放最后便于统一格式,汉字就是有点不一样
    count=0
    for i in ilt:
        count = count + 1 
        print(tplt.format(count,i[0],i[1]))



def main():
    goods='机械键盘'
    depth=3
    init_url='http://s.taobao.com/search?q='+goods+'&s='
    infoList=[]
    for i in range(depth):
        try:
            url=init_url+str(i*44)
            html=getHtmlText(url)
            parsePage(infoList,html)
        except:
            continue
    printGoodsList(infoList)



main()

你可能感兴趣的:(python爬京东商品价格)