<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 <h2>Caveat</h2>
Web sites often change the format of their pages so this may not always work. If it doesn't, rework the examples after examining the html content of the page (most browsers will let you see the html source - look for a "page source" option - though you might have to turn on the developer mode in your browser preferences. For example, on Chrome you need to click the "developer mode" check box under Extensions in the Preferences/Options menu. 
<h3>Import necessary modules</h3>
Step1:  <h3>The http request response cycle</h3>
Step2:  <h3>Set up the BeautifulSoup object</h3>
Step3:  <h3>BS4 functions</h3>
<h4>find_all finds all instances of a specified tag</h4>
<h4>returns a result_set (a list)</h4>
Step4:  <h4>find finds the first instance of a specified tag</h4>
<h4>returns a bs4 element</h4>
Step5:  <h4>bs4 functions can be recursively applied on elements</h4>
Step6:  <h4>Both find as well as find_all can be qualified by css selectors</h4>
<li>using selector=value
<li>using a dictionary
Step7:  <h4>get_text() returns the marked up text (the content) enclosed in a tag.</h4>
<li>returns a string
Step8:  <h4>get returns the value of a tag attribute</h4>
<li>returns a string
Step9:  <h1>A function that returns a list containing recipe names, recipe descriptions (if any) and recipe urls</h1>
Step10:  <h2>Let's write a function that</h2>
<h3>given a recipe link</h3>
<h3>returns a dictionary containing the ingredients and preparation instructions</h3>
Step11:  <h2>Construct a list of dictionaries for all recipes</h2>
Step12:  <h1>Logging in to a web server</h1>
<h2>Get username and password</h2>
<li>Best to store in a file for reuse
<li>You will need to set up your own login and password and place them in a file called wikidata.txt
<li>Line one of the file should contain your username
<li>Line two your password
Step13:  <h3>Construct an object that contains the data to be sent to the login page</h3>
Step14:  <h3>get the value of the login token</h3>
Step15:  <h3>Setup a session, login, and get data</h3>
<ASSISTANT_TASK:>
Python Code:
import requests
from bs4 import BeautifulSoup
Explanation: <h2>Caveat</h2>
Web sites often change the format of their pages so this may not always work. If it doesn't, rework the examples after examining the html content of the page (most browsers will let you see the html source - look for a "page source" option - though you might have to turn on the developer mode in your browser preferences. For example, on Chrome you need to click the "developer mode" check box under Extensions in the Preferences/Options menu. 
<h3>Import necessary modules</h3>
End of explanation
url = "http://www.epicurious.com/search/Tofu Chili"
response = requests.get(url)
if response.status_code == 200:
    print("Success")
else:
    print("Failure")
keywords = input("Please enter the things you want to see in a recipe")
url = "http://www.epicurious.com/search/" + keywords
response = requests.get(url)
if response.status_code == 200:
    print("Success")
else:
    print("Failure")
Explanation: <h3>The http request response cycle</h3>
End of explanation
results_page = BeautifulSoup(response.content,'lxml')
print(results_page.prettify())
#print(results_page)
Explanation: <h3>Set up the BeautifulSoup object</h3>
End of explanation
all_a_tags = results_page.find_all('a')
print(type(all_a_tags))
all_a_tags[0:5]
Explanation: <h3>BS4 functions</h3>
<h4>find_all finds all instances of a specified tag</h4>
<h4>returns a result_set (a list)</h4>
End of explanation
div_tag = results_page.find('div')
print(div_tag)
type(div_tag)
Explanation: <h4>find finds the first instance of a specified tag</h4>
<h4>returns a bs4 element</h4>
End of explanation
div_tag.find('a')
Explanation: <h4>bs4 functions can be recursively applied on elements</h4>
End of explanation
#When using this method and looking for 'class' use 'class_' (because class is a reserved word in python)
#Note that we get a list back because find_all returns a list
type(results_page.find_all('article',class_="recipe-content-card"))
#Since we're using a string as the key, the fact that class is a reserved word is not a problem
#We get an element back because find returns an element
type(results_page.find('article',{'class':'recipe-content-card'}))
Explanation: <h4>Both find as well as find_all can be qualified by css selectors</h4>
<li>using selector=value
<li>using a dictionary
End of explanation
results_page.find('article',{'class':'recipe-content-card'}).get_text()
Explanation: <h4>get_text() returns the marked up text (the content) enclosed in a tag.</h4>
<li>returns a string
End of explanation
recipe_tag = results_page.find('article',{'class':'recipe-content-card'})
recipe_link = recipe_tag.find('a')
print("a tag:",recipe_link)
link_url = recipe_link.get('href')
print("link url:",link_url)
print(type(link_url))
Explanation: <h4>get returns the value of a tag attribute</h4>
<li>returns a string
End of explanation
def get_recipes(keywords):
    recipe_list = list()
    import requests
    from bs4 import BeautifulSoup
    url = "http://www.epicurious.com/search/" + keywords
    response = requests.get(url)
    if not response.status_code == 200:
        return None
    try:
        results_page = BeautifulSoup(response.content,'lxml')
        recipes = results_page.find_all('article',class_="recipe-content-card")
        for recipe in recipes:
            recipe_link = "http://www.epicurious.com" + recipe.find('a').get('href')
            recipe_name = recipe.find('a').get_text()
            try:
                recipe_description = recipe.find('p',class_='dek').get_text()
            except:
                recipe_description = ''
            recipe_list.append((recipe_name,recipe_link,recipe_description))
        return recipe_list
    except:
        return None
