1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python通过关键字搜索淘宝商品详细信息

python通过关键字搜索淘宝商品详细信息

时间:2020-03-14 12:57:04

相关推荐

python通过关键字搜索淘宝商品详细信息

# coding=utf-8importurllib.requestimportrefromurllib.requestimporturlopen, urlretrieve#打开网页,获取网页内容defurl_open(url):headers=("user-agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.22 Safari/537.36 SE 2.X MetaSr 1.0")opener=urllib.request.build_opener()opener.addheaders=[headers]urllib.request.install_opener(opener)data=urllib.request.urlopen(url).read().decode("utf-8","ignore")returndataif__name__=='__main__':try:#定义要查询的商品关键词 keywd="女朋友礼物" keywords=urllib.request.quote(keywd)#定义要爬取的页数 num=100foriinrange(num):url="/search?q="+keywords+"&imgfile=&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=a21bo.50862.56-taobao-item.1&ie=utf8&bcoffset=4&ntoffset=4&p4ppushleft=1%2C48&s="+str(i*44)data=url_open(url)#定义各个字段正则匹配规则 img_pat='"pic_url":"(//.*?)"' name_pat='"raw_title":"(.*?)"' nick_pat='"nick":"(.*?)"' price_pat='"view_price":"(.*?)"' fee_pat='"view_fee":"(.*?)"' sales_pat='"view_sales":"(.*?)"' comment_pat='"comment_count":"(.*?)"' city_pat='"item_loc":"(.*?)"' #查找满足匹配规则的内容,并存在列表中 imgL=pile(img_pat).findall(data)nameL=pile(name_pat).findall(data)nickL=pile(nick_pat).findall(data)priceL=pile(price_pat).findall(data)feeL=pile(fee_pat).findall(data)salesL=pile(sales_pat).findall(data)commentL=pile(comment_pat).findall(data)cityL=pile(city_pat).findall(data)forjinrange(len(imgL)):img="http:"+imgL[j]#商品图片链接name=nameL[j]#商品名称nick=nickL[j]#淘宝店铺名称price=priceL[j]#商品价格fee=feeL[j]#运费sales=salesL[j]#商品付款人数comment=commentL[j]#商品评论数,会存在为空值的情况if(comment==""):comment=0city=cityL[j]#店铺所在城市print('正在爬取第'+str(i)+"页,第"+str(j)+"个商品信息...")description=str(price)+"#"+str(sales)+"#"+nick+"#"+nametry:urllib.request.urlretrieve(img,'e://taobao//'+description+".jpg")exceptExceptionase:print(e)exceptExceptionase:print(str(e))print("任务完成")

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