python数据抓取分析的示例代码(python + mongodb)

本文介绍了Python数据抓取分析,分享给大家,具体如下:

编程模块:requests,lxml,pymongo,time,BeautifulSoup

首先获取所有产品的分类网址:

def step():
 try:
  headers = {
   。。。。。
   }
  r = requests.get(url,headers,timeout=30)
  html = r.content
  soup = BeautifulSoup(html,"lxml")
  url = soup.find_all(正则表达式)
  for i in url:
   url2 = i.find_all('a')
   for j in url2:
     step1url =url + j['href']
     print step1url
     step2(step1url)
 except Exception,e:
  print e

我们在产品分类的同时需要确定我们所访问的地址是产品还是又一个分类的产品地址(所以需要判断我们访问的地址是否含有if判断标志):

def step2(step1url):
 try:
  headers = {
   。。。。
   }
  r = requests.get(step1url,headers,timeout=30)
  html = r.content
  soup = BeautifulSoup(html,"lxml")
  a = soup.find('div',id='divTbl')
  if a:
   url = soup.find_all('td',class_='S-ITabs')
   for i in url:
    classifyurl = i.find_all('a')
    for j in classifyurl:
      step2url = url + j['href']
      #print step2url
      step3(step2url)
  else:
   postdata(step1url)

当我们if判断后为真则将第二页的分类网址获取到(第一个步骤),否则执行postdata函数,将网页产品地址抓取!

def producturl(url):
 try:
  p1url = doc.xpath(正则表达式)
  for i in xrange(1,len(p1url) + 1):
   p2url = doc.xpath(正则表达式)
   if len(p2url) > 0:
    producturl = url + p2url[0].get('href')
    count = db[table].find({'url':producturl}).count()
    if count <= 0:
      sn = getNewsn()
      db[table].insert({"sn":sn,"url":producturl})
      print str(sn) + 'inserted successfully'
    else:
      'url exist'
 except Exception,e:
  print e

其中为我们所获取到的产品地址并存入mongodb中,sn作为地址的新id。

下面我们需要在mongodb中通过新id索引来获取我们的网址并进行访问,对产品进行数据分析并抓取,将数据更新进数据库内!

其中用到最多的BeautifulSoup这个模块,但是对于存在于js的价值数据使用BeautifulSoup就用起来很吃力,所以对于js中的数据我推荐使用xpath,但是解析网页就需要用到HTML.document_fromstring(url)方法来解析网页。

对于xpath抓取价值数据的同时一定要细心!如果想了解xpath就在下面留言,我会尽快回答!

def parser(sn,url):
 try:
  headers = {
   。。。。。。
   }
  r = requests.get(url, headers=headers,timeout=30)
  html = r.content
  soup = BeautifulSoup(html,"lxml")
  dt = {}
  #partno
  a = soup.find("meta",itemprop="mpn")
  if a:
   dt['partno'] = a['content']
  #manufacturer
  b = soup.find("meta",itemprop="manufacturer")
  if b:
   dt['manufacturer'] = b['content']
  #description
  c = soup.find("span",itemprop="description")
  if c:
   dt['description'] = c.get_text().strip()
  #price
  price = soup.find("table",class_="table table-condensed occalc_pa_table")
  if price:
   cost = {}
   for i in price.find_all('tr'):
    if len(i) > 1:
     td = i.find_all('td')
     key=td[0].get_text().strip().replace(',','')
     val=td[1].get_text().replace(u'\u20ac','').strip()
     if key and val:
      cost[key] = val
   if cost:
    dt['cost'] = cost
    dt['currency'] = 'EUR'
  
  #quantity
  d = soup.find("input",id="ItemQuantity")
  if d:
   dt['quantity'] = d['value']
  #specs
  e = soup.find("div",class_="row parameter-container")
  if e:
   key1 = []
   val1= []
   for k in e.find_all('dt'):
    key = k.get_text().strip().strip('.')
    if key:
     key1.append(key)
   for i in e.find_all('dd'):
    val = i.get_text().strip()
    if val:
     val1.append(val)
   specs = dict(zip(key1,val1))
  if specs:
   dt['specs'] = specs
   print dt   
  if dt:
   db[table].update({'sn':sn},{'$set':dt})
   print str(sn) + ' insert successfully'
   time.sleep(3)
  else:
   error(str(sn) + '\t' + url)
 except Exception,e:
  error(str(sn) + '\t' + url)
  print "Don't data!"

最后全部程序运行,将价值数据分析处理并存入数据库中!

相关推荐