程序的结构设计
1 提交商品搜索清酒
2 提取商品的名称和价格信用
3 将信息输出到屏幕上
来自mooc
import requestsimport redef getHTMLText(url):try:r = requests.get(url)r.raise_for_status()r.encoding = r.apparent_encodingreturn r.textexcept:return ""def parsePage(ilt,html):try:plt = re.findall(r'\"view_price\"\:\"[\d\.]*\"',html)tlt = re.findall(r'\"raw_title\"\:\".*?\"',html) #*?表示的是最小匹配只取得最后一个双引号所需要的东西for i in range(len(plt)):price = eval(plt[i].split(':')[1])#获得后面的那个 因为是字典 下面同 也是这个样子title = eval(tlt[i].split(':')[1])#用eval可以将最外层的双引号或单引号去掉ilt.append([price,title]) #字典的添加方式?except:print("")def printGoodList(ilt):tplt = "{:4}\t{:8}\t{:16}"print(tplt.format("序号","价格","商品名称"))count = 0for g in ilt:count = count + 1print(tplt.format(count,g[0],g[1]))def main():goods = "书包"depth = 3start_url = '/search?q=' + goodsinfoList = []for i in range(depth):try:url = start_url + '&s=' + str(48 * i)html = getHTMLText(url)parsePage(infoList,html)except:continueprintGoodList(infoList)main()