python 抓取凡客商品名称和价格demo

python 3.2下测试通过。

#加载模块

import urllib.request

import urllib.response

import urllib.parse

import re

import time

conn_req=urllib.request

conn_pre=urllib.parse

conn_res=urllib.response

#定义header头信息

user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.15 Safari/537.36'

headers = {'User-Agent' : user_agent,

           'Accept':'text/javascript, */*; q=0.01',

           'Accept-Language':'zh-CN,zh;q=0.8',

           'Referer':'http://www.jd.com'}

#获取商品名称和价格

def get_price(page):

   try:

           t = re.findall('<title>(.*)</title>',str(page,encoding = "utf-8"))

           if t:

               m = re.findall(r'(\d+\.00)',str(page,encoding = "utf-8"))

               m1 = re.search('strong',str(page,encoding = "utf-8"))

               if m and m1:

                   print(str(t[0])+":"+str(m[0]))

   except Exception as e:

       print(e)

#循环访问单品页

for i in range(4300,6999):

   time.sleep(1)

   try:

       url='http://item.vt.vancl.com/040'+repr(i)+'.html'

       req=conn_req.Request(url,None,headers)

       fb=conn_req.urlopen(req)

       page=fb.read()

       get_price(page)

       fb.close()

   except Exception as e:

       print(url)

       print(e)

       continue


你可能感兴趣的:(python,抓取,凡客,商品,价格,vancl)