0

I'm using parsehub to scrape a bunch of movie names and have a python script export it to an html file and that is working fine. However I want to use an if statement to only print titles that have "The" in them. The structure is fine and it is evaluating the if clause, but it always evaluates to false and I am not sure why. This is my first time using Jinju2 or even hearing about the language.

{% for movie in movies %}
     {% if 'The' in movie %}
          <div name="title">{{movie["title"]}}</div>
     {% endif %}
{% endfor %}

Alternatively, the parsehub tutorial I was following https://help.parsehub.com/hc/en-us/articles/217751808-API-Tutorial-How-to-get-run-data-using-Python-Flask said that there is a way to optionally filter the scraped data but I have looked into it and am not sure how to do so.

# movies.py

from flask import Flask, render_template
import requests
import json

API_KEY = ''
PROJECT_KEY = ''
PROJECT_DATA = f"https://www.parsehub.com/api/v2/projects/{PROJECT_KEY}/last_ready_run/data"
app = Flask(__name__, template_folder='.')

@app.route('/')
def homepage():
  params = {
    'api_key': API_KEY,
  }
  r = requests.get(
      PROJECT_DATA,
      params=params)
  #print("The r var is: \n")
  #print(r)
  return render_template('movies.html', movies=json.loads(r.text)['movies'])

if __name__ == '__main__':
  app.run(host='0.0.0.0', debug=True)

How do I get the jinju2 if statement to evaluate to true, or how do I filter the scraped data?

I used an else statement so I could ensure that the if statement was in fact being evaluated and it is, but it evaluates to false every time.

1 Answers1

0

I think it's this line: {% if 'The' in movie %}

Try changing that to: {% if 'The' in movie["title"] %}

If movie is a dict, then if 'The' in movie is looking for a key named The