<jupyter_start><jupyter_text>Goal: Find the recipe with the best success of luring each Pokemon in the game Pokemon Quest.<jupyter_code># Pull Pokemon Stats from "https://www.serebii.net/quest/pokemon.shtml"
from bs4 import BeautifulSoup
import urllib.request
import requests
# requires from bs4 import BeautifulSoup
# import urllib.request
# import requests
def getSoup( url ):
    # Getting the webpage, creating a Response object.
    response = requests.get(url)

    # Extracting the source code of the page.
    data = response.text

    # Passing the source code to BeautifulSoup to create a BeautifulSoup object for it.
    soup = BeautifulSoup(data, 'html.parser')
    
    return soup
from operator import itemgetter

def printTopTen( tupList, index ):
    tupList.sort(key=itemgetter(index), reverse=True)
    count = 0
    for item in tupList:
        count += 1
        print(item)
        if( count == 10 ):
            break
# Main
def findBestRecipe( name, url ):
    baseURL = "https://www.serebii.net"
    soup = getSoup(baseURL + url)

    #h3s = soup.findAll("td", {"class":"cen"})
    tables = soup.findAll("table", {"class":"dextable"})
    links = soup.findAll("a")

    '''
    for link in links:
        print(link, end = " -> ")
        text = link.findAll(text=True)
        print(text)
    '''

    nameTable = tables[0].findAll("td")
    soupName = nameTable[4].findAll(text=True)
    soupName = soupName[0]
    #print("\t", soupName)

    d_ingredients = {}
    l_ingredients = []

    ingTds = tables[2].findAll("td", {"class":["foomoon","fooinfo"]})
    for i in range(0,len(ingTds),2):
        quality = str(ingTds[i].findAll(text=True)[0])
        items = str(ingTds[i+1].findAll(text=True))
        d_ingredients[quality] = items
        l_ingredients.append((quality, items))
    
    #print(ingredients)
    
    maxName = "none"
    maxPercentage = 0
    maxRecipe = "none"
    
    list_index = 0
    for i in range(3,len(tables)):
        trs = tables[i].findAll("tr")
        #print(l_ingredients[list_index][0])
        for row in trs:
            tds = row.findAll("td")
            if( len(tds) == 7 ):
                number = str(tds[0].findAll(text=True)[0]).strip()
                pname = str(tds[3].findAll(text=True)[1])
                percent = str(tds[6].findAll(text=True)[0]).strip()
                percent = float(percent.strip('%'))
                #print(number, pname, percent)
                if (pname == name and percent > maxPercentage ):
                    maxPercentage = percent
                    maxName = name
                    maxRecipe = l_ingredients[list_index][1]
        list_index += 1

    return(maxName, maxPercentage, maxRecipe)


    
    
urls = ["/quest/recipes/hotpotalacube.shtml", "/quest/recipes/plaincrepealacube.shtml", "/quest/recipes/mulliganstewalacube.shtml", "/quest/recipes/yellowcurryalacube.shtml", "/quest/recipes/sludgesoupalacube.shtml", "/quest/recipes/veggiesmoothiealacube.shtml" ]
    
#for url in urls:
#    print(findBestRecipe("Snorlax", url))


# Main
baseURL = "https://www.serebii.net"
soup = getSoup(baseURL + "/quest/pokemon.shtml")

#h3s = soup.findAll("td", {"class":"cen"})
trs = soup.findAll("tr")
links = soup.findAll("a")

'''
for link in links:
    print(link, end = " -> ")
    text = link.findAll(text=True)
    print(text)
'''
pokelist = []
count = 1

for tr in trs:
    inner_td = tr.findAll("td", {"class":"fooinfo"})
    if len(inner_td) == 5 :
        name = inner_td[0].findAll(text=True)[1]
        hp = int(inner_td[2].findAll(text=True)[0])
        atk = int(inner_td[3].findAll(text=True)[0])
        pokemon = (name, hp, atk, hp+atk)
        pokelist.append(pokemon)
        print(count,pokemon)
        count += 1
        links = inner_td[4].find_all('a', href=True)
        if links:
            bestChance = 0
            bestResult = "none"
            for link in links:
                #print("\t", link['href'])
                try:
                    result = findBestRecipe(name, link['href'])
                    if( result[1] > bestChance ):
                        bestChance = result[1]
                        bestResult = result
                except:
                    print("\t", "*!* a failure occured...")
            print("\tBest result:", bestResult)
        

print("Top HP:")
printTopTen( pokelist, 1 )
print("\nTop Attack:")
printTopTen( pokelist, 2 )
print("\nTop Strongest:")
printTopTen( pokelist, 3 )

    
    #print( str(count) + ". " + ''.join(h.findAll(text=True)))<jupyter_output>1 ('Bulbasaur', 125, 75, 200)
	Best result: ('Bulbasaur', 80.0, "['3 Big Root', '1 Apricorn', '1 Rainbow Matter']")
2 ('Ivysaur', 400, 100, 500)
3 ('Venusaur', 550, 150, 700)
4 ('Charmander', 75, 125, 200)
	 *!* a failure occured...
	Best result: ('Charmander', 19.76, "['4 or more Big Root']")
5 ('Charmeleon', 100, 400, 500)
6 ('Charizard', 200, 600, 800)
7 ('Squirtle', 100, 100, 200)
	Best result: ('Squirtle', 33.56, "['4 or more Icy Rock']")
8 ('Wartortle', 250, 250, 500)
9 ('Blastoise', 400, 400, 800)
10 ('Caterpie', 110, 90, 200)
	Best result: ('Caterpie', 20.0, "['5 Bluk Berry']")
11 ('Metapod', 275, 25, 300)
12 ('Butterfree', 150, 250, 400)
13 ('Weedle', 100, 100, 200)
	Best result: ('Weedle', 12.5, "['5 Apricorn']")
14 ('Kakuna', 250, 50, 300)
15 ('Beedrill', 150, 250, 400)
16 ('Pidgey', 100, 100, 200)
	Best result: ('Pidgey', 25.0, "['3 Bluk Berry', '2 Fossils']")
17 ('Pidgeotto', 250, 250, 500)
18 ('Pidgeot', 400, 400, 800)
19 ('Rattata', 75, 125, 200)
	Best result: ('Rattata'[...]
{'fraction_non_alphanumeric': 0.16148174788707068, 'fraction_numerical': 0.04819277108433735, 'mean_word_length': 2.7104736490993995, 'pattern_counts': {'":': 5, '<': 4, '<?xml version=': 0, '>': 8, 'https://': 3, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '356306', 'license': 'MIT', 'n_tokens_mistral': 2199, 'n_tokens_neox': 1930, 'n_words': 473, 'path': 'PokemonQuest Stats Scraping.ipynb', 'repo_name': 'murfrosoft/webscraping', 'size': '5561'}