def crawler(): print "开始任务 [%s] -- \r\n" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))) resp = requests.get( "http://movie.youku.com/?spm=a2hww.20023042.topNav.5~1~3!3~A") resp.encoding = "utf-8" soup = BeautifulSoup(resp.text, "html5lib") div = soup.find(id="m_86988") cols = div.findAll("div", attrs={"class": "yk-col4"}) for col in cols: is_hovers = col.findAll("div", attrs={"class": "v ishover"}) for is_hover in is_hovers: # print col link = is_hover.find("div", attrs={"class": "v-link"}).a['href'] # print link cover = is_hover.div.img["_src"] title = is_hover.find("div", attrs={"class": "v-meta-title"}).text has = movies.movies.query.filter( movies.movies.title == title).first() if not has: new_data = movies.movies( title, cover, "优酷", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())), link, "院线热映") dbSession.add(new_data) dbSession.commit() dbSession.close() time.sleep(0.5)
def pages(): if request.method == 'POST': # put page title = request.form['title'] menuId = request.form['menuId'] page = Page(title=title, menuId=menuId) dbSession.add(page) dbSession.commit() return 'done' else: return 'done'
def menus(): if request.method == 'POST': menu = Menu(title=request.form['title']) dbSession.add(menu) dbSession.commit() return 'done' else: menus = getMenusWithPages() data = { "menus" : menus } return flask.jsonify(**data)
def header(forcedName=None): if request.method == 'POST': fileName = secure_filename(urlparse.unquote(request.headers['X-File-Name'])) if allowed_file(fileName): filePath = os.path.join(app.config['UPLOAD_FOLDER'], fileName) with open(filePath, 'w') as fd: fd.write(request.data) # delete existing header dbSession.query(ImageTag).filter(ImageTag.tag == "header").delete() tag = ImageTag(hash=md5(filePath), tag='header', path=fileName) dbSession.add(tag) dbSession.commit() return 'done' else: imageTag = dbSession.query(ImageTag).filter(ImageTag.tag == "header").first() data = { 'url' : _getHeaderUrl() } return flask.jsonify(**data)
#coding=utf-8 from models import Posts,dbSession new = Posts.Posts("搞","萌") dbSession.add(new) dbSession.commit() dbSession.close()