get_recipes("Tofu chili")
get_recipes('Nothing')
Explanation: <h1>A function that returns a list containing recipe names, recipe descriptions (if any) and recipe urls</h1>
End of explanation
recipe_link = "http://www.epicurious.com" + '/recipes/food/views/spicy-lemongrass-tofu-233844'
def get_recipe_info(recipe_link):
    recipe_dict = dict()
    import requests
    from bs4 import BeautifulSoup
    try:
        response = requests.get(recipe_link)
        if not response.status_code == 200:
            return recipe_dict
        result_page = BeautifulSoup(response.content,'lxml')
        ingredient_list = list()
        prep_steps_list = list()
        for ingredient in result_page.find_all('li',class_='ingredient'):
            ingredient_list.append(ingredient.get_text())
        for prep_step in result_page.find_all('li',class_='preparation-step'):
            prep_steps_list.append(prep_step.get_text().strip())
        recipe_dict['ingredients'] = ingredient_list
        recipe_dict['preparation'] = prep_steps_list
        return recipe_dict
    except:
        return recipe_dict
        
get_recipe_info(recipe_link)
Explanation: <h2>Let's write a function that</h2>
<h3>given a recipe link</h3>
<h3>returns a dictionary containing the ingredients and preparation instructions</h3>
End of explanation
def get_all_recipes(keywords):
    results = list()
    all_recipes = get_recipes(keywords)
    for recipe in all_recipes:
        recipe_dict = get_recipe_info(recipe[1])
        recipe_dict['name'] = recipe[0]
        recipe_dict['description'] = recipe[2]
        results.append(recipe_dict)
    return(results)
get_all_recipes("Tofu chili")
Explanation: <h2>Construct a list of dictionaries for all recipes</h2>
End of explanation
with open('wikidata.txt') as f:
    contents = f.read().split('\n')
    username = contents[0]
    password = contents[1]
Explanation: <h1>Logging in to a web server</h1>
<h2>Get username and password</h2>
<li>Best to store in a file for reuse
<li>You will need to set up your own login and password and place them in a file called wikidata.txt
<li>Line one of the file should contain your username
<li>Line two your password
End of explanation
payload = {
    'wpName': username,
    'wpPassword': password,
    'wploginattempt': 'Log in',
    'wpEditToken': "+\\",
    'title': "Special:UserLogin",
    'authAction': "login",
    'force': "",
    'wpForceHttps': "1",
    'wpFromhttp': "1",
    #'wpLoginToken': ‘', #We need to read this from the page
    }
Explanation: <h3>Construct an object that contains the data to be sent to the login page</h3>
End of explanation
def get_login_token(response):
    soup = BeautifulSoup(response.text, 'lxml')
    token = soup.find('input',{'name':"wpLoginToken"}).get('value')
    return token
Explanation: <h3>get the value of the login token</h3>
End of explanation
with requests.session() as s:
    response = s.get('https://en.wikipedia.org/w/index.php?title=Special:UserLogin&returnto=Main+Page')
    payload['wpLoginToken'] = get_login_token(response)
    #Send the login request
    response_post = s.post('https://en.wikipedia.org/w/index.php?title=Special:UserLogin&action=submitlogin&type=login',
                           data=payload)
    #Get another page and check if we’re still logged in
    response = s.get('https://en.wikipedia.org/wiki/Special:Watchlist')
    data = BeautifulSoup(response.content,'lxml')
    print(data.find('div',class_='mw-changeslist').get_text())
dir(requests.session())
Explanation: <h3>Setup a session, login, and get data</h3>
End of explanation
<END_TASK>