收集到很多易迅网的商品ID,于是想把这些ID对应的商品信息爬下来。通过简单分析发现,易迅网的各类信息都是直接放在HTML页面上,所以,解析一个页面就好了。
最后返回每个ID对应的商品url,标题,易迅价,促销价,类目 。
下面是python代码:
#!/usr/bin/env python
#coding:utf-8
'''
Created on 2015年03月11日
@author: zhaohf
'''
import urllib2
from bs4 import BeautifulSoup
def get_yixun(id):
price_origin,price_sale,category = '0','0',''
url = 'http://item.yixun.com/item-' + id + '.html'
html = urllib2.urlopen(url).read().decode('utf-8')
soup = BeautifulSoup(html)
title = unicode(soup.title.text.strip().strip(u'【价格_报价_图片_行情】-易迅网').replace(u'】','')).encode('utf-8').decode('utf-8')
try:
soup_origin = soup.find("dl", { "class" : "xbase_item xprice xprice_origin" })
price_origin = soup_origin.find("span", { "class" : "mod_price xprice_val" }).contents[1].text #易迅价
print 'price_origin: ' + price_origin
except:
pass
try:
soup_sale= soup.find('dl',{'class':'xbase_item xprice'})
price_sale = soup_sale.find("span", { "class" : "mod_price xprice_val" }).contents[1] #促销价
print 'price_sale: '+ price_sale
except:
pass
try:
category = unicode(soup.find('div',{'class','mod_crumb'}).text).encode('utf-8').decode('utf-8').replace('\n','') #所属类目
except:
pass
if not (price_origin is None or price_origin =='0'):
print url + '\t'+ price_origin + '\t' + price_sale + '\t'+ category
return url + '\t' + title +'\t'+price_origin+'\t'+price_sale+ '\t'+ category
else:
print url + '\t' + price_sale+ '\t' + price_sale + '\t' + category
return url + '\t' + title +'\t'+price_sale+'\t'+price_sale+ '\t'+ category
return None