Python Flask使用html post参数

时间:2019-05-08 05:42:08

标签: python selenium-webdriver flask

我正在使用python进行爬网,并且正在使用flask

但是,我已经确认我从html表单中收到了post参数,但是之后,它在def中仍然失败



app = Flask(__name__)

@app.route('/search', methods = ['POST'])
def search():
   if request.method == 'POST':
      keyword = request.form['keyword'] 
      #keyword = request.args.get('keyword')

      driver.get('https://manage.searchad.naver.com/customers/1625985/tool/keyword-planner')
      driver.find_element_by_xpath('//*[@id="wrap"]/div/div/div[1]/div[1]/div/div/div/div[2]/div[1]/div[1]/div[2]/form/div[1]/div/div/textarea').send_keys(keyword)
      driver.find_element_by_xpath('//*[@id="wrap"]/div/div/div[1]/div[1]/div/div/div/div[2]/div[1]/div[1]/div[2]/form/div[4]/div/div/ul/li/button/span').click()


      try:
        title = WebDriverWait(driver, 10) \
            .until(EC.presence_of_element_located((By.CSS_SELECTOR, "td.text-left.txt-l > span > span")))
        html = driver.page_source
        soup = BeautifulSoup(html, 'html.parser')

        datas = []
        data = {'title': '', 'month_search_pc': '', 'month_search_mobile': '', 'month_avg_click_pc': '', 'month_avg_click_mobile': '', 'month_avg_rate_pc': '', 'month_avg_rate_mobile': '', 'competition_degree': '', 'adv_cnt': ''}
        @app.route('/')
        def get_data() -> 'html':
            global datas, data
            i = 0
            for n in soup.find('tbody', attrs={'has-data'}).findAll('tr'):
             i += 1-1
             data['title'] = n.find('td', attrs={'class': ['text-left', 'txt-l']}).get_text()
             data['month_search_pc'] = n.select('td:nth-child(3)')[i].text
             data['month_search_mobile'] = n.select('td:nth-child(4)')[i].text
             data['month_avg_click_pc'] = n.select('td:nth-child(5)')[i].text
             data['month_avg_click_mobile'] = n.select('td:nth-child(6)')[i].text
             data['month_avg_rate_pc'] = n.select('td:nth-child(7)')[i].text
             data['month_avg_rate_mobile'] = n.select('td:nth-child(8)')[i].text
             data['competition_degree'] = n.select('td:nth-child(9)')[i].text
             data['adv_cnt'] = n.select('td:nth-child(10)')[i].text
             datas.append(dict(data))

            return render_template('shop_click_anal.html', result = datas)
        if __name__ == '__main__':
            app.run(debug=True)

      finally:
            print('data-end')

   else :
        print('None')
driver.get('https://manage.searchad.naver.com/customers/1625985/tool/keyword-planner')

try:
    @app.route('/')
    def get_data1() -> 'html':

        return render_template('shop_click_anal.html')
    if __name__ == '__main__':
        app.run(debug=True)

finally:
    print('data-end')

这是我的错误日志

AssertionError AssertionError:处理完第一个请求后,调用了设置函数。这通常表示应用程序中的错误,其中未导入模块,并且装饰器或其他功能的调用为时已晚。 要解决此问题,请确保在应用程序开始处理请求之前,将所有视图模块,数据库模型以及所有相关内容都导入一个中央位置。

这里总是有错误。


def get_data() -> 'html':

0 个答案:

没有答案