程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 對Java中JSON解析器的一些看法

對Java中JSON解析器的一些看法

編輯:關於JAVA

對Java中JSON解析器的一些看法。本站提示廣大學習愛好者:(對Java中JSON解析器的一些看法)文章只能為提供參考,不一定能成為您想要的結果。以下是對Java中JSON解析器的一些看法正文


我匯集了國際10幾個片子網站的數據,外面近幾十W筆記錄,用文本沒法存,mongodb進修本錢異常低,裝置、下載、運轉起來不會花你5分鐘時光。

# -*- coding: utf-8 -*-
# by awakenjoys. my site: www.dianying.at
import re
import urllib2
from bs4 import BeautifulSoup
import string, time
import pymongo
 
NUM  = 0   #全局變量,片子數目
m_type = u''  #全局變量,片子類型
m_site = u'qq' #全局變量,片子網站
 
#依據指定的URL獲得網頁內容
def gethtml(url):
 req = urllib2.Request(url) 
 response = urllib2.urlopen(req) 
 html = response.read()
 return html
 
#從片子分類列表頁面獲得片子分類
def gettags(html):
 global m_type
 soup = BeautifulSoup(html)  #過濾出分類內容
 #print soup
 #<ul class="clearfix _group" gname="mi_type" gtype="1">
 tags_all = soup.find_all('ul', {'class' : 'clearfix _group' , 'gname' : 'mi_type'})
 #print len(tags_all), tags_all
 #print str(tags_all[1]).replace('\n', '')
 
 #<a _hot="tag.sub" class="_gtag _hotkey" href="http://v.qq.com/list/1_0_-1_-1_1_0_0_20_0_-1_0.html" title="舉措" tvalue="0">舉措</a>
 re_tags = r'<a _hot=\"tag\.sub\" class=\"_gtag _hotkey\" href=\"(.+?)\" title=\"(.+?)\" tvalue=\"(.+?)\">.+?</a>'
 p = re.compile(re_tags, re.DOTALL)
 
 tags = p.findall(str(tags_all[0]))
 if tags:
  tags_url = {}
  #print tags
  for tag in tags:
   tag_url = tag[0].decode('utf-8')
   #print tag_url
   m_type = tag[1].decode('utf-8')
   tags_url[m_type] = tag_url 
    
 else:
   print "Not Find"
 return tags_url
 
#獲得每一個分類的頁數
def get_pages(tag_url):
 tag_html = gethtml(tag_url)
 #div class="paginator
 soup = BeautifulSoup(tag_html)  #過濾出標志頁面的html
 #print soup
 #<div class="mod_pagenav" id="pager">
 div_page = soup.find_all('div', {'class' : 'mod_pagenav', 'id' : 'pager'})
 #print div_page #len(div_page), div_page[0]
 
 #<a class="c_txt6" href="http://v.qq.com/list/1_2_-1_-1_1_0_24_20_0_-1_0.html" title="25"><span>25</span></a>
 re_pages = r'<a class=.+?><span>(.+?)</span></a>'
 p = re.compile(re_pages, re.DOTALL)
 pages = p.findall(str(div_page[0]))
 #print pages
 if len(pages) > 1:
  return pages[-2]
 else:
  return 1
  
 
def getmovielist(html):
 soup = BeautifulSoup(html)
 
 #<ul class="mod_list_pic_130">
 divs = soup.find_all('ul', {'class' : 'mod_list_pic_130'})
 #print divs
 for div_html in divs:
  div_html = str(div_html).replace('\n', '')
  #print div_html
  getmovie(div_html)
 
 
def getmovie(html):
 global NUM
 global m_type
 global m_site
 
 #<h6 class="caption"> <a href="http://www.tudou.com/albumcover/Z7eF_40EL4I.html" target="_blank" title="徒步觀光隊">徒步觀光隊</a> </h6> <ul class="info"> <li class="desc">法國賣座悲劇片</li> <li class="cast"> </li> </ul> </div> <div class="ext ext_last"> <div class="ext_txt"> <h3 class="ext_title">徒步觀光隊</h3> <div class="ext_info"> <span class="ext_area">地域: 法國</span> <span class="ext_cast">導演: </span> <span class="ext_date">年月: 2009</span> <span class="ext_type">類型: 悲劇</span> </div> <p class="ext_intro">理查德·達奇具有一家小的旅游公司,重要運營法國旅客到非洲年夜草原的旅游辦事。六個法國旅客決議加入理查德·達奇組織的到非洲的一...</p>
 
 re_movie = r'<li><a class=\"mod_poster_130\" href=\"(.+?)\" target=\"_blank\" title=\"(.+?)\"><img.+?</li>'
 p = re.compile(re_movie, re.DOTALL)
 movies = p.findall(html)
 if movies:
  conn = pymongo.Connection('localhost', 27017)
  movie_db = conn.dianying
  playlinks = movie_db.playlinks
  #print movies
  for movie in movies:
   #print movie
   NUM += 1
   print "%s : %d" % ("=" * 70, NUM)
   values = dict(
    movie_title = movie[1],
    movie_url = movie[0],
    movie_site  = m_site,
    movie_type  = m_type
    )
   print values
   playlinks.insert(values)
   print "_" * 70
   NUM += 1
   print "%s : %d" % ("=" * 70, NUM)
 
 #else:
 # print "Not Find"
 
def getmovieinfo(url):
 html = gethtml(url)
 soup = BeautifulSoup(html)
 
 #pack pack_album album_cover
 divs = soup.find_all('div', {'class' : 'pack pack_album album_cover'})
 #print divs[0]
 
 #<a href="http://www.tudou.com/albumplay/9NyofXc_lHI/32JqhiKJykI.html" target="new" title="《血滴子》獨家記載片" wl="1"> </a> 
 re_info = r'<a href=\"(.+?)\" target=\"new\" title=\"(.+?)\" wl=\".+?\"> </a>'
 p_info = re.compile(re_info, re.DOTALL)
 m_info = p_info.findall(str(divs[0]))
 if m_info:
  return m_info
 else:
  print "Not find movie info"
 
 return m_info
 
 
def insertdb(movieinfo):
 global conn
 movie_db = conn.dianying_at
 movies = movie_db.movies
 movies.insert(movieinfo)
 
if __name__ == "__main__":
 global conn
 
 tags_url = "http://v.qq.com/list/1_-1_-1_-1_1_0_0_20_0_-1_0.html"
 #print tags_url
 tags_html = gethtml(tags_url)
 #print tags_html
 tag_urls = gettags(tags_html)
 #print tag_urls
 
 
 for url in tag_urls.items():
  print str(url[1]).encode('utf-8') #,url[0]
  maxpage = int(get_pages(str(url[1]).encode('utf-8')))
  print maxpage
 
  for x in range(0, maxpage):
   #http://v.qq.com/list/1_0_-1_-1_1_0_0_20_0_-1_0.html
   m_url = str(url[1]).replace('0_20_0_-1_0.html', '')
   movie_url = "%s%d_20_0_-1_0.html" % (m_url, x)
   print movie_url
   movie_html = gethtml(movie_url.encode('utf-8'))
   #print movie_html
   getmovielist(movie_html)
   time.sleep(0.1)

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved