Web Scraper Flask App可以在本地完美运行,但是在部署到Heroku时会出现500个服务器错误

时间:2019-12-15 04:02:39

标签: python heroku flask flask-sqlalchemy

我正在为亚马逊制作刮板烧瓶应用程序。我有一个表格,用户必须输入亚马逊产品,他们的预算和电子邮件的链接。在我当地,一切正常,价格低于预算时,用户会收到一封电子邮件。但是当我部署到heroku时,一旦提交表单,就会收到500错误 这是表格

{% extends "bootstrap/base.html" %}

{% block title %}
Dashboard | The Price is Now Right
{% endblock %}

{% block head %}
{{super()}}
{% block favicon %}
<link href="../static/favicon.ico" rel="icon" type="image/x-icon" />
{% endblock %}
{% endblock %}

{% block styles %}
{{super()}}
<link rel="stylesheet" href="{{url_for('.static', filename='dashboard.css')}}">
<link href="https://fonts.googleapis.com/css?family=Cabin:700&display=swap" rel="stylesheet"> 
{% endblock %}

{% block content %}
<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar"
                aria-expanded="false" aria-controls="navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="logo navbar-brand" href="{{ url_for('index') }}">The Price is Now Right</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
            <ul class="nav navbar-nav navbar-right">
                <li><a href="{{ url_for('logout') }}">Log Out</a></li>
            </ul>

        </div>
    </div>
</nav>

<br>

<div class="container-fluid text-center">
    <form method="POST" onsubmit="change()" action="/dashboard">
        1. Enter the URL of the item you want on Amazon:
        <input type="text" class="input" name="product"><br><br><br>
        2. Enter your budget for this item:
        <input type="text" class="input" name="budget"><br><br><br>
        3. Enter your gmail; We will only use it to send you updates:
        <input type="text" class="input" name="email" value={{ name }}><br><br><br>
        <button class="btn btn-primary btn-lg" type="submit" id="enter">Start Price Tracking!</button>
    </form>
    <br>
    <img class="tracker" src="../static/tracker.gif">
</div>

{%- block footer %}
<footer>
    <span class="container-fluid">
        <h4 class="created text-center">Developed with Flask, a Python MicroFramework</h4>        
  </span>
</footer>
{%- endblock footer %}

<script>
    function change() {
        alert("Price Tracking in Progress...\nWe will send you an email once the price goes down!");
    }
</script>

</div>

{% endblock %}

这是我的app.py中的发送电子邮件功能和仪表板路线

def check_price(): 
    URL = request.form.get('product')   
    budget = request.form.get('budget')
    mail = request.form.get('email')
    page = requests.get(URL, headers=headers)

    soup = BeautifulSoup(page.content, 'html')

    price = soup.find(id="price_inside_buybox").text.strip()
    converted_price = float(price[5:8])

    if (converted_price < float(budget)):
        send_mail(mail, URL)



def send_mail(address, link):   

    server = smtplib.SMTP('smtp.gmail.com', 587)
    #ehlo establishes connection to email
    server.ehlo()
    #tls encrypts
    server.starttls()
    server.ehlo()

    server.login('fake@gmail.com', 'xxxxxxxxxxxx')

    subject = 'Price fell down! The price is now right!'
    body = f'Check the amazon link! {link}'

    msg = f"Subject: {subject}\n\n{body}"

    server.sendmail(
        'fake@gmail.com',
        address,
        msg
    )

    print('Email sent')

    server.quit()
@app.route('/dashboard')
@login_required
def dashboard():
    return render_template('dashboard.html', name=current_user.email)


@app.route('/dashboard', methods=['POST'])
@login_required
def submission():
    while(True):
        check_price()
        time.sleep(86400)    
    return redirect(url_for('index'))

我认为网络抓取存在问题,因为在heroku日志中,我得到了:

File "/app/app.py", line 142, in submission
2019-12-15T03:47:25.681783+00:00 app[web.1]: check_price()
2019-12-15T03:47:25.681785+00:00 app[web.1]: File "/app/app.py", line 56, in check_price
**2019-12-15T03:47:25.681786+00:00 app[web.1]: price = soup.find(id="price_inside_buybox").text.strip()
2019-12-15T03:47:25.681788+00:00 app[web.1]: AttributeError: 'NoneType' object has no attribute 'text'**
2019-12-15T03:47:25.682929+00:00 app[web.1]: 10.47.227.158 - - [15/Dec/2019:03:47:25 +0000] "POST /dashboard HTTP/1.1" 500 290 "https://price-is-now-right.herokuapp.com/dashboard" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36"
2019-12-15T03:47:25.682885+00:00 heroku[router]: at=info method=POST path="/dashboard" host=price-is-now-right.herokuapp.com request_id=468a1c66-bbb0-47dd-a2e2-377cb5f59047 fwd="99.227.177.217" dyno=web.1 connect=1ms service=1500ms status=500 bytes=469 protocol=https

我很困惑,因为我使用示例URL执行脚本,并且效果很好

2019-12-15T03:47:25.990726+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=price-is-now-right.herokuapp.com request_id=2b6e23b1-c1b6-44cd-998b-413cd0c3c6c3 fwd="99.227.177.217" dyno=web.1 connect=0ms service=2ms status=404 bytes=385 protocol=https
2019-12-15T03:47:25.990904+00:00 app[web.1]: 10.47.227.158 - - [15/Dec/2019:03:47:25 +0000] "GET /favicon.ico HTTP/1.1" 404 232 "https://price-is-now-right.herokuapp.com/dashboard" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36"

0 个答案:

没有答案