100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 淘宝商品比价定向爬虫

淘宝商品比价定向爬虫

时间:2021-07-14 23:27:51

相关推荐

淘宝商品比价定向爬虫

程序的结构设计

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()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。