Guest User

Untitled

a guest
Aug 13th, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.17 KB | None | 0 0
  1.     def parse_blog(self, soup):
  2.         for block in soup.find_all('li', class_='g'):
  3.             if block.get('id') == 'newsbox':
  4.                 continue
  5.  
  6.             item = self.init_item()
  7.             try:
  8.                 item['title'] = block.find('h3', class_="r").text.strip()
  9.                 url = block.find('h3', class_="r").a.get('href')
  10.                 link = urlparse.parse_qs(url).get('/url?q')
  11.                 if link:
  12.                     item['link'] = link[0]
  13.                 else:
  14.                     item['link'] = url
  15.             except:
  16.                 print traceback.format_exc()
  17.  
  18.             try:
  19.                 tmp = block.find('div', class_="f slp")
  20.                 if tmp:
  21.                     date_author = tmp.text
  22.                     da = date_author.split(' by ')
  23.                     if len(da) == 1:
  24.                         item['date'] = da[0]
  25.                     if len(da) == 2:
  26.                         item['date'], item['author'] = da[0], da[1]
  27.             except:
  28.                 print traceback.format_exc()
  29.  
  30.             try:
  31.                 item['excerpt'] = block.find('div',
  32.                                              class_='st').text.strip()
  33.             except:
  34.                 try:
  35.                     item['excerpt'] = block.find('span',
  36.                                                  class_='st').text.strip()
  37.                 except:
  38.                     print traceback.format_exc()
  39.  
  40.             try:
  41.                 tmp = item['excerpt'].split('-', 1)
  42.                 if len(tmp) == 2:
  43.                     ds = tmp[0]
  44.                     if 'days ago' in ds:
  45.                         days = int(ds.split()[0])
  46.                         date = datetime.date.today() - datetime.timedelta(days=days)
  47.                         date = date.strftime('%b %d, %Y')
  48.                         item['date'] = date
  49.                     else:
  50.                         item['date'] = ds
  51.             except:
  52.                 print traceback.format_exc()
  53.  
  54.             if item:
  55.                 item['search_url'] = self.search_url
  56.                 item['type'] = 'blog'
  57.                 self.count += 1
  58.                 self.work_q.put(item)
Advertisement
Add Comment
Please, Sign In to add comment