0.上传库包。
http://jingyan.baidu.com/article/ae97a646aba4afbbfd461d94.html
1。布置。
index.wsgi
# coding UTF-8
import sys
import os
app_root = os.path.dirname(__file__)
sys.path.insert(0, os.path.join(app_root, 'beautifulsoup4-4.4.1'))
import sae
from myapp import app
application = sae.create_wsgi_app(app)
import urllib2
2.
myapp.py
# -*- coding:utf8 -*-
#encoding = utf-8
from flask import Flask,render_template
from bs4 import BeautifulSoup
import urllib2
import MySQLdb
import re
app = Flask(__name__)
from sae.const import (MYSQL_HOST, MYSQL_HOST_S, MYSQL_PORT, MYSQL_USER, MYSQL_PASS, MYSQL_DB)
## connent = MySQLdb.connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB, port = int(MYSQL_PORT), charset = 'utf8')
## connent.query("""INSERT INTO `app_zncup`.`pic`(`html`) VALUES ('%s')""" % c)
@app.route('/u')
def hello():
zhongjian = '?pn='
tie = 'http://tieba.baidu.com'
piclist = []
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
headers = { 'User-Agent' : user_agent ,'Referer':'http://www.baidu.com/link?url=kNfvsxIMUJxdImh0xTzdwNHEmZaluYPdMmXldEI8xuKwwOr1c59E3uTxwcW9bo9bk3Ow1XXyYa1A9n33X2Cb9q&wd=&eqid=fc9d6c66000ae9640000000656fd09d2'}
request = urllib2.Request( url = 'http://tieba.baidu.com/f?kw=%BF%B9%D1%B9&fr=ala0&tpl=5',headers = headers)
response = urllib2.urlopen(request)
cont = response.read()
soup = BeautifulSoup(cont,'html.parser',from_encoding = 'utf-8')
links = soup.find_all('a',class_='j_th_tit')
for link in links:
tieba = tie + link['href']
tieba_response = urllib2.urlopen(tieba)
tieba_cont = tieba_response.read()
tieba_soup = BeautifulSoup(tieba_cont,'html.parser',from_encoding = 'utf-8')
page_links = tieba_soup.find_all('span',class_='red')
for page_link in page_links:
page_link.get_text()
page_lin = page_link.get_text()
page_l = int(page_lin)
for i in range(1,page_l + 1):
tieweb = tieba + zhongjian + str(i)
tieweb_response = urllib2.urlopen(tieweb)
tieweb_cont = tieweb_response.read()
tieweb_soup = BeautifulSoup(tieweb_cont,'html.parser',from_encoding = 'utf-8')
link_node = tieweb_soup.find_all('img',class_='BDE_Image')
for i in link_node:
c = i['src']
piclist.append(c)
connent = MySQLdb.connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB, port = int(MYSQL_PORT), charset = 'utf8')
connent.query("""INSERT INTO `app_zncup`.`pic`(`html`) VALUES ('%s')""" % c)
# return render_template('index.html',piclist=piclist)
return 'ok'
@app.route('/ky')
def ky():
tupian = []
cha = MySQLdb.connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB, port = int(MYSQL_PORT))
cux = cha.cursor()
cux.execute("SELECT `html`FROM `pic`ORDER BY `id`DESC")
result = cux.fetchall()
for chaxun in result:
tupian.append(chaxun)
return render_template('index.html',piclist = tupian)
@app.route('/dou')
def doub():
xiaozu = 'https://www.douban.com/group/haixiuzu/'
xz_response = urllib2.urlopen(xiaozu)
cont = xz_response.read()
soup = BeautifulSoup(cont,'html.parser',from_encoding = 'utf-8')
link_nodes = soup.find_all('a',href = re.compile(r"https://www.douban.com/group/topic/"))
for link_node in link_nodes:
tieurl = link_node['href']
tie_response = urllib2.urlopen(tieurl)
tie_cont = tie_response.read()
tie_soup = BeautifulSoup(tie_cont,'html.parser',from_encoding = 'utf-8')
pic_nodes = tie_soup.find_all('img',src = re.compile(r"https://img3.doubanio.com/view/group_topic/large/public/"))
for pic_node in pic_nodes:
pir = pic_node['src']
douban = MySQLdb.connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB, port = int(MYSQL_PORT), charset = 'utf8')
douban.query("""INSERT INTO `app_zncup`.`pir`(`html`) VALUES ('%s')""" % pir)
return 'ok'
@app.route('/kdouban')
def kdouban():
kans = []
ck = MySQLdb.connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB, port = int(MYSQL_PORT))
cu = ck.cursor()
cu.execute("SELECT `html`FROM `pir`ORDER BY `id`DESC")
cresult = cu.fetchall()
for meinv in cresult:
kans.append(meinv)
return render_template('dou.html',kans = kans)
if __name__ == '__main__':
app.run()
sae实在太贵,价格有点承受不了,不然的话还是可以搞一个博客出来的